package com.reactnativequickomsdk;

import android.util.Log;

import com.beowulfchain.beowulfchainsdk.AppData.AppData.ChatMessage.ChatMess;
import com.beowulfchain.beowulfchainsdk.AppData.AppData.ChatMessage.ChatMessStatus;
import com.beowulfchain.beowulfchainsdk.BWFCallCenter;
import com.beowulfchain.beowulfchainsdk.util.ChatObservation;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

public class CallCenter {
    private DeviceEventModule deviceEventModule;

    public CallCenter(ReactApplicationContext reactContext) {
        deviceEventModule = new DeviceEventModule(reactContext);
    }

    // integrate framework and preparation
    public void initSDK(String apiKey) {
        BWFCallCenter.getInstance().configureWithApiKey(apiKey);
        BWFCallCenter.getInstance().startAsAnonymousRequester();
        BWFCallCenter.getInstance().SetSDKInitCallback(new BWFCallCenter.SdkCallBack() {
            @Override
            public void onChatResult(boolean b) {
                Log.d("chat-result:", "" + b);

                try {
                    JSONObject msg = new JSONObject();
                    msg.put("chat_result", "" + b);
//                    deviceEventModule.initSDK(msg.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onSipResult(boolean b) {
                Log.d("sip-result:", "" + b);

                try {
                    JSONObject msg = new JSONObject();
                    msg.put("sip_result", "" + b);
//                    deviceEventModule.initSDK(msg.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onSuccess(boolean b) {
                try{
                    Log.d("success-init:", "" + b);
                    JSONObject msg = new JSONObject();
                    msg.put("success", "" + b);
                    deviceEventModule.initSDK(msg.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    // request voice call
    public void requestVoiceCallWithDisplayName(String optionalName, String groupName) {
        BWFCallCenter.getInstance().requestVoiceCallWithDisplayName(optionalName, groupName);
    }

    public void registerErrorCallBack(final Callback callBack) {
        BWFCallCenter.getInstance().RegisterErrorCallBack(new BWFCallCenter.ErrorCallBack() {
            @Override
            public void onNumberOfSupporterRemainded(int i) {
                try{
                    JSONObject msg = new JSONObject();
                    msg.put("number_of_supporter_remained", "" + i);
                    callBack.invoke(msg.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void registerState() {
        BWFCallCenter.getInstance().RegisterState(new BWFCallCenter.CallStateChange() {
            @Override
            public void onCallStateChage(int i) {
                try{
                    JSONObject msg = new JSONObject();
                    msg.put("call_state_chage", "" + i);
                    deviceEventModule.registerStatusState(msg.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void registerVoiceVideoCallCallback(final Callback callBack) {
        BWFCallCenter.getInstance().RegisterVoiceVideoCallCallback(new BWFCallCenter.VoiceVideoCallCallBack() {
            @Override
            public void onResult(int i, String s) {
                try{
                    JSONObject msg = new JSONObject();
                    JSONObject msgChild = new JSONObject();
                    msgChild.put("code", "" + i);
                    msgChild.put("message", "" + s);
                    msg.put("result", msgChild.toString());
                    callBack.invoke(msg.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    // request chat
    public void requestChatWithDisplayName(String optional_name, String group_name) {
        BWFCallCenter.getInstance().requestChatWithDisplayName(optional_name, group_name);
    }

    public void registerChatObserver() {
        BWFCallCenter.getInstance().RegisterChatObserver(new ChatObservation() {
            @Override
            public void onUserDataChanged(ChatMess chatMess) {
                try {
                    JSONObject msg = new JSONObject();
                    msg.put("id", chatMess.getMsgId());
                    msg.put("to", chatMess.getMessageTo());
                    msg.put("from", chatMess.getMessageFrom());
                    msg.put("content", chatMess.getPushContent());
                    deviceEventModule.registerChatObserver(msg.toString());
                } catch (JSONException e) {
                }
            }
        });
    }

    public void sendText(String messContent, String msgIdTo) {
        BWFCallCenter.getInstance().sendText(messContent, msgIdTo);
    }

    public void sendImage(File compressedImgFile) {
        BWFCallCenter.getInstance().sendImage(compressedImgFile);
    }

    public void sendSeen(String msgId, String msgIdTo, String msgIdFrom, String seen) {
        BWFCallCenter.getInstance().SendSeen(new ChatMessStatus(msgId, msgIdTo, msgIdFrom, seen));
    }
}
