import React, { Component } from "react";
import PropTypes from "prop-types";
import { View} from "react-native";
import { Enx, removeNativeEvents, setNativeEvents } from "./Enx";
import EnxPlayerView from "./views/EnxPlayerView";
import { sanitizePlayerViewEvents } from "./helpers/EnxStreamHelper";
import 'react-native-get-random-values';
import {v4 as uuid4} from "uuid";
import { isNull } from "underscore";
class EnxStream extends Component {
  constructor(props) {
    super(props);
    this.state = {
      streamId: this.props.isPreview ? "abc-123-xyz" : uuid4(),
      isFrontCamera: this.props.isFrontCamera
    };
  }

  UNSAFE_componentWillMount() {
    if (this.props.eventHandlers != null) {
      const publisherEvents = sanitizePlayerViewEvents(this.props.eventHandlers);
      setNativeEvents(publisherEvents);
    }
    Enx.initStream(this.state.streamId, this.state.isFrontCamera);
  }

  componentWillUnmount() {
    if (this.props.eventHandlers != null) {
      const events = sanitizePlayerViewEvents(this.props.eventHandlers);
      removeNativeEvents(events);
    }
  }

  render() {
    const { streamId, isFrontCamera } = this.state;

    console.log("EnxStream.id", streamId);
    return <EnxPlayerView streamId={streamId} isLocal="local" isFrontCamera={isFrontCamera} {...this.props} />;
  }
}

const viewPropTypes = View.propTypes;

EnxStream.propTypes = {
  ...viewPropTypes,
  eventHandlers: PropTypes.object, // eslint-disable-line react/forbid-prop-types
  isPreview: PropTypes.bool,
  isFrontCamera: PropTypes.bool
};

EnxStream.defaultProps = {
  eventHandlers: {},
  isPreview: false,
  isFrontCamera: true
};

export default EnxStream;
