package com.eactive.testmaster.client.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
public class ApiLayoutItemDTO implements Serializable {

    private static final long serialVersionUID = 1L;

    public ApiLayoutItemDTO(Long id, Long parent, String loutItemType, String loutName, Integer loutItemDepth, Integer loutItemLength, String value) {
        this.id = id;
        this.loutItemSerno = id.intValue();
        this.parent = parent;
        this.loutItemType = loutItemType;
        this.loutName = loutName;
        this.loutItemDepth = loutItemDepth;
        this.level = loutItemDepth;
        this.loutItemLength = loutItemLength;
        this.value = value;
    }

    @JsonProperty("itemId")
    private Long itemId;

    @JsonProperty("id")
    private Long id;

    @JsonProperty("parent")
    private Long parent;

    @JsonProperty("level")
    private Integer level;

    @JsonProperty("loutName")
    private String loutName;

    @JsonProperty("loutItemSerno")
    private Integer loutItemSerno;

    @JsonProperty("loutItemName")
    private String loutItemName;

    @JsonProperty("loutItemDesc")
    private String loutItemDesc;

    @JsonProperty("loutItemDepth")
    private Integer loutItemDepth;

    @JsonProperty("loutItemType")
    private String loutItemType;

    @JsonProperty("loutItemOccCnt")
    private String loutItemOccCnt;

    @JsonProperty("loutItemOccRef")
    private String loutItemOccRef;

    @JsonProperty("loutItemDataType")
    private String loutItemDataType;

    @JsonProperty("loutItemLength")
    private Integer loutItemLength;

    @JsonProperty("loutItemDecimal")
    private Integer loutItemDecimal;

    @JsonProperty("loutItemDefault")
    private String loutItemDefault;

    @JsonProperty("loutItemMaskYn")
    private String loutItemMaskYn;

    @JsonProperty("loutItemMaskOffset")
    private Integer loutItemMaskOffset;

    @JsonProperty("loutItemMaskLength")
    private Integer loutItemMaskLength;

    @JsonProperty("parentLoutItemIndex")
    private Integer parentLoutItemIndex;

    @JsonProperty("expanded")
    private Boolean expanded;

    @JsonProperty("isLeaf")
    private Boolean isLeaf;

    @JsonProperty("LOUTITEMPATH")
    private String loutItemPath;

    @JsonProperty(value = "value", defaultValue = "")
    private String value;

    private List<ApiLayoutItemDTO> children = new ArrayList<>();

    private int childOccCnt;

    public ApiLayoutItemDTO() {
    }


    public Long getItemId() {
        return itemId;
    }

    public void setItemId(Long itemId) {
        this.itemId = itemId;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getParent() {
        return parent;
    }

    public void setParent(Long parent) {
        this.parent = parent;
    }

    public Integer getLevel() {
        return level;
    }

    public void setLevel(Integer level) {
        this.level = level;
    }

    public String getLoutName() {
        return loutName;
    }

    public void setLoutName(String loutName) {
        this.loutName = loutName;
    }

    public Integer getLoutItemSerno() {
        return loutItemSerno;
    }

    public void setLoutItemSerno(Integer loutItemSerno) {
        this.loutItemSerno = loutItemSerno;
    }

    public String getLoutItemName() {
        return loutItemName;
    }

    public void setLoutItemName(String loutItemName) {
        this.loutItemName = loutItemName;
    }

    public String getLoutItemDesc() {
        return loutItemDesc;
    }

    public void setLoutItemDesc(String loutItemDesc) {
        this.loutItemDesc = loutItemDesc;
    }

    public Integer getLoutItemDepth() {
        return loutItemDepth;
    }

    public void setLoutItemDepth(Integer loutItemDepth) {
        this.loutItemDepth = loutItemDepth;
    }

    public String getLoutItemType() {
        return loutItemType;
    }

    public void setLoutItemType(String loutItemType) {
        this.loutItemType = loutItemType;
    }

    public String getLoutItemOccCnt() {
        return loutItemOccCnt;
    }

    public void setLoutItemOccCnt(String loutItemOccCnt) {
        this.loutItemOccCnt = loutItemOccCnt;
    }

    public String getLoutItemOccRef() {
        return loutItemOccRef;
    }

    public void setLoutItemOccRef(String loutItemOccRef) {
        this.loutItemOccRef = loutItemOccRef;
    }

    public String getLoutItemDataType() {
        return loutItemDataType;
    }

    public void setLoutItemDataType(String loutItemDataType) {
        this.loutItemDataType = loutItemDataType;
    }

    public Integer getLoutItemLength() {
        return loutItemLength;
    }

    public void setLoutItemLength(Integer loutItemLength) {
        this.loutItemLength = loutItemLength;
    }

    public Integer getLoutItemDecimal() {
        return loutItemDecimal;
    }

    public void setLoutItemDecimal(Integer loutItemDecimal) {
        this.loutItemDecimal = loutItemDecimal;
    }

    public String getLoutItemDefault() {
        return loutItemDefault;
    }

    public void setLoutItemDefault(String loutItemDefault) {
        this.loutItemDefault = loutItemDefault;
    }

    public String getLoutItemMaskYn() {
        return loutItemMaskYn;
    }

    public void setLoutItemMaskYn(String loutItemMaskYn) {
        this.loutItemMaskYn = loutItemMaskYn;
    }

    public Integer getLoutItemMaskOffset() {
        return loutItemMaskOffset;
    }

    public void setLoutItemMaskOffset(Integer loutItemMaskOffset) {
        this.loutItemMaskOffset = loutItemMaskOffset;
    }

    public Integer getLoutItemMaskLength() {
        return loutItemMaskLength;
    }

    public void setLoutItemMaskLength(Integer loutItemMaskLength) {
        this.loutItemMaskLength = loutItemMaskLength;
    }

    public Integer getParentLoutItemIndex() {
        return parentLoutItemIndex;
    }

    public void setParentLoutItemIndex(Integer parentLoutItemIndex) {
        this.parentLoutItemIndex = parentLoutItemIndex;
    }

    public Boolean getExpanded() {
        return expanded;
    }

    public void setExpanded(Boolean expanded) {
        this.expanded = expanded;
    }

    public Boolean getLeaf() {
        return isLeaf;
    }

    public void setLeaf(Boolean leaf) {
        isLeaf = leaf;
    }

    public String getLoutItemPath() {
        return loutItemPath;
    }

    public void setLoutItemPath(String loutItemPath) {
        this.loutItemPath = loutItemPath;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

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

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

    public int getChildOccCnt() {
        return childOccCnt;
    }

    public void setChildOccCnt(int childOccCnt) {
        this.childOccCnt = childOccCnt;
    }

    @JsonIgnore
    public String fillPadding(int dataLength, String value) {
        StringBuilder sb = new StringBuilder(value);
        while (sb.length() < dataLength) {
            sb.insert(0, ' '); // Adds space at the beginning of the string
        }
        return sb.toString();
    }

    @JsonIgnore
    public String buildMessage() {
        if (this.getLoutItemType().equalsIgnoreCase("Field") || this.getLoutItemType().equalsIgnoreCase("Attr")) {
            return fillPadding(this.getLoutItemLength(), this.getValue());
        } else if (this.getLoutItemType().equalsIgnoreCase("Group") || this.getLoutItemType().equalsIgnoreCase("Grid")) {
            if (this.getChildren() == null || this.getChildren().isEmpty()) {
                return "";
            }

            StringBuilder sb = new StringBuilder();
//            sb.append("[");
            for (int i = 0; i < this.getChildOccCnt(); i++) {
                for (ApiLayoutItemDTO child : this.getChildren()) {
                    sb.append(child.buildMessage());
                }
            }
//            sb.append("]");
            return sb.toString();
        } else {
            return "";
        }

    }
}
