package com.wdkl.ncs.android.middleware.tcp.channel;

import android.util.Log;

import com.google.gson.Gson;
import com.wdkl.ncs.android.middleware.common.Constant;
import com.wdkl.ncs.android.middleware.common.MessageEvent;
import com.wdkl.ncs.android.middleware.model.vo.InteractionVO;
import com.wdkl.ncs.android.middleware.tcp.dto.TcpModel;
import com.wdkl.ncs.android.middleware.tcp.enums.TcpAction;

import org.greenrobot.eventbus.EventBus;

import static com.wdkl.ncs.android.middleware.common.Constant.EVENT_CALL_MSG;
import static com.wdkl.ncs.android.middleware.common.Constant.EVENT_TCP_MSG;

/**
 * 前提：所有安卓设备可以互相通话。
 * 在此前提下，所有安卓设备接收TCP信号的逻辑一致
 */
public class DeviceChannel {

    public static TcpModel handleTcpReceived(TcpModel tcpModel){
        TcpModel responseTcpModel = null;

        Log.d("tcpModle", "received tcp model: " + tcpModel.toJson() + ", call state: " + Constant.CALL_STATE);
        switch (tcpModel.getType()){
            case CALLBACK:
                if(tcpModel.getAction()== TcpAction.CallbackAction.SUCCESS){
                    //todo: 刷新呼叫记录列表
                    EventBus.getDefault().post(new MessageEvent(tcpModel, EVENT_TCP_MSG));
                }else if(tcpModel.getAction()== TcpAction.CallbackAction.FAILED){
                    EventBus.getDefault().post(new MessageEvent(tcpModel, EVENT_TCP_MSG));
                }
                break;
            case VOICE:
                if (tcpModel.getAction()==TcpAction.VoiceAction.CALL){ //语音呼入
                    //todo: 判断当前是否通话
                    // 当前正在通话中或有新的来电或正在呼叫,直接返回该状态给服务器
                    if (Constant.CALL_STATE == Constant.CALL_INCOMING || Constant.CALL_STATE == Constant.CALL_CALLING
                            || Constant.CALL_STATE == Constant.CALL_OUTGOING){
                        InteractionVO interactionVO = new Gson().fromJson(tcpModel.getData().toString(), InteractionVO.class);
                        responseTcpModel = VoiceUtil.voiceCalling(Constant.DEVICE_ID, tcpModel.getFromId(), interactionVO.getId());
                        return responseTcpModel;
                    } else  {
                        //todo: 通话界面展现，data中服务器传过来呼入名称；从接口重新获取左侧数据
                        // 当前待机状态，返回呼叫成功，并切换到呼叫界面
                        Constant.CALL_STATE = Constant.CALL_INCOMING;
                        EventBus.getDefault().post(new MessageEvent(tcpModel, EVENT_TCP_MSG));
                    }
                } else {
                    //tcpModel.getAction()==TcpAction.VoiceAction.ACCEPT
                    //todo: 通话中界面更新；建立数据通话
                    //tcpModel.getAction()==TcpAction.VoiceAction.REJECT
                    //todo: 清掉呼出的TcpModel，通话中界面更新 --- 显示对方拒绝并停留3秒，结束至正常界面；更新左侧
                    //tcpModel.getAction()==TcpAction.VoiceAction.CALLING
                    //todo: 清掉呼出的TcpModel，通话中界面更新 --- 显示对方占线并停留3秒，结束至正常界面；更新左侧
                    //tcpModel.getAction()==TcpAction.VoiceAction.FAILED
                    //todo: 清掉呼出的TcpModel，通话中界面更新 --- 显示对方离线并停留3秒，结束至正常界面；更新左侧
                    //tcpModel.getAction()==TcpAction.VoiceAction.HANDOFF
                    //todo: 清掉呼出的TcpModel，通话中界面更新 --- 显示对方已挂断并停留3秒，结束至正常界面；更新左侧

                    //交由后续处理
                    EventBus.getDefault().post(new MessageEvent(tcpModel, EVENT_TCP_MSG));
                }
                break;
            case VIDEO:
                EventBus.getDefault().post(new MessageEvent(tcpModel, EVENT_TCP_MSG));
                break;
            case IM:
                if (tcpModel.getAction()==TcpAction.IMAction.MSG){
                    //todo: 从接口重新获取左侧数据；使用通话中界面，显示有语音留言，停留3秒，结束至正常界面
                } else if (tcpModel.getAction()==TcpAction.IMAction.MSG_READ){ //语音已读取
                    //todo: 从data得到语音id；更新左侧，注意这里不使用通话中界面提示已读
                }
                break;
            case EVENT:
                if (tcpModel.getAction()==TcpAction.EventAction.KEY_CLICK){ //有新事件过来
                    //todo: 服务端通过data给出具体的事件名称放置data。从接口重新获取左侧数据；使用通话中界面，显示事件名称，停留3秒，结束至正常界面
                } else if (tcpModel.getAction()==TcpAction.EventAction.RESPONSE){ //事件已处理，仅分机用
                    //todo: 从data得到事件id，使用通话中界面，显示事件名称及已响应字样，停留3秒，结束至正常界面；更新左侧
                    EventBus.getDefault().post(new MessageEvent(tcpModel, EVENT_TCP_MSG));
                }
                break;
            case DATA:
                if (tcpModel.getAction()==TcpAction.DataAction.REFRESH){
                    //todo: 通过data判断刷新何种数据
                    EventBus.getDefault().post(new MessageEvent(tcpModel,EVENT_TCP_MSG));
                } else if (tcpModel.getAction()==TcpAction.DataAction.INTERACTION) {
                    EventBus.getDefault().post(new MessageEvent(tcpModel, EVENT_TCP_MSG));
                }
                break;
            case DEVICE:
            case TIME:
            case BROADCAST:
            case SOS:
            case SIDE:
            case ENTRACEGUARD:
                EventBus.getDefault().post(new MessageEvent(tcpModel, EVENT_TCP_MSG));
                break;
        }

        return responseTcpModel;
    }
}
