import React, { useState, useEffect, useRef } from 'react';
import ComplyCubeRN from './ComplyCubeSDK';
import { View } from 'react-native';

interface CCProps {
  settings: Object;
  onSuccess: any; // TODO: Add types
  onCancel: any;
  onError: any;
  onEventCallback: any;
}
export default function ComplyCube({
  settings,
  onSuccess,
  onCancel,
  onError,
  onEventCallback,
}: CCProps) {
  const [ error, setError ] = useState(false);
  const complycubeRef = useRef<ComplyCubeRN | null>(null);
  if (complycubeRef.current == null) {
    complycubeRef.current = new ComplyCubeRN();
  }

  useEffect(() => {
    const complycube = complycubeRef.current;
    if (!complycube) return;

    complycube.addHandlers(onSuccess, onError, onCancel, onEventCallback);
    if (complycube.setSettings(settings) == false) {
      setError(true);
      complycube.removeHandlers();
      return;
    }

    complycube.mount();
    return () => {
      complycube.removeHandlers();
    };
  }, []);
  if(error) return (<View />);
  return <View/>;
}
