package net.northking.cctp.element.internal.protocol.socket.cups8583;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import net.northking.cctp.element.internal.dto.ColumnDTO;
import net.northking.cctp.element.internal.protocol.socket.cups8583.internal.ValueObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * CupsRequestParam
 *
 * @author fengxiang.zhang
 * @since 1.0
 */
public class CupsRequestParam {

    /**
     * 启用头长度字段
     */
    private boolean enableHeaderLength;
    /**
     * 头标识和版本：1Byte，b8
     */
    private String headerFlagAndVersion;
    /**
     * 目的ID：11Byte，ans11 不足11位后补空格
     */
    private String destinationId;
    /**
     * 源ID：11Byte，ans11 不足11位后补空格
     */
    private String sourceId;
    /**
     * 用户信息：1Byte，b8
     */
    private String userInfo;
    /**
     * 报文类型：4位定长数字
     */
    private String messageType;

    /**
     * 字段域
     */
    private List<ColumnDTO> columnList;
    /**
     * 字段域
     */
    private Map<String, ValueObject> valueMap = new HashMap<>();

    public CupsRequestParam addField(String key, String value, boolean hex) {
        ValueObject valueObject = new ValueObject();
        valueObject.setKey(key);
        valueObject.setValue(value);
        valueObject.setHex(hex);
        this.valueMap.put(key, valueObject);
        return this;
    }

    public boolean isEnableHeaderLength() {
        return enableHeaderLength;
    }

    public String getHeaderFlagAndVersion() {
        return headerFlagAndVersion;
    }

    public String getDestinationId() {
        return destinationId;
    }

    public String getSourceId() {
        return sourceId;
    }

    public String getUserInfo() {
        return userInfo;
    }

    public String getMessageType() {
        return messageType;
    }

    public Map<String, ValueObject> getValueMap() {
        return valueMap;
    }

    @JSONField(deserialize = true)
    public void setFields(List<JSONObject> fields) {
        if (fields != null) {
            for (JSONObject f : fields) {
                ValueObject valueObject = f.toJavaObject(ValueObject.class);
                this.valueMap.put(valueObject.getKey(), valueObject);
            }
        }
    }

    public void setEnableHeaderLength(boolean enableHeaderLength) {
        this.enableHeaderLength = enableHeaderLength;
    }

    public void setHeaderFlagAndVersion(String headerFlagAndVersion) {
        this.headerFlagAndVersion = headerFlagAndVersion;
    }

    public void setDestinationId(String destinationId) {
        this.destinationId = destinationId;
    }

    public void setSourceId(String sourceId) {
        this.sourceId = sourceId;
    }

    public void setUserInfo(String userInfo) {
        this.userInfo = userInfo;
    }

    public void setMessageType(String messageType) {
        this.messageType = messageType;
    }

    public void setValueMap(Map<String, ValueObject> valueMap) {
        this.valueMap = valueMap;
    }

    public List<ColumnDTO> getColumnList() {
        return columnList;
    }

    public void setColumnList(List<ColumnDTO> columnList) {
        this.columnList = columnList;
    }
}
