package com.energy.manage.service.util;

import cn.hutool.core.date.DateUtil;
import org.springframework.util.StringUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Date日期工具
 */
public class DateUtils {


    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static String YYYY_MM_DD = "yyyy-MM-dd";
    public static ZoneOffset defultZoneOffset = ZoneOffset.of("+8");


    static DateFormat dateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);

    static DateFormat dateFormatYdm = new SimpleDateFormat(YYYY_MM_DD);


    public static LocalDateTime convertString2DataTime(String dateStr, String dateFormat) {
        if (StringUtils.isEmpty(dateStr) || "0000-00-00 00:00:00".equals(dateStr)) {
            return null;
        }
        if (StringUtils.isEmpty(dateFormat)) {
            dateFormat = "yyyy-MM-dd HH:mm:ss";
        }

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);
        return LocalDateTime.parse(dateStr, formatter);
    }

    /**
     * String 转 Date 固定格式
     *
     * @param dateStr
     * @param dateFormat 时间格式
     * @return
     */
    public static Date convertString2Date(String dateStr, String dateFormat) {
        if (StringUtils.isEmpty(dateStr) || "0000-00-00 00:00:00".equals(dateStr)) {
            return null;
        }
        if (StringUtils.isEmpty(dateFormat)) {
            dateFormat = "yyyy-MM-dd HH:mm";
        }
        ZoneId zoneId = ZoneId.systemDefault();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);
        LocalDateTime localDateTime = LocalDateTime.parse(dateStr, formatter);
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
        Date date = Date.from(zdt.toInstant());
        return date;
    }

    /**
     * String 转 Date   年月日时分
     *
     * @param dateStr
     * @return
     */
    public static Date convertString2DateYMDHM(String dateStr) {
        return convertString2Date(dateStr, "yyyy-MM-dd HH:mm");
    }

    /**
     * String 转 Date   年月日时分秒
     *
     * @param dateStr
     * @return
     */
    public static Date convertString2LocalDateTimeYMDHMS(String dateStr) {
        return convertString2Date(dateStr, "yyyy-MM-dd HH:mm:ss");
    }


    public static String convertLocalDateTime2StringYMD(LocalDateTime date) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return formatter.format(date);
    }


    public static String convertDate2StringYMD(Date date) {


        return dateFormatYdm.format(date);
    }

    public static String convertDate2StringYMDHMS(Date date) {


        return dateFormat.format(date);
    }

    public static Date convertStringLocalDateYMD(String dateStr) {

        ZoneId zoneId = ZoneId.systemDefault();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYY_MM_DD);
        LocalDate localDate = LocalDate.parse(dateStr, formatter);
        ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
        return Date.from(zdt.toInstant());
    }

    /**
     * 两个时间内的所有天数从大到小
     */
    public static List<LocalDate> getDescDateList(LocalDate startDate, LocalDate endDate) {
        List<LocalDate> result = new ArrayList<>();
        if (endDate.compareTo(startDate) < 0) {
            return result;
        }
        while (true) {
            result.add(endDate);
            if (endDate.compareTo(startDate) <= 0) {
                break;
            }
            endDate = endDate.plusDays(-1);
        }
        return result;
    }

    /**
     * 两个时间内的所有天数 从小到大
     */
    public static List<LocalDate> getAscDateList(LocalDate startDate, LocalDate endDate) {
        List<LocalDate> result = new ArrayList<>();
        if (endDate.compareTo(startDate) < 0) {
            return result;
        }
        while (true) {
            result.add(startDate);
            if (startDate.compareTo(endDate) >= 0) {
                break;
            }
            startDate = startDate.plusDays(1);
        }
        return result;
    }

    /**
     * 格式化秒数到时分秒
     *
     * @param totalSeconds 总秒数
     * @return
     */
    public static String formateNoneNullTime(long totalSeconds) {

        long hours = totalSeconds / 60 / 60;
        long minute = totalSeconds / 60 % 60;
        long seconds = totalSeconds % 60;
        StringBuffer sb = new StringBuffer();
        if (hours > 0) {
            sb.append(String.format("%d时%02d分%02d秒", hours, minute, seconds));
        } else {
            if (minute > 0) {
                sb.append(String.format("%d分%02d秒", minute, seconds));
            } else {
                sb.append(String.format("%d秒", seconds));
            }
        }
        return sb.toString();
    }


    /**
     * Date--》LocalDateTime
     *
     * @param date
     * @param
     * @return
     */
    public static LocalDateTime convertDate2DateTime(Date date) {

        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }


    /**
     * 加天数
     *
     * @param date
     * @param days
     * @return
     */
    public static Date addDays(Date date, int days) {
        LocalDate localDate = convertDate2Date(date);
        return Date.from(localDate.plusDays(days).atStartOfDay(ZoneId.systemDefault()).toInstant());
    }


    /**
     * Date--》LocalDate
     *
     * @param date
     * @param
     * @return
     */
    public static LocalDate convertDate2Date(Date date) {

        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }

    /**
     * 获取最近的日期
     *
     * @param lastDays      最近多少天
     * @param containsToday 是否包含今天
     * @param isAsc         正序还是倒序
     * @return 最近的日期数组 yyyy-MM-dd格式
     */
    public static List<String> getLastDaysStr(int lastDays, boolean containsToday, boolean isAsc) {
        List<String> list = new ArrayList<>();
        LocalDateTime today = LocalDateTime.now();
        int i = 0;
        if (!containsToday) {
            i = 1;
            lastDays += 1;
        }
        for (; i < lastDays; i++) {
            list.add(DateUtil.format(today.minusDays(i), YYYY_MM_DD));
        }

        if (isAsc) {
            Collections.reverse(list);
        }

        return list;
    }

}
