package com.example.kingway.ptt;

import timber.log.Timber;

import java.util.ArrayList;
import java.util.List;

/**指令返回类
 * 说明：操作UI请在主线程进行
 * */
public class CallBackUtil {
    private static String TAG = "TYT_CallBackUtil";


    /**
     * 返回登录
     * loginState 是否登录成功
     * name 用户名
     */
    public static void callBackLogin(boolean loginState, String name) {
        Timber.tag(TAG).d("callBackLogin loginState=%s name=%s", loginState, name);
    }

    /**
     * 登录状态通知
     * state
     * 0 : 离线
     * 1 : 登陆中
     * 2 : 登陆成功
     * 3 : 注销中
     * 说明：第一次登录 通知状态 先01再02，
     * 离线再上线 通知状态 先00再01
     * <p>
     * loginId 用户ID，登录成功id有效
     */
    public static void callBackLoinState(String state, String loginId) {
        Timber.tag(TAG).d("callBackLoinState state=%s loginId=%s", state, loginId);
    }

    /**
     * 判断账号登录是否冲突
     * true：登录冲突
     * false：登录不冲突
     * */
    public static void callBackLoginConflict(boolean loginConflict) {
        Timber.tag(TAG).d("callBackLoginConflict: %s", loginConflict);
    }

    /**
     * 登录账号有视频功能通知
     */
    public static boolean callBackHaveVideo() {
        Timber.tag(TAG).d("callBackHaveVideo invoked");
        return true;
    }



    /**
     * 查询群组返回
     * state true-成功， false-失败
     */
    public static void callBackOnGroupSuccess(boolean state) {
        Timber.tag(TAG).d("callBackOnGroupSuccess state=%s", state);
    }

    /**
     * 查询群组成员返回
     * state true-成功， false-失败
     */
    public static void callBackOnMemberSuccess(boolean state, List<GroupMemberInfoDto> dtos) {
        try {
            org.json.JSONArray arr = new org.json.JSONArray();
            if (dtos != null && state) {
                for (GroupMemberInfoDto dto : dtos) {
                    org.json.JSONObject obj = new org.json.JSONObject();
                    obj.put("memberId", dto.getMemberId());
                    obj.put("memberName", dto.getGmemberName());
                    obj.put("status", dto.getStatus());
                    arr.put(obj);
                }
            }
            com.stand.standapp.MainActivity.executeJs("if(window.onPttEvent){window.onPttEvent('MemberList', " + arr.toString() + ");}");
        } catch (Exception e) { Timber.tag(TAG).e(e, "Failed to push PTT event to JS"); }
    }

    /**
     * 应用发起讲话返回
     * state true-成功， false-失败
     */
    public static void callBackSpeakPlaySuccess(boolean state) {
        Timber.tag(TAG).d("callBackSpeakPlaySuccess state=%s", state);
        if (state) {
            AudioRecordManager.getInstance().startRecord();
            Timber.tag(TAG).d("AudioRecord started");
        }
        try {
            org.json.JSONObject obj = new org.json.JSONObject();
            obj.put("success", state);
            com.stand.standapp.MainActivity.executeJs("if(window.onPttEvent){window.onPttEvent('SpeakResult', " + obj.toString() + ");}");
        } catch (Exception e) { Timber.tag(TAG).e(e, "Failed to push PTT event to JS"); }
    }

    /**
     * 应用结束讲话返回
     * state true-成功， false-失败
     */
    public static void callBackSpeakReleaseSuccess(boolean state) {
        try {
            org.json.JSONObject obj = new org.json.JSONObject();
            obj.put("success", state);
            com.stand.standapp.MainActivity.executeJs("if(window.onPttEvent){window.onPttEvent('SpeakEnd', " + obj.toString() + ");}");
        } catch (Exception e) { Timber.tag(TAG).e(e, "Failed to push PTT event to JS"); }
    }

    /**
     * 返回群组信息
     * groupId 群组ID
     * groupName 群组名称
     * groupNo 表示此组为第几个组
     * groupCount 表示此组用户成员数
     */
    public static void callBackGroupInfo(String groupId, String groupName, int groupNo, int groupCount) {
        Timber.tag(TAG).d("callBackGroupInfo groupId=%s name=%s no=%d count=%d", groupId, groupName, groupNo, groupCount);
    }

    /**
     * 返回群组集合信息
     * GroupInfoDto 群组信息
     * */
    public static void callBackGroupInfo (ArrayList < GroupInfoDto > groupInfoDtos) {
        try {
            org.json.JSONArray arr = new org.json.JSONArray();
            for (GroupInfoDto dto : groupInfoDtos) {
                org.json.JSONObject obj = new org.json.JSONObject();
                obj.put("groupId", dto.getGroupId());
                obj.put("groupName", dto.getGroupName());
                obj.put("groupNo", dto.getGroupNo());
                arr.put(obj);
            }
            com.stand.standapp.MainActivity.executeJs("if(window.onPttEvent){window.onPttEvent('GroupList', " + arr.toString() + ");}");
        } catch (Exception e) { Timber.tag(TAG).e(e, "Failed to push PTT event to JS"); }
    }

    /**
     * 讲话用户信息通知
     * speckType 讲话状态： 0 ： 表示自己无法讲话； 1 ： 表示自己可以中断讲话人的讲话，可以进行强插讲话
     * talkId 讲话用户ID
     * talkName 讲话用户名字
     * */
    public static void callBackNotifySpker ( int speckType, String groupId, String groupName, String talkId, String talkName){
        Timber.tag(TAG).d("callBackNotifySpker speckType=%d groupId=%s talkId=%s talkName=%s", speckType, groupId, talkId, talkName);
        try {
            org.json.JSONObject obj = new org.json.JSONObject();
            obj.put("speckType", speckType);
            obj.put("groupId", groupId);
            obj.put("groupName", groupName);
            obj.put("talkId", talkId);
            obj.put("talkName", talkName);
            com.stand.standapp.MainActivity.executeJs("if(window.onPttEvent){window.onPttEvent('SpeakerUpdate', " + obj.toString() + ");}");
        } catch (Exception e) { Timber.tag(TAG).e(e, "Failed to push PTT event to JS"); }
    }

    public static void callBackNotifyPlayStatus ( int playStatus, int speckType, String groupId, String groupName, String talkId, String talkName){
        Timber.tag(TAG).d("callBackNotifyPlayStatus playStatus=%d talkId=%s", playStatus, talkId);
        try {
            org.json.JSONObject obj = new org.json.JSONObject();
            obj.put("playStatus", playStatus);
            obj.put("speckType", speckType);
            obj.put("groupId", groupId);
            obj.put("groupName", groupName);
            obj.put("talkId", talkId);
            obj.put("talkName", talkName);
            com.stand.standapp.MainActivity.executeJs("if(window.onPttEvent){window.onPttEvent('PlayStatus', " + obj.toString() + ");}");
        } catch (Exception e) { Timber.tag(TAG).e(e, "Failed to push PTT event to JS"); }
    }

    public static void callBackTempCallNotify (String tempCallNotify){
        Timber.tag(TAG).d("callBackTempCallNotify msg=%s", tempCallNotify);
        try {
            org.json.JSONObject obj = new org.json.JSONObject();
            obj.put("msg", tempCallNotify);
            com.stand.standapp.MainActivity.executeJs("if(window.onPttEvent){window.onPttEvent('TempCall', " + obj.toString() + ");}");
        } catch (Exception e) { Timber.tag(TAG).e(e, "Failed to push PTT event to JS"); }
    }

    public static void callBackNotifyUpdateGroup (String userGroupId, String userGroupName){
        Timber.tag(TAG).d("callBackNotifyUpdateGroup groupId=%s groupName=%s", userGroupId, userGroupName);
        try {
            org.json.JSONObject obj = new org.json.JSONObject();
            obj.put("groupId", userGroupId);
            obj.put("groupName", userGroupName);
            com.stand.standapp.MainActivity.executeJs("if(window.onPttEvent){window.onPttEvent('UpdateGroup', " + obj.toString() + ");}");
        } catch (Exception e) { Timber.tag(TAG).e(e, "Failed to push PTT event to JS"); }
    }

    /**
     *  用户定位信息通知
     *  status 0：获取成功， 1：获取失败
     *  userId 用户ID
     *  latitude 纬度
     *  longitude 经度
     *  time 时间
     */
    public static void callBackNotifyLocationStatus ( int status, String userId,double latitude, double longitude, String time){
        Timber.tag(TAG).d("callBackNotifyLocationStatus status=%d userId=%s lat=%f lng=%f", status, userId, latitude, longitude);
        try {
            org.json.JSONObject obj = new org.json.JSONObject();
            obj.put("status", status);
            obj.put("userId", userId);
            obj.put("latitude", latitude);
            obj.put("longitude", longitude);
            obj.put("time", time);
            com.stand.standapp.MainActivity.executeJs("if(window.onPttEvent){window.onPttEvent('LocationStatus', " + obj.toString() + ");}");
        } catch (Exception e) { Timber.tag(TAG).e(e, "Failed to push PTT event to JS"); }
    }

    /**
     * 用户上报定位信息通知
     * status true：成功， false：失败
     * */
    public static void callBackNotifyUploadLocation ( boolean status){
        Timber.tag(TAG).d("callBackNotifyUploadLocation status=%s", status);
    }

    /**
     * 空中写账号信息通知
     * codeInfo ip=106.15.8.60;id=Android3;gps=1;inv=1
     *          ip=106.15.8.60  用户ip：106.15.8.60
     *          id=Android3  用户账号：Android3
     *          gps=1：1勾选定位，0不勾选定位
     *          inv=1：1勾选单呼，0不勾选单呼
     * */
    public static void callBackNotifyCodeInfo (String codeInfo){
        Timber.tag(TAG).d("callBackNotifyCodeInfo: %s", codeInfo);
    }

    /**
     * 空中写密码信息通知
     * pwdInfo age=123456;pwd=111111
     *          age=123456   经销商密码：123456
     *          pwd=111111   账号密码：111111
     * */
    public static void callBackNotifyPwdInfo (String pwdInfo){
        Timber.tag(TAG).d("callBackNotifyPwdInfo: %s", pwdInfo);
    }

    /**
     * 进入写码模式通知
     * status true：成功，false：失败
     * */
    public static void callBackNotifyCodeOpenStatus ( boolean status){
        Timber.tag(TAG).d("callBackNotifyCodeOpenStatus status=%s", status);
    }

    /**
     * 退出写码模式通知
     * status true：成功，false：失败
     * */
    public static void callBackNotifyCodeExiteStatus ( boolean status){
        Timber.tag(TAG).d("callBackNotifyCodeExiteStatus status=%s", status);
    }

    /**
     * 上传经销商密码通知
     * status true：成功，false：失败
     * */
    public static void callBackNotifyUploadAgentPwd ( boolean status){
        Timber.tag(TAG).d("callBackNotifyUploadAgentPwd status=%s", status);
    }

    /**
     * 上报本机告警信号通知
     * status true：成功，false：失败
     * */
    public static void callBackRequestReportAlarm ( boolean status){
        Timber.tag(TAG).d("callBackRequestReportAlarm status=%s", status);
    }

    /**
     * 停止本机告警信号通知
     * status true：成功，false：失败
     * */
    public static void callBackRequestStopAlarm ( boolean status){
        Timber.tag(TAG).d("callBackRequestStopAlarm status=%s", status);
    }

    /**
     * 收到群成员警告通知
     * memberName 群成员名称
     * latitude 纬度
     * longitude 经度
     * */
    public static void callBackEnotifyAlarm (String memberName,double latitude, double longitude){
        Timber.tag(TAG).w("callBackEnotifyAlarm memberName=%s lat=%f lng=%f", memberName, latitude, longitude);
        try {
            org.json.JSONObject obj = new org.json.JSONObject();
            obj.put("memberName", memberName);
            obj.put("latitude", latitude);
            obj.put("longitude", longitude);
            com.stand.standapp.MainActivity.executeJs("if(window.onPttEvent){window.onPttEvent('Alarm', " + obj.toString() + ");}");
        } catch (Exception e) { Timber.tag(TAG).e(e, "Failed to push PTT event to JS"); }
    }

    /**
     * 收到群成员停止警告通知
     * */
    public static void callBackEnotifyStopAlarm () {
        Timber.tag(TAG).d("callBackEnotifyStopAlarm invoked");
        com.stand.standapp.MainActivity.executeJs("if(window.onPttEvent){window.onPttEvent('StopAlarm', {});}");
    }

    /**
     * 上传NFC信息通知
     * status true：成功，false：失败
     * */
    public static void callBackRequestNfc ( boolean status){
        Timber.tag(TAG).d("callBackRequestNfc status=%s", status);
    }

}
