package com.sonic.shark.utils;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Lists;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.WeekFields;
import java.util.Date;
import java.util.List;

/**
 * @Author code
 */
public class DateConvertUtils {


    /**
     * 获取本周周一的日期
     * @return
     */
    public static String getMondayOfThisWeek() {
        LocalDate monday = LocalDate.now().with(TemporalAdjusters.previous(DayOfWeek.SUNDAY)).plusDays(1);
        return monday.toString();
    }

    /**
     * 根据年和周数获取周的所有日期时间
     * @param year
     * @param num
     * @param dayOfWeekList
     * @return
     */
    public static List<LocalDate> getDateByYearAndWeekNumAndDayOfWeek(Integer year, Integer num, List<DayOfWeek> dayOfWeekList) {
        List<LocalDate> result = Lists.newArrayList();
        if(CollectionUtils.isEmpty(dayOfWeekList)) {
            dayOfWeekList = Lists.newArrayList(DayOfWeek.MONDAY, DayOfWeek.TUESDAY, DayOfWeek.WEDNESDAY, DayOfWeek.THURSDAY, DayOfWeek.FRIDAY, DayOfWeek.SATURDAY, DayOfWeek.SUNDAY);
        }
        //周数小于10在前面补个0
        String numStr = num < 10 ? "0" + String.valueOf(num) : String.valueOf(num);
        //2019-W01-01获取第一周的周一日期，2019-W02-07获取第二周的周日日期
        for (DayOfWeek dayOfWeek : dayOfWeekList) {
            String weekDate = String.format("%s-W%s-%s", year, numStr, dayOfWeek.getValue());
            LocalDate localDate = LocalDate.parse(weekDate, DateTimeFormatter.ISO_WEEK_DATE);
            result.add(localDate);
        }
        return result;
    }

    /**
     * 获取当前时间的年
     * @return
     */
    public static int getYear() {
        LocalDateTime now = LocalDateTime.now();
        return now.getYear();
    }

    /**
     * 获取当前年中的周
     * @return
     */
    public static int getYearWeek() {
        LocalDateTime now = ZonedDateTime.now(ZoneId.of("America/Los_Angeles")).toLocalDateTime();
        WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY,4);
        int week = now.get(weekFields.weekOfWeekBasedYear());
        return week;
    }

    public static String format(LocalDateTime localDateTime, String pattern) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        //美国洛杉矶时间
        String date = dateTimeFormatter.format(localDateTime.minusHours(15));
        return date;
    }

    public static String format(LocalDateTime localDateTime) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        //美国洛杉矶时间
        String date = dateTimeFormatter.format(localDateTime.minusHours(15));
        return date;
    }

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

    public static String noTimeZoneChangeFormat(LocalDate localDate) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String date = localDate.format(dateTimeFormatter);
        return date;
    }

    public static String format() {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        //美国洛杉矶时间
        String date = dateTimeFormatter.format(ZonedDateTime.now(ZoneId.of("America/Los_Angeles")).toLocalDateTime());
        return date;
    }

    public static String format(Date date) {
        SimpleDateFormat dateTimeFormatter = new SimpleDateFormat("yyyy-MM-dd");
        String result = dateTimeFormatter.format(date);
        return result;
    }

    public static LocalDateTime getDateMinTime(){
        LocalDateTime now = ZonedDateTime.now(ZoneId.of("America/Los_Angeles")).toLocalDateTime()
                .with(LocalTime.MIN);
        return now;
    }

    public static LocalDateTime getDateMinTime(LocalDateTime localDateTime){
        LocalDateTime now = localDateTime.now(ZoneId.of("America/Los_Angeles"))
                .with(LocalTime.MIN);
        return now;
    }

    public static LocalDateTime getDateMaxTime(){
        LocalDateTime now = ZonedDateTime.now(ZoneId.of("America/Los_Angeles")).toLocalDateTime()
                .with(LocalTime.MAX);
        return now;
    }

    public static LocalDateTime getDateMaxTime(LocalDateTime localDateTime){
        LocalDateTime now = localDateTime.now(ZoneId.of("America/Los_Angeles"))
                .with(LocalTime.MAX);
        return now;
    }

    public static LocalDateTime getDateLaTime(){
        LocalDateTime now = ZonedDateTime.now(ZoneId.of("America/Los_Angeles")).toLocalDateTime();
        return now;
    }

    public static Integer formatToLocalInt(LocalDateTime localDateTime) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String date = dateTimeFormatter.format(localDateTime);
        return Integer.valueOf(date);
    }

    /**
     * 获取两个时间相差的秒数
     * @param startTime
     * @param endTime
     * @return
     */
    public static Long getTimeDifference(LocalDateTime startTime, LocalDateTime endTime) {
        Duration duration = Duration.between(startTime, endTime);
        return duration.getSeconds();
    }

}
