package com.engine.salary.report.util;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class ReportTimeUtil {
    private static final DateTimeFormatter YEAR_MONTH = DateTimeFormatter.ofPattern("yyyy-MM");

    public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static Map<String, String> getLastRange(String startMonth, String endMonth) {
        LocalDate start = LocalDate.parse(startMonth + "-01");
        LocalDate end = LocalDate.parse(endMonth + "-01");
        long between = ChronoUnit.MONTHS.between(start, end);
        LocalDate lastEndMonth = start.plusMonths(-1);
        LocalDate lastStartMonth = lastEndMonth.plusMonths(-between);
        Map<String, String> result = new HashMap<>();
        result.put("startMonth", YEAR_MONTH.format(lastStartMonth));
        result.put("endMonth", YEAR_MONTH.format(lastEndMonth));
        return result;
    }

    public static String getQuarter(String billMonth) {
        if (StringUtils.isBlank(billMonth)) {
            return "";
        }
        String[] split = billMonth.split("-");
        String year = split[0] + "年";
        String quarter = "";
        int month = Integer.parseInt(split[1]);
        if (1 <= month && month <= 3) {
            quarter = "1季度";
        }
        if (4 <= month && month <= 6) {
            quarter = "2季度";
        }
        if (7 <= month && month <= 9) {
            quarter = "3季度";
        }
        if (10 <= month && month <= 12) {
            quarter = "4季度";
        }
        return year + quarter;
    }

    public static String getHalfYear(String salaryMonth) {
        if (StringUtils.isBlank(salaryMonth)) {
            return "";
        }
        String[] split = salaryMonth.split("-");
        String year = split[0] + "年-";
        String halfYear = "";
        int month = Integer.parseInt(split[1]);
        if (1 <= month && month <= 6) {
            halfYear = "上半年";
        }
        if (7 <= month && month <= 12) {
            halfYear = "下半年";
        }
        return year + halfYear;
    }

    public static String getYear(String billMonth) {
        if (StringUtils.isEmpty(billMonth)) {
            return "";
        }
        String[] split = billMonth.split("-");
        return split[0] + "年";
    }

    public static String getFormatYearMonth(LocalDate localDate) {
        if (localDate == null) {
            return StringUtils.EMPTY;
        }
        try {
            return localDate.format(YEAR_MONTH);
        } catch (Exception e) {
            log.warn("格式化月份错误", e);
            return StringUtils.EMPTY;
        }
    }

    public static String getLastYear(String year) {
        if (StringUtils.isEmpty(year)) {
            return "";
        }
        return (Integer.parseInt(year.replace("年", "")) - 1) + "年";
    }

    public static String getLastYearMonth(String yearMonth) {
        return YEAR_MONTH.format(LocalDate.parse(yearMonth + "-01").plusMonths(-1));
    }

    public static String getSameYearMonth(String yearMonth) {
        return YEAR_MONTH.format(LocalDate.parse(yearMonth + "-01").plusYears(-1));
    }

    public static Date getLastYearMonth(Date yearMonth) {
        return DateUtil.offsetMonth(yearMonth,-1);
    }

    public static Date getSameYearMonth(Date yearMonth) {
        return DateUtil.offset(yearMonth, DateField.YEAR,-1);
    }

    public static String getPlusYearMonth(String yearMonth, long months) {
        return YEAR_MONTH.format(LocalDate.parse(yearMonth + "-01").plusMonths(months));
    }

    public static String getLastQuarter(String quarter) {
        if (StringUtils.isEmpty(quarter)) {
            return "";
        }
        String[] ym = quarter.split("年");
        String year = ym[0];
        String month = ym[1].split("季度")[0];
        if ("1".equals(month)) {
            year = (Integer.parseInt(year) - 1) + "";
            return year + "年" + "4季度";
        }
        return year + "年" + (Integer.parseInt(month) - 1) + "季度";
    }

    public static String[] getYearMonthRange(String startMonth, String endMonth) {
        LocalDate start = LocalDate.parse(startMonth + "-01");
        LocalDate end = LocalDate.parse(endMonth + "-01");
        long between = ChronoUnit.MONTHS.between(start, end);
        String[] result = new String[(int) between + 1];
        result[0] = startMonth;
        for (int i = 1; i < between + 1; i++) {
            start = start.plusMonths(1);
            if (start.isBefore(end) || start.isEqual(end)) {
                result[i] = YEAR_MONTH.format(start);
            }
        }
        return result;
    }

    public static String getFormatLocalDateTime(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return StringUtils.EMPTY;
        }
        try {
            return localDateTime.format(DATE_TIME_FORMATTER);
        } catch (Exception e) {
            log.warn("格式化日期错误", e);
            return StringUtils.EMPTY;
        }
    }

    public static LocalDate dateToLocalDate(Date date) {
        if (date == null) {
            return null;
        }
        Instant instant = date.toInstant();
        ZonedDateTime zone = instant.atZone(ZoneId.systemDefault());
        return zone.toLocalDate();
    }
}
