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 { isNull } from "underscore";
const uuid = require("uuid");

class EnxStream extends Component {
  constructor(props) {
    super(props);
    this.props.isPreview ?this.state = {
      streamId: "abc-123-xyz",
   }  : this.state = {
      streamId: uuid(),
   };
  }

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

  render() {
    if (true) {
      const { streamId } = this.state;
      console.log("EnxStream.id", streamId);
      return <EnxPlayerView streamId={streamId} isLocal="local" {...this.props} />;
    }
    return <View />;
  }
}
const viewPropTypes = View.propTypes;
EnxStream.propTypes = {
  ...viewPropTypes,
  eventHandlers: PropTypes.object // eslint-disable-line react/forbid-prop-types
};
EnxStream.defaultProps = {
  eventHandlers: {}
};
export default EnxStream;
