package com.purui.prims.common.pojo.enums;

import com.purui.prims.common.pojo.Constants;
import org.springframework.util.CollectionUtils;

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


public enum TimeDimensionEnum {

    DAY("pr_biz_date", "数据日期"),

    WEEK("pr_biz_week", "数据日期_周"),

    MONTH("pr_biz_month", "数据日期_月"),

    YEAR("pr_biz_year", "数据日期_年"),

    HOUR("pr_biz_hour", "数据日期_小时");

    private String name;

    private String chName;

    TimeDimensionEnum(String name, String chName) {
        this.name = name;
        this.chName = chName;
    }

    public static boolean containsTimeDimension(String fieldName) {
        if (getNameList().contains(fieldName) || getChNameList().contains(fieldName)) {
            return true;
        }
        return false;
    }

    public static List<String> getNameList() {
        return Arrays.stream(TimeDimensionEnum.values()).map(TimeDimensionEnum::getName).collect(Collectors.toList());
    }

    public static List<String> getChNameList() {
        return Arrays.stream(TimeDimensionEnum.values()).map(TimeDimensionEnum::getChName).collect(Collectors.toList());
    }

    public static Map<String, String> getChNameToNameMap() {
        return Arrays.stream(TimeDimensionEnum.values())
                .collect(Collectors.toMap(TimeDimensionEnum::getChName, TimeDimensionEnum::getName, (k1, k2) -> k1));
    }

    public static Map<String, String> getNameToChNameMap() {
        return Arrays.stream(TimeDimensionEnum.values())
                     .collect(Collectors.toMap(TimeDimensionEnum::getName, TimeDimensionEnum::getChName, (k1, k2) -> k1));
    }

    public static Map<String, String> getNameToNameMap() {
        return Arrays.stream(TimeDimensionEnum.values())
                .collect(Collectors.toMap(TimeDimensionEnum::getName, TimeDimensionEnum::getName, (k1, k2) -> k1));
    }

    public String getName() {
        return name;
    }

    public String getChName() {
        return chName;
    }

    /**
     * Determine if a time dimension field is included in a Chinese/English text field
     *
     * @param fields field
     * @return true/false
     */
    public static boolean containsZhTimeDimension(List<String> fields) {
        if (CollectionUtils.isEmpty(fields)) {
            return false;
        }
        return fields.stream().anyMatch(field -> containsTimeDimension(field));
    }

    public static TimeDimensionEnum parseTimeDimension(String format) {
        TimeDimensionEnum timeType = TimeDimensionEnum.DAY;
        switch (format) {
            case Constants.HOUR_FORMAT:
                timeType = TimeDimensionEnum.HOUR;
                break;
            case Constants.MONTH_FORMAT:
                timeType = TimeDimensionEnum.MONTH;
                break;
            case Constants.YEAR_FORMAT:
                timeType = TimeDimensionEnum.YEAR;
                break;
            default:
        }
        return timeType;
    }
}
