package com.purui.prims.headless.api.pojo.response;


import com.google.common.collect.Lists;
import com.purui.prims.common.pojo.DataFormat;
import com.purui.prims.common.pojo.enums.DataFormatTypeEnum;
import com.purui.prims.headless.api.pojo.DrillDownDimension;
import com.purui.prims.headless.api.pojo.MetricDefineByFieldParams;
import com.purui.prims.headless.api.pojo.MetricDefineByMeasureParams;
import com.purui.prims.headless.api.pojo.MetricDefineByMetricParams;
import com.purui.prims.headless.api.pojo.RelateDimension;
import com.purui.prims.headless.api.pojo.SchemaItem;
import com.purui.prims.headless.api.pojo.enums.MetricDefineType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Data
@ToString(callSuper = true)
@ApiModel(value = "指标")
public class MetricResp extends SchemaItem {

    @ApiModelProperty(value = "模型ID")
    private Long modelId;

    @ApiModelProperty(value = "主题域ID")
    private Long domainId;

    @ApiModelProperty(value = "主题域名称")
    private String domainName;

    @ApiModelProperty(value = "模型业务名称")
    private String modelBizName;

    @ApiModelProperty(value = "模型名称")
    private String modelName;

    // 最新版本号
    @ApiModelProperty(value = "最新版本号")
    private String lastVersionNo;

    //ATOMIC DERIVED
    @ApiModelProperty(value = "类型")
    private String type;

    @ApiModelProperty(value = "时间维度ID")
    private Long dateDimId;

    @ApiModelProperty(value = "数据格式")
    private DataFormatTypeEnum dataFormatType;

    @ApiModelProperty(value = "日期式")
    private DataFormat dataFormat;

    @ApiModelProperty(value = "别名")
    private String alias;

    @ApiModelProperty(value = "分类")
    private List<String> classifications;

    @ApiModelProperty(value = "关联维度")
    private RelateDimension relateDimension;

    private boolean hasAdminRes = false;

    @ApiModelProperty(value = "是否收藏")
    private Boolean isCollect;

    @ApiModelProperty(value = "扩展信息")
    private Map<String, Object> ext = new HashMap<>();

    @ApiModelProperty(value = "指标定义类型")
    private MetricDefineType metricDefineType = MetricDefineType.MEASURE;

    @ApiModelProperty(value = "度量定义参数")
    private MetricDefineByMeasureParams metricDefineByMeasureParams;

    @ApiModelProperty(value = "字段定义参数")
    private MetricDefineByFieldParams metricDefineByFieldParams;

    @ApiModelProperty(value = "指标定义参数")
    private MetricDefineByMetricParams metricDefineByMetricParams;

    @ApiModelProperty(value = "是否发布")
    private Integer isPublish;

    @ApiModelProperty(value = "相似度")
    private double similarity;

    @ApiModelProperty(value = "默认聚合")
    private String defaultAgg;

    @ApiModelProperty(value = "统计周期")
    private String statisticsPeriod;

    private Integer periodId;

    @ApiModelProperty(value = "算子")
    private String operations;

    @ApiModelProperty(value = "业务限定")
    private BizConditionResp bizConditionResp;

    @ApiModelProperty(value = "时间维度")
    private DimensionResp dateDimensionResp;

    @ApiModelProperty(value = "负责人")
    private String director;

    public void setClassifications(String tag) {
        if (StringUtils.isBlank(tag)) {
            classifications = Lists.newArrayList();
        } else {
            classifications = Arrays.asList(tag.split(","));
        }
    }

    public String getRelaDimensionIdKey() {
        if (relateDimension == null || CollectionUtils.isEmpty(relateDimension.getDrillDownDimensions())) {
            return "";
        }
        return relateDimension.getDrillDownDimensions().stream()
                .map(DrillDownDimension::getDimensionId)
                .map(String::valueOf)
                .collect(Collectors.joining(","));
    }

    public List<DrillDownDimension> getDrillDownDimensions() {
        if (relateDimension == null || CollectionUtils.isEmpty(relateDimension.getDrillDownDimensions())) {
            return Lists.newArrayList();
        }
        return relateDimension.getDrillDownDimensions();
    }

    public String getExpr() {
        if (MetricDefineType.MEASURE.equals(metricDefineType)) {
            return metricDefineByMeasureParams.getExpr();
        } else if (MetricDefineType.METRIC.equals(metricDefineType)) {
            return metricDefineByMetricParams.getExpr();
        } else if (MetricDefineType.CUSTOM.equals(metricDefineType)) {
            return metricDefineByFieldParams.getExpr();
        }
        return "";
    }

}
