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


import com.purui.prims.headless.api.pojo.MeasureParam;
import com.purui.prims.headless.api.pojo.MetricDefineByMeasureParams;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Objects;

public enum MetricType {

    /**
     * 原子指标
     */
    ATOMIC,
    /**
     * 衍生/派生指标
     */
    DERIVED,
    /**
     * 复合指标
     */
    COMPOSITE;

    public static MetricType of(String src) {
        for (MetricType metricType : MetricType.values()) {
            if (Objects.nonNull(src) && src.equalsIgnoreCase(metricType.name())) {
                return metricType;
            }
        }
        return null;
    }

    public static Boolean isDerived(String src) {
        MetricType metricType = of(src);
        return Objects.nonNull(metricType) && metricType.equals(DERIVED);
    }

    public static Boolean isDerived(MetricDefineType metricDefineType, MetricDefineByMeasureParams typeParams) {
        if (MetricDefineType.METRIC.equals(metricDefineType)) {
            return true;
        }
        if (MetricDefineType.CUSTOM.equals(metricDefineType)) {
            return false;
        }
        if (MetricDefineType.MEASURE.equals(metricDefineType)) {
            List<MeasureParam> measures = typeParams.getMeasures();
            if (measures.size() > 1) {
                return true;
            }
            if (measures.size() == 1 && StringUtils.isNotEmpty(measures.get(0).getConstraint())) {
                return true;
            }
        }
        return false;
    }

    // 是否复合指标
    public static Boolean isComposite(MetricDefineType metricDefineType) {
        if (MetricDefineType.METRIC.equals(metricDefineType)) {
            return true;
        }
        return false;
    }
}
