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_TCP_MSG;

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

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

        Log.d("tcpModel", "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:
            case VIDEO:
            case DEVICE:
            case BROADCAST:
            case TIME:
            case SOS:
            case SIDE:
                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;
        }

        return responseTcpModel;
    }
}
