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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import com.wdkl.ncs.android.middleware.model.dos.DeviceDO;
import com.wdkl.ncs.android.middleware.model.dos.FrameDO;
import com.wdkl.ncs.android.middleware.model.vo.InteractionVO;
import com.wdkl.ncs.android.middleware.tcp.enums.TcpAction;
import com.wdkl.ncs.android.middleware.tcp.enums.TcpType;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * tcp传输对象
 *
 * @author allen
 * 2021-03-30 11:49
 */
public class TcpModel implements Serializable {
    /**
     * TCP传输对象的类型
     */
    private TcpType type;
    /**
     * TCP传输对象类型的动作
     */
    private TcpAction action;

    /**
     * 来源设备id
     */
    private Integer fromId;
    /**
     * 目标设备id，呼叫分机或其它设备时需要。分机呼叫不需要
     */
    private Integer toId;

    /**
     * TCP传输对象的数据
     */
    private Object data;

    public TcpType getType() {
        return type;
    }

    public void setType(TcpType type) {
        this.type = type;
    }

    public TcpAction getAction() {
        return action;
    }

    public void setAction(TcpAction action) {
        this.action = action;
    }

    public Integer getFromId() {
        return fromId;
    }

    public void setFromId(Integer fromId) {
        this.fromId = fromId;
    }

    public Integer getToId() {
        return toId;
    }

    public void setToId(Integer toId) {
        this.toId = toId;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String toJson(){
        return JSON.toJSONString(this);
    }

    public static TcpModel getModelByJson(String tcpModelJsonString){
        if (Strings.isNullOrEmpty(tcpModelJsonString)){
            return null;
        }
        TcpModel tcpModel = new TcpModel();

        JSONObject jsonObject = JSON.parseObject(tcpModelJsonString);
        String type = jsonObject.getString("type");
        String action = jsonObject.getString("action");
        Integer fromId = jsonObject.getInteger("fromId");
        Integer toId = jsonObject.getInteger("toId");
        String dataString = jsonObject.getString("data");

        TcpType tcpType = TcpType.fromString(type);
        TcpAction tcpAction = null;
        switch (tcpType){
            case CALLBACK:
                tcpAction = TcpAction.CallbackAction.fromString(action);
                break;
            case VOICE:
                tcpAction = TcpAction.VoiceAction.fromString(action);
                break;
            case VIDEO:
                tcpAction = TcpAction.VideoAction.fromString(action);
                break;
            case IM:
                tcpAction = TcpAction.IMAction.fromString(action);
                break;
            case BROADCAST:
                tcpAction = TcpAction.BroadcastAction.fromString(action);
                break;
            case DATA:
                tcpAction = TcpAction.DataAction.fromString(action);
                break;
            case DEVICE:
                tcpAction = TcpAction.DeviceAction.fromString(action);
                break;
            case TIME:
                tcpAction = TcpAction.TimeAction.fromString(action);
                break;
            case EVENT:
                tcpAction = TcpAction.EventAction.fromString(action);
                break;
            case SOS:
                tcpAction = TcpAction.SOSAction.fromString(action);
                break;
            case SIDE:
                tcpAction = TcpAction.SideAction.fromString(action);
                break;
            case ENTRACEGUARD:
                tcpAction =TcpAction.EntraceGuardAction.fromString(action);
                break;
        }

        tcpModel.setType(tcpType);
        tcpModel.setAction(tcpAction);
        tcpModel.setToId(toId);
        tcpModel.setFromId(fromId);
        tcpModel.setData(dataString);

        return tcpModel;
    }

    public static String callbackSuccess(){
        TcpModel tcpModel = new TcpModel();
        tcpModel.setType(TcpType.CALLBACK);
        tcpModel.setAction(TcpAction.CallbackAction.SUCCESS);
        return tcpModel.toJson();
    }

    public static String callbackFailed(){
        TcpModel tcpModel = new TcpModel();
        tcpModel.setType(TcpType.CALLBACK);
        tcpModel.setAction(TcpAction.CallbackAction.FAILED);
        return tcpModel.toJson();
    }

    public static String refresh() {
        TcpModel tcpModel = new TcpModel();
        tcpModel.setType(TcpType.DATA);
        tcpModel.setAction(TcpAction.DataAction.REFRESH);
        return tcpModel.toJson();
    }


    public static String interaction() {
        TcpModel tcpModel = new TcpModel();
        tcpModel.setType(TcpType.DATA);
        tcpModel.setAction(TcpAction.DataAction.INTERACTION);
        return tcpModel.toJson();
    }

    /**
     * 示例
     * @param args
     */
    public static void main(String[] args) {
        //创建对象示例
        TcpModel tcpModel = new TcpModel();
        tcpModel.type = TcpType.VOICE;
        tcpModel.action = TcpAction.VoiceAction.CALL;

        Map map = new HashMap();

        DeviceDO deviceDO = new DeviceDO();
        deviceDO.setCode("123");
        deviceDO.setEthMac("abcd");
        map.put("device",deviceDO);

        FrameDO frameDO = new FrameDO();
        frameDO.setName("ok");
        map.put("frame",frameDO);

        tcpModel.setData(map);

        System.out.println("enum show === " + tcpModel.action.getDescription() + " -- " + tcpModel.action.getName());

        //要传输的json字符串
        String jsonStr = tcpModel.toJson();
        System.out.println("json show === " + jsonStr);

        //获利传输的JSON字符串，转换为对象
        TcpModel dTcpModel = TcpModel.getModelByJson(jsonStr);
        //数据反转
        String dataString = dTcpModel.getData().toString();
        JSONObject dataJSON = JSON.parseObject(dataString);
        DeviceDO dDeviceDO = JSON.parseObject(dataJSON.getString("device"), DeviceDO.class);

        System.out.println("json de show === " + dTcpModel.action + " -- " + dDeviceDO.getEthMac());
    }
}
