package com.example.libytx;

import android.text.TextUtils;
import android.util.Log;

import com.cmos.rtc.control.Voip;
import com.cmos.rtc.control.listener.ConnectState;
import com.cmos.rtc.control.listener.ConnectStateChangeListener;
import com.cmos.rtc.ui.VoipUI;
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 org.json.JSONException;
import org.json.JSONObject;

public class VoipModule extends ReactContextBaseJavaModule implements ConnectStateChangeListener {


    private static final String TAG = "VoipModule";
    private ReactApplicationContext mContext;
    private Callback success;
    private Callback error;

    public VoipModule(ReactApplicationContext reactContext) {
        super(reactContext);
        mContext = reactContext;
    }

    @Override
    public String getName() {
        return "VoipModule";
    }

    @ReactMethod
    public void init(String params) {
        Log.e(TAG, "init");

        try {
            JSONObject object = new JSONObject(params);

            String appKey = object.getString("appKey");
            String token = object.getString("token");

            Voip.init(mContext, TextUtils.isEmpty(appKey) ? "8ab3bdf156e3e63d0156e43bb86a0006" : appKey, TextUtils.isEmpty(token) ? "57d9675b075e4b46882bedc900bbaccc" : token);
            VoipUI.getInstance().init(mContext);

        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

    @ReactMethod
    public void login(String params, Callback success, Callback error) {
        Log.e(TAG, "login");
        this.success = success;
        this.error = error;

        try {
            JSONObject object = new JSONObject(params);

            String host = object.getString("host");
            String port = object.getString("port");
            String account = object.getString("account");

            Voip.getInstance().login(host, port, account);
            Voip.getInstance().setOnConnectListener(this);

        } catch (JSONException e) {
            e.printStackTrace();
            error.invoke(e.getMessage());

        }

    }

    @ReactMethod
    public void logout(Callback success, Callback error) {
        this.success = success;
        this.error = error;
        Log.e(TAG, "logout");
        Voip.getInstance().logout();
        Voip.getInstance().setOnConnectListener(this);
    }

    @ReactMethod
    public void makeVoiceCall(String num) {
        Voip.getInstance().getCallService().makeVoiceCall(num);
    }

    @ReactMethod
    public void makeVideoCall(String num) {
        Voip.getInstance().getCallService().makeVideoCall(num);
    }

    @Override
    public void onConnectStateChanged(ConnectState connectState, String s) {
        if (connectState == ConnectState.CONNECT_SUCCESS) {
            success.invoke("LOGIN_SUCCESS");
        } else if (connectState == ConnectState.CONNECT_FAILED) {
            error.invoke(s);
        } else if (connectState == ConnectState.ON_INIT_ERROR) {
            error.invoke(s);
        } else if (connectState == ConnectState.LOGOUT) {
            success.invoke("LOGOUT_SUCCESS");
        }
    }
}
