import { NativeModules, NativeEventEmitter, Platform, requireNativeComponent } from 'react-native';

const LINKING_ERROR =
  `The package 'react-native-onmeeting-sdk' doesn't seem to be linked. Make sure: \n\n` +
  Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
  '- You rebuilt the app after installing the package\n' +
  '- You are not using Expo Go\n';

const OnmeetingSdk = NativeModules.OnmeetingSdk
  ? NativeModules.OnmeetingSdk
  : new Proxy(
      {},
      {
        get() {
          throw new Error(LINKING_ERROR);
        },
      }
    );

const emitter = new NativeEventEmitter(OnmeetingSdk);

export const OnMeetView = requireNativeComponent('OnmeetingSdk');

export function initSdk(): Promise<string> {
  return OnmeetingSdk.initSdk();
}

export function openMeeting(publicKey: string, accessToken: string): Promise<string> {
  return OnmeetingSdk.openMeeting(publicKey, accessToken);
}

export function destroy(): Promise<string> {
  return OnmeetingSdk.destroy();
}

export function setListener(options: any) {
  if (options) {
    emitter.addListener("onConferenceJoined", data => {
      if (typeof options.onConferenceJoined === "function")
        options.onConferenceJoined(data);
    });

    emitter.addListener("onConferenceTerminated", data => {
      if (typeof options.onConferenceTerminated === "function")
        options.onConferenceTerminated(data);
    });

    emitter.addListener("onConferenceWillJoin", data => {
      if (typeof options.onConferenceWillJoin === "function")
        options.onConferenceWillJoin(data);
    });

    emitter.addListener("onAudioMutedChanged", data => {
      if (typeof options.onAudioMutedChanged === "function")
        options.onAudioMutedChanged(data);
    });

    emitter.addListener("onParticipantJoined", data => {
      if (typeof options.onParticipantJoined === "function")
        options.onParticipantJoined(data);
    });

    emitter.addListener("onParticipantLeft", data => {
      if (typeof options.onParticipantLeft === "function")
        options.onParticipantLeft(data);
    });

    emitter.addListener("onEndpointTextMessageReceived", data => {
      if (typeof options.onEndpointTextMessageReceived === "function")
        options.onEndpointTextMessageReceived(data);
    });

    emitter.addListener("onScreenShareToggled", data => {
      if (typeof options.onScreenShareToggled === "function")
        options.onScreenShareToggled(data);
    });

    emitter.addListener("onParticipantsInfoRetrieved", data => {
      if (typeof options.onParticipantsInfoRetrieved === "function")
        options.onParticipantsInfoRetrieved(data);
    });

    emitter.addListener("onChatMessageReceived", data => {
      if (typeof options.onChatMessageReceived === "function")
        options.onChatMessageReceived(data);
    });

    emitter.addListener("onChatToggled", data => {
      if (typeof options.onChatToggled === "function")
        options.onChatToggled(data);
    });

    emitter.addListener("onVideoMutedChanged", data => {
      if (typeof options.onVideoMutedChanged === "function")
        options.onVideoMutedChanged(data);
    });

    emitter.addListener("onReadyToClose", data => {
      if (typeof options.onReadyToClose === "function")
        options.onReadyToClose(data);
    });
  }  
}
