package com.wdkl.ncs.s433.transbox.tcp;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;

/**
 * tcp传输对象
 *
 * @author allen
 * 2021-03-30 11:49
 */
public class TcpModel implements Serializable {

    private String tid;

    /**
     * TCP传输对象的类型
     */
    private TcpType type;
    /**
     * TCP传输对象类型的动作
     */
    private TcpAction action;

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

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

    public TcpModel() {
    }

    public TcpModel(String tid) {
        this.tid = tid;
    }

    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 (TextUtils.isEmpty(tcpModelJsonString)){
            return null;
        }
        TcpModel tcpModel = new TcpModel();

        JSONObject jsonObject = JSON.parseObject(tcpModelJsonString);
        String tid = jsonObject.getString("tid");
        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 ENTRACEGUARD:
                tcpAction =TcpAction.EntraceGuardAction.fromString(action);
                break;
            case REINFORCE:
                tcpAction =TcpAction.ReinforceAction.fromString(action);
                break;
            case S433:
                tcpAction = TcpAction.S433Action.fromString(action);
                break;
        }
        tcpModel.setTid(tid);
        tcpModel.setType(tcpType);
        tcpModel.setAction(tcpAction);
        tcpModel.setToId(toId);
        tcpModel.setFromId(fromId);
        tcpModel.setData(dataString);

        return tcpModel;
    }

    public String getTid() {
        return tid;
    }

    public void setTid(String tid) {
        this.tid = tid;
    }
}
