package com.njcn.device.pq.utils;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class SpiltDateUtil {

    private static SimpleDateFormat monthDateFormat = new SimpleDateFormat("MM");
    private static SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 将时间段按星期分割
     * @param start
     * @param end
     * @return
     */
    public static final List<Range> splitToWeeks(Date start, Date end) {
        List<Range> result = new ArrayList<>();
        result.add(Range.create(start));
        Date from = new Date(start.getTime() + 7L * 24 * 3600 * 1000);
        Date weekEnd = DateUtil.endOfWeek(end);
        while (from.compareTo(weekEnd) <= 0) {
            Date dt = DateUtil.beginOfWeek(from);
            CollectionUtil.getLast(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
            CollectionUtil.getLast(result).setMonth(monthDateFormat.format(new Date(dt.getTime() - 24L * 3600 * 1000)));
            result.add(Range.create(dt));
            from.setTime(from.getTime() + 7L * 24 * 3600 * 1000);
        }
        CollectionUtil.getLast(result).end(end);
        CollectionUtil.getLast(result).setMonth(monthDateFormat.format(end));
        return result;
    }

    /**
     * 将时间段按照月分割
     * @param startTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    public static final List<Range> splitToMonths(String startTime, String endTime) throws ParseException {
        Date start = date.parse(startTime);
        Date end = date.parse(endTime);
        List<Range> result = new ArrayList<>();
        result.add(Range.create(start));
        Calendar cal = Calendar.getInstance();
        cal.setTime(start);
        cal.add(Calendar.MONTH, 1);
        Date monthEnd = DateUtil.endOfMonth(end);
        while (cal.getTimeInMillis() <= monthEnd.getTime()) {
            Date dt = DateUtil.beginOfMonth(cal.getTime());
            CollectionUtil.getLast(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
            CollectionUtil.getLast(result).setMonth(monthDateFormat.format(new Date(dt.getTime() - 24L * 3600 * 1000)));
            result.add(Range.create(dt));
            cal.add(Calendar.MONTH, 1);
        }
        CollectionUtil.getLast(result).end(end);
        CollectionUtil.getLast(result).setMonth(monthDateFormat.format(end));
        return result;
    }

    /**
     * 将时间段按照季度分割
     * @param start
     * @param end
     * @return
     */
    public static final List<Range> splitToQuarts(Date start, Date end) {
        List<Range> result = new ArrayList<>();
        result.add(Range.create(start));
        Calendar cal = Calendar.getInstance();
        cal.setTime(start);
        cal.add(Calendar.MONTH, 3);
        Date quartEnd = DateUtil.endOfQuarter(end);
        while (cal.getTimeInMillis() <= quartEnd.getTime()) {
            Date dt = DateUtil.beginOfQuarter(cal.getTime());
            CollectionUtil.getLast(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
            System.out.println(cal.get(Calendar.MONTH));
            if (cal.get(Calendar.MONTH) >= 1 && cal.get(Calendar.MONTH) <= 3) {
                CollectionUtil.getLast(result).setMonth("4");
            } else if (cal.get(Calendar.MONTH) >= 4 && cal.get(Calendar.MONTH) <= 6) {
                CollectionUtil.getLast(result).setMonth("1");
            } else if (cal.get(Calendar.MONTH) >= 7 && cal.get(Calendar.MONTH) <= 9) {
                CollectionUtil.getLast(result).setMonth("2");
            } else if (cal.get(Calendar.MONTH) >= 10 && cal.get(Calendar.MONTH) <= 12) {
                CollectionUtil.getLast(result).setMonth("3");
            }
            result.add(Range.create(dt));
            cal.add(Calendar.MONTH, 3);
        }
        CollectionUtil.getLast(result).end(end);
        System.out.println(cal.get(Calendar.MONTH));
        if (cal.get(Calendar.MONTH) >= 1 && cal.get(Calendar.MONTH) <= 3) {
            CollectionUtil.getLast(result).setMonth("4");
        } else if (cal.get(Calendar.MONTH) >= 4 && cal.get(Calendar.MONTH) <= 6) {
            CollectionUtil.getLast(result).setMonth("1");
        } else if (cal.get(Calendar.MONTH) >= 7 && cal.get(Calendar.MONTH) <= 9) {
            CollectionUtil.getLast(result).setMonth("2");
        } else if (cal.get(Calendar.MONTH) >= 10 && cal.get(Calendar.MONTH) <= 12) {
            CollectionUtil.getLast(result).setMonth("3");
        }
        return result;
    }

    /**
     * 将时间段按照半年分割
     * @param start
     * @param end
     * @return
     */
    public static final List<Range> splitToHalfOfYears(Date start, Date end) {
        List<Range> result = new ArrayList<>();
        result.add(Range.create(start));
        Calendar cal = Calendar.getInstance();
        cal.setTime(start);
        cal.add(Calendar.MONTH, 6);
        Calendar calOne = Calendar.getInstance();
        calOne.setTime(end);
        int mon = cal.get(Calendar.MONTH);
//        Calendar tmp = Calendar.getInstance();
//        if (mon < 6) {
//            tmp.setTimeInMillis(cal.getTimeInMillis());
//            tmp.set(Calendar.MONTH, 5);
//        } else {
//            tmp.setTimeInMillis(cal.getTimeInMillis());
//            tmp.set(Calendar.MONTH, 11);
//        }
        Calendar halfYearEnd = DateUtil.endOfMonth(calOne);
        while (cal.getTimeInMillis() <= halfYearEnd.getTimeInMillis()) {
            mon = cal.get(Calendar.MONTH);
            Calendar cal1 = Calendar.getInstance();
            if (mon < 6) {
                cal1.setTimeInMillis(cal.getTimeInMillis());
                cal1.set(Calendar.MONTH, 0);
            } else {
                cal1.setTimeInMillis(cal.getTimeInMillis());
                cal1.set(Calendar.MONTH, 6);
            }
            Date dt = DateUtil.beginOfMonth(cal1.getTime());
            CollectionUtil.getLast(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
            result.add(Range.create(dt));
            cal.add(Calendar.MONTH, 6);
        }
        CollectionUtil.getLast(result).end(end);
        return result;
    }

    /**
     * 将时间段按照年分割
     * @param start
     * @param end
     * @return
     */
    public static final List<Range> splitToYears(Date start, Date end) {
        List<Range> result = new ArrayList<>();
        result.add(Range.create(start));
        Calendar cal = Calendar.getInstance();
        cal.setTime(start);
        cal.add(Calendar.YEAR, 1);
        Date yearEnd = DateUtil.endOfYear(end);
        while (cal.getTimeInMillis() <= yearEnd.getTime()) {
            Date dt = DateUtil.beginOfYear(cal.getTime());
            CollectionUtil.getLast(result).end(new Date(dt.getTime() - 24L * 3600 * 1000));
            result.add(Range.create(dt));
            cal.add(Calendar.YEAR, 1);
        }
        CollectionUtil.getLast(result).end(end);
        return result;
    }

    /**
     * 日期区间
     */
    public static class Range {
        Date start;
        Date end;
        String Month;

        public Range() {

        }

        private Range(Date start) {
            this.start = start;
        }

        public static Range create(Date start) {
            return new Range(start);
        }

        public Range end(Date end) {
            this.end = end;
            return this;
        }

        public void setStart(Date start) {
            this.start = start;
        }

        public void setEnd(Date end) {
            this.end = end;
        }

        public Date getStart() {
            return start;
        }

        public Date getEnd() {
            return end;
        }

        public String getMonth() {
            return Month;
        }

        public void setMonth(String month) {
            Month = month;
        }

        @Override
        public String toString() {
            return "[" + DateUtil.format(start, "yyyy-MM-dd") + "," + DateUtil.format(end, "yyyy-MM-dd")
                    + "]";
        }
    }

    /**
     * 根据年，月，第几周获取时间范围
     * @param year
     * @param month
     * @param week
     * @return
     */
    public static Map getWeekDateLimits(String year, String month, String week) {
        Map result = new HashMap();
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c1 = Calendar.getInstance();
        c1.set(Calendar.YEAR, Integer.parseInt(year));
        c1.set(Calendar.MONTH, Integer.parseInt(month) - 1);
        c1.set(Calendar.DAY_OF_WEEK, 2);
        c1.set(Calendar.WEEK_OF_MONTH, Integer.parseInt(week));
        Date time1 = c1.getTime();
        String begin = dft.format(time1);
        c1.set(Calendar.DATE, c1.get(Calendar.DATE) + 6);
        Date time2 = c1.getTime();
        String end = dft.format(time2);
        result.put("begin", begin);
        result.put("end", end);
        return result;
    }


    /***
     * 获取两个时间段的年份/年第一天/年最后一天
     * @param startTime
     * @param endTime
     * @return
     */
    public static String getYears(String startTime, String endTime) {
        String year = null;
//        List<Map> res=new ArrayList<Map>();
        DateFormat dateFormat = new SimpleDateFormat("yyyy");
        DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.YEAR,1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                String years=dateFormat.format(tempStart.getTime());
                String first=year+"-01-01 00:00:00";
                String last=year+"-12-31 23:59:59";
                Map<String,Object> map=new HashMap<String,Object>();
                year=years;
//                map.put("year", year);
//                map.put("first", dateFormat2.parse(first));
//                map.put("last", dateFormat2.parse(last));
//                res.add(map);
                tempStart.add(Calendar.YEAR,1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return year;
    }

    /***
     * 获取两个时间段的年份-月份/月第一天/月最后一天
     * @param startTime
     * @param endTime
     * @return
     */
    public static String getMonths(String startTime, String endTime) {
//        List<Map> res=new ArrayList<Map>();

        String moths = null;

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat dateFormat3 = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.MONTH,1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                String month=dateFormat.format(tempStart.getTime());
                tempStart.set(Calendar.DAY_OF_MONTH, 1);
                String first=dateFormat3.format(tempStart.getTime());
                tempStart.set(Calendar.DAY_OF_MONTH, tempStart.getActualMaximum(Calendar.DAY_OF_MONTH));
//                Map<String,Object> map=new HashMap<String,Object>();
//                map.put("month", month);
//                map.put("first", dateFormat2.parse(first+" 00:00:00"));
//                map.put("last", dateFormat2.parse(first+" 23:59:59"));
//                res.add(map);
                moths = month;
                tempStart.add(Calendar.MONTH,1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return moths;
    }

    /***
     * 获取两个时间段的天数/开始时间/结束时间
     * @param startTime
     * @param endTime
     * @return
     */
    public static String getDays(String startTime, String endTime) {
        // 返回的日期集合
//        List<Map> res=new ArrayList<Map>();

        String days = null;

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                String day=dateFormat.format(tempStart.getTime());
//                Map<String,Object> map=new HashMap<String,Object>();
//                map.put("day", day);
//                map.put("first", dateFormat2.parse(day+" 00:00:00"));
//                map.put("last", dateFormat2.parse(day+" 23:59:59"));
//                res.add(map);
                days=day;
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }





}
