package cn.ibizlab.core.extensions.dto;

import cn.ibizlab.core.analysis.domain.DAMetric;
import cn.ibizlab.util.dict.Catalog;
import cn.ibizlab.util.dict.CodeItem;
import cn.ibizlab.util.dict.Option;
import cn.ibizlab.util.helper.DataObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.util.*;

@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
@Setter
@NoArgsConstructor
@Accessors(chain = true)
public class FetchResult {


    // 表格展示类型：1.  default（维度数据以行的形式展示），2.  LIST_BOX（维度数据以列的形式展示）
    private String displayType = "default";

    private String title;

    private List<FetchItem> rows;

    private List<VMConfig> headers;

    public List<VMConfig> getHeaders() {
        if(headers==null)
        {
            headers = new ArrayList<>();
            headers.add(new VMConfig().setHeaderName("序号").setEntity("item").setWidth(50).setField("no").setPinned("left"));
            headers.add(new VMConfig().setHeaderName(getGroup()).setEntity("item").setWidth(250).setField("itemName").setPinned("left"));
            if(vmConfigs!=null)
                headers.addAll(vmConfigs);
        }
        return headers;
    }

    public FetchResult trans2ListBox()
    {
        FetchResult listBox=new FetchResult();

        List<VMConfig> newVms=new ArrayList<>();

        rows.forEach(row->{
            VMConfig config=new VMConfig();
            config.setHeaderName(row.getDimName()).setEntity("item").setField(row.getDimId()).setWidth(80);
            newVms.add(config);
        });

        headers = new ArrayList<>();
        headers.add(new VMConfig().setHeaderName("序号").setEntity("item").setField("no").setPinned("left"));

        List<FetchItem> newRows=new ArrayList<>();
        for(FetchItem item:rows)
        {

        }



        this.setVmConfigs(newVms);
        return listBox;
    }

    public int getListBoxHeaders(List<VMConfig> vmConfigs,Map<Integer,VMConfig> set,List<FetchItem> newRows,int layer)
    {
        int maxlayer=layer;
        if(!set.containsKey(layer)) {
            VMConfig header=new VMConfig().setLayer(layer).setPinned("left").setEntity("item").setField("item_" + layer).setHeaderName("栏目" + layer);
            set.put(layer,header);
        }
        for(VMConfig config:vmConfigs){

            if(!ObjectUtils.isEmpty(config.getChildren()))
            {
                int sublayer=getListBoxHeaders(config.getChildren(),set,newRows,layer+1);
                if(sublayer>maxlayer)
                    maxlayer=sublayer;
            }
            else if(!StringUtils.isEmpty(config.getField()))
            {
                FetchItem fetchItem=new FetchItem().setNo(newRows.size()+1).setMetricId(config.getField()).setMetricName(config.getHeaderName()).setDisplayType(this.getDisplayType());
                rows.forEach(row->{
                    fetchItem.setVal(row.getDimId(),row.getVal(config.getField()));
                });
                newRows.add(fetchItem);
            }
        }
        return maxlayer;
    }

    @JsonIgnore
    @JSONField(serialize = false)
    private List<VMConfig> vmConfigs;


    private String group;

    private Catalog dict;

    private String selectValue;

    @JsonIgnore
    @JSONField(serialize = false)
    private Option selectDimItem;

    @JsonFormat(pattern = "yyyy-MM-dd", locale = "zh", timezone = "GMT+8")
    @JSONField(name = "startTime", format = "yyyy-MM-dd")
    private Timestamp startTime;
    @JsonFormat(pattern = "yyyy-MM-dd", locale = "zh", timezone = "GMT+8")
    @JSONField(name = "endTime", format = "yyyy-MM-dd")
    private Timestamp endTime;

    private String snapId;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
    @JSONField(name = "snapTime", format = "yyyy-MM-dd HH:mm:ss")
    private Timestamp snapTime;

    public static FetchResult from(FetchParam fetchParam)
    {
        FetchResult fetchResult=new FetchResult();
        fetchResult.setDisplayType(fetchParam.getDisplayType());

        if(fetchParam.getVmConfigs()!=null)
        {
            fetchResult.setVmConfigs(fetchParam.getVmConfigs());
        }
        fetchResult.setGroup(StringUtils.isEmpty(fetchParam.getGroup())?"分项":fetchParam.getGroup());
        fetchResult.setTitle(fetchParam.getTitle()).setDict(fetchParam.getDict()).setSelectValue(fetchParam.getDimValue()).setStartTime(fetchParam.getStartTime()).setEndTime(fetchParam.getEndTime()).setSelectDimItem(fetchParam.getDimItem());

        return fetchResult;
    }

}
