package cn.cecep.talroad.enums;

import com.fasterxml.jackson.annotation.JsonCreator;

import java.time.LocalDateTime;
import java.util.Calendar;

public enum EnStatTimeType {
    REAL("R", "实时", "R", "yyyy-MM-dd HH:mm:ss"),
    MINUTE("M", "分钟", "R", "yyyy-MM-dd HH:mm"),
    HOUR("H", "小时", "R", "yyyy-MM-dd HH:00"),
    DAY("D", "日", "H", "yyyy-MM-dd"),
    WEEK("W", "周", "D", "yyyy'W'ww"),
    MONTH("N", "月", "D", "yyyy-MM"),
    QUARTER("Q", "季", "D", "yyyy'Q'q"),
    YEAR("Y", "年", "D", "yyyy");

    private String enumId;
    private String enumTxt;
    private String sonTimeType;
    private String timeFormat;

    EnStatTimeType(String id, String txt, String sonTime, String format) {
        this.enumId = id;
        this.enumTxt = txt;
        this.sonTimeType = sonTime;
        this.timeFormat = format;
    }

    public String id() {
        return this.enumId;
    }


    public LocalDateTime plus(LocalDateTime time, long num) {
        if (this == REAL) {
            return time.plusMinutes(num);
        } else if (this == MINUTE) {
            return time.plusMinutes(num);
        } else if (this == HOUR) {
            return time.plusHours(num);
        } else if (this == DAY) {
            return time.plusDays(num);
        } else if (this == WEEK) {
            return time.plusWeeks(num);
        } else if (this == MONTH) {
            return time.plusMonths(num);
        } else if (this == QUARTER) {
            return time.plusMonths(num * 3L);
        } else {
            return this == YEAR ? time.plusYears(num) : null;
        }
    }

    public Calendar plus(Calendar time, int num) {
        if (this == REAL) {
            time.add(12, num);
        } else if (this == MINUTE) {
            time.add(12, num);
        } else if (this == HOUR) {
            time.add(11, num);
        } else if (this == DAY) {
            time.add(5, num);
        } else if (this == WEEK) {
            time.add(3, num);
        } else if (this == MONTH) {
            time.add(2, num);
        } else if (this == QUARTER) {
            time.add(2, num * 3);
        } else {
            if (this != YEAR) {
                return null;
            }

            time.add(1, num);
        }

        return time;
    }

    @JsonCreator
    public static EnStatTimeType getEnumById(String id) {
        EnStatTimeType[] var1 = values();
        int var2 = var1.length;

        for(int var3 = 0; var3 < var2; ++var3) {
            EnStatTimeType en = var1[var3];
            if (en.id().equalsIgnoreCase(id)) {
                return en;
            }
        }

        return null;
    }

    public static EnStatTimeType getEnumByName(String name) {
        EnStatTimeType[] var1 = values();
        int var2 = var1.length;

        for(int var3 = 0; var3 < var2; ++var3) {
            EnStatTimeType en = var1[var3];
            if (en.name().equalsIgnoreCase(name)) {
                return en;
            }
        }

        return null;
    }
}
