package com.mvc.netty.dealconstant;


import com.mvc.entity.TPackLog;
import com.mvc.utils.DateUtil;
import com.mvc.utils.NumberUtil;
import com.mvc.utils.StringUtil;

import java.util.Date;

/**
 * 报文数据
 */
public enum PackageData {
    Central_Station_Code("中心站地址",2),
    Telemetry_Station_Code("遥测站地址",10),
    Password("密码",4),
    Msg_Fun_Code("功能码",2),
    Body_Length("报文上下行标识及长度",4),
    Serial_Number("流水号",4),
    Send_Time("发报时间",12),
    Telemetry_Type_Code("遥测站分类码",2),
    Data_Time("观测时间",10),
    Body_End_Pattern("报文结束符",2),
    Check_Num("验证码",4),
    ;

    private String desc;
    private int size;
    private String value;

    private PackageData(String desc, int size) {
        this.desc = desc;
        this.size = size;
    }

    /**
     * 返回除去数据后的报文
     * @param msg
     * @return
     */
    public String parse(String msg) {
        String result = msg;
        if (this == Body_End_Pattern) {
            this.value = result.substring(result.length() - size);
            result = result.substring(0, result.length() - size);
        } else if (this == Check_Num) {
            this.value = result.substring(result.length() - size);
            result = result.substring(0, result.length() - size);
        } else {
            this.value = result.substring(0, size);
            result = result.substring(size);
        }
        return result;
    }

    /**
     * 编制报文
     * @param msg
     * @return
     */
    public String encode(String msg, TPackLog tPackage, int... bodyLength) {
        String result = msg;
        String value = "";
        switch (this) {
            case Central_Station_Code:
                value = tPackage.getCentralCode();
                break;
            case Telemetry_Station_Code:
                value = tPackage.getStationCode();
                break;
            case Password:
                value = tPackage.getRptPwd();
                break;
            case Msg_Fun_Code:
                value = HandleMsgCode.get(tPackage.getFunCode()).getResponse();
                break;
            case Body_Length:
                int len = 0;
                if (bodyLength != null) {
                    len = bodyLength[0];
                }
                value = "1000" + StringUtil.leftPad(Integer.toBinaryString(len), 12, '0');
                value = NumberUtil.binaryStringToHexString(value);
                break;
            case Serial_Number:
//                value = RandomStringUtils.random(4);
                value = tPackage.getSerialNum();
                break;
            case Send_Time:
                value = DateUtil.formateDate(new Date(), "yyMMddHHmmss");
                break;

        }
        result += value;
        return result;
    }

    public String encode(String msg, String content, int... bodyLength) {
        String result = msg;
        String value = "";
        switch (this) {
            case Msg_Fun_Code:
                value = HandleMsgCode.get(content).getResponse();
                break;
            case Body_Length:
                int len = 0;
                if (bodyLength != null) {
                    len = bodyLength[0];
                }
                value = "1000" + StringUtil.leftPad(Integer.toBinaryString(len), 12, '0');
                value = NumberUtil.binaryStringToHexString(value);
                break;
            case Serial_Number:
                value = "0002";
                break;
            case Send_Time:
                value = DateUtil.formateDate(new Date(), "yyMMddHHmmss");
                break;
            default:
                value = content;
        }
        result += value;
        return result;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public String getValue() {
        return value;
    }
}
