package com.reactnativequickomsdk;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.uimanager.IllegalViewOperationException;

import java.io.File;

public class CallCenterModule extends ReactContextBaseJavaModule {
    private CallCenter callCenter;

    public CallCenterModule(ReactApplicationContext reactContext) {
        super(reactContext); //required by React Native
        this.callCenter = new CallCenter(reactContext);
    }

    @Override
    //getName is required to define the name of the module represented in JavaScript
    public String getName() {
        return "CallCenter";
    }

    @ReactMethod
    public void initSDK(String apiKey, Callback errorCallback, Callback successCallback) {
        try {
            callCenter.initSDK(apiKey);
            successCallback.invoke("success");
        } catch (IllegalViewOperationException e) {
            errorCallback.invoke(e.getMessage());
        }
    }

    @ReactMethod
    public void requestVoiceCallWithDisplayName(String optionalName, String groupName, Callback errorCallback, Callback successCallback) {
        try {
            callCenter.requestVoiceCallWithDisplayName(optionalName, groupName);
            successCallback.invoke("success");
        } catch (IllegalViewOperationException e) {
            errorCallback.invoke(e.getMessage());
        }
    }

    @ReactMethod
    public void registerErrorCallBack(Callback errorCallback, final Callback successCallback) {
        try {
            callCenter.registerErrorCallBack(new Callback() {
                @Override
                public void invoke(Object... args) {
                    successCallback.invoke(args);
                }
            });
        } catch (IllegalViewOperationException e) {
            errorCallback.invoke(e.getMessage());
        }
    }

    @ReactMethod
    public void registerState(Callback errorCallback, Callback successCallback) {
        try {
            callCenter.registerState();
            successCallback.invoke("success");
        } catch (IllegalViewOperationException e) {
            errorCallback.invoke(e.getMessage());
        }
    }

    @ReactMethod
    public void registerVoiceVideoCallCallback(Callback errorCallback, final Callback successCallback) {
        try {
            callCenter.registerVoiceVideoCallCallback(new Callback() {
                @Override
                public void invoke(Object... args) {
                    successCallback.invoke(args);
                }
            });
        } catch (IllegalViewOperationException e) {
            errorCallback.invoke(e.getMessage());
        }
    }

    @ReactMethod
    public void requestChatWithDisplayName(String optional_name, String group_name, Callback errorCallback, Callback successCallback) {
        try {
            callCenter.requestChatWithDisplayName(optional_name, group_name);
            successCallback.invoke("success");
        } catch (IllegalViewOperationException e) {
            errorCallback.invoke(e.getMessage());
        }
    }

    @ReactMethod
    public void registerChatObserver(Callback errorCallback, Callback successCallback) {
        try {
            callCenter.registerChatObserver();
            successCallback.invoke("success");
        } catch (IllegalViewOperationException e) {
            errorCallback.invoke(e.getMessage());
        }
    }

    @ReactMethod
    public void sendText(String messContent, String msgIdTo, Callback errorCallback, Callback successCallback) {
        try {
            callCenter.sendText(messContent, msgIdTo);
            successCallback.invoke("success");
        } catch (IllegalViewOperationException e) {
            errorCallback.invoke(e.getMessage());
        }
    }

    @ReactMethod
    public void sendImage(File compressedImgFile, Callback errorCallback, Callback successCallback) {
        try {
            callCenter.sendImage(compressedImgFile);
            successCallback.invoke("success");
        } catch (IllegalViewOperationException e) {
            errorCallback.invoke(e.getMessage());
        }
    }

    @ReactMethod
    public void sendSeen(String msgId, String msgIdTo, String msgIdFrom, String seen, Callback errorCallback, Callback successCallback) {
        try {
            callCenter.sendSeen(msgId, msgIdTo, msgIdFrom, seen);
            successCallback.invoke("success");
        } catch (IllegalViewOperationException e) {
            errorCallback.invoke(e.getMessage());
        }
    }
}
