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

import com.alibaba.fastjson.annotation.JSONField;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 中国银联银行卡交换系统域（字段）定义
 *
 * @author fengxiang.zhang
 * @since 1.0
 */
public class CupsField {

    /**
     * 技术字段域号顺序号
     */
    String key;
    /**
     * 域号，形如：2、3...128
     */
    String code;
    /**
     * 域中文名称
     */
    String chName;
    /**
     * 域类型
     */
    CupsFieldType fieldType;
    /**
     * 固定长度值或变长最大长度
     */
    int length;
    /**
     * 数据类型
     */
    CupsDataType dataType;
    /**
     * 子域
     */
    List<CupsField> children;
    /**
     * 父域KEY
     */
    String parentKey;

    /**
     * 是否使用扩展位图
     */
    @JSONField(serialize = false)
    public boolean isExtBitMapField() {
        int index = Integer.parseInt(StringUtils.substringBefore(this.key, "."));
        return index > 64;
    }

    /**
     * 字段域序号
     */
    @JSONField(serialize = false)
    public int getTopIndex() {
        return Integer.parseInt(StringUtils.substringBefore(this.key, "."));
    }

    public void addChildField(CupsField field) {
        if (this.children == null) {
            this.children = new ArrayList<>();
        }
        this.children.add(field);
    }

    public boolean hasChildrenField() {
        return this.children != null && this.children.size() > 0;
    }

    @JSONField
    public String getDataTypeDesc() {
        return this.dataType.getDesc();
    }

    @JSONField
    public String getFieldTypeDesc() {
        return this.fieldType.getDesc();
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getChName() {
        return chName;
    }

    public void setChName(String chName) {
        this.chName = chName;
    }

    public CupsFieldType getFieldType() {
        return fieldType;
    }

    public void setFieldType(CupsFieldType fieldType) {
        this.fieldType = fieldType;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public CupsDataType getDataType() {
        return dataType;
    }

    public void setDataType(CupsDataType dataType) {
        this.dataType = dataType;
    }

    public List<CupsField> getChildren() {
        return children;
    }

    public void setChildren(List<CupsField> children) {
        this.children = children;
    }

    public String getParentKey() {
        return parentKey;
    }

    public void setParentKey(String parentKey) {
        this.parentKey = parentKey;
    }
}
