package cn.cslg.pas.common.utils;


import cn.hutool.core.date.DateUtil;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

import static cn.cslg.pas.common.utils.DateUtils.FORMAT_STRING;

public class DateUtils2 {

    public static DateTime formStrToDateTime(String dateString) {
        try {
            DateTimeFormatter formatter;

            if (dateString.contains("-")) {
                formatter = DateTimeFormat.forPattern("yyyy-MM-dd");
            } else if (dateString.contains("//")) {
                formatter = DateTimeFormat.forPattern("yyyy/MM/dd");
            } else if (dateString.contains(":")) {
                formatter = DateTimeFormat.forPattern("yyyy:MM:dd");
            } else if (dateString.contains(".")) {
                formatter = DateTimeFormat.forPattern("yyyy.MM.dd");
            } else {
                formatter = DateTimeFormat.forPattern("yyyyMMdd");
            }

            return formatter.parseDateTime(dateString);
        } catch (Exception e) {
            throw new RuntimeException("时间转化格式错误" + "[dateString=" + dateString + "]" + "[FORMAT_STRING=" + FORMAT_STRING + "]");
        }
    }


    public static List<DateTime> formStrToDateTime(DateTime dateStart, DateTime dateEnd) {
        List<DateTime> dateTimes = new ArrayList<>();
        dateTimes.add(dateStart);
        DateTime minDate1 = new DateTime();
        DateTime minDate2= new DateTime();
        Integer startYear = dateStart.getYear();
        Integer startMonth = dateStart.getMonthOfYear();
        Integer startDay = dateStart.getDayOfYear();
        Integer endYear = dateEnd.getYear();
        Integer endMonth = dateEnd.getMonthOfYear();
        Integer endDay = dateEnd.getDayOfYear();
        if (startYear.equals(endYear) ) {
            if (startMonth.equals(endMonth)) {
                if (startDay .equals(endDay) ) {
                    minDate1=dateStart;
                } else {
                    int diffDay = endDay - startDay;
                    int minDay = diffDay / 2;
                    minDate1 = dateStart.plusDays(minDay);
                }
            } else {
                int diffMonth = endYear - startYear;
                int minMonth = diffMonth / 2;
                if(minMonth==0){
                    minMonth=1;
                }
                minDate1 = dateStart.plusMonths(minMonth).minusDays(1);
                minDate2 =dateStart.plusMonths(minMonth);
            }
        } else {
            int diffYear = endYear - startYear;
            int minYear = diffYear / 2;
            if(minYear==0){
                minYear=1;
            }
            minDate1 = dateStart.plusYears(minYear).minusDays(1);
            minDate2 =dateStart.plusYears(minYear);
            if(minDate1.isAfter(minDate2)){
                System.out.println("aa");
            }
        }
        dateTimes.add(minDate1);
        dateTimes.add(minDate2);
        dateTimes.add(dateEnd);
        return dateTimes;
    }

    public static void main(String[] args) {
        List<DateTime> dateTimeList =new ArrayList<>();
        List<DateTime> dateTimeList2 =new ArrayList<>();
        DateTime dateTime =DateUtils2.formStrToDateTime("1901-7-14");
        DateTime dateTime1 =DateUtils2.formStrToDateTime("1900-1-1");
      List<DateTime> dateTimes =   DateUtils2.formStrToDateTime(dateTime1,dateTime);
        dateTimeList2.addAll(dateTimes);
         while (true){
             DateTime d1 =dateTimeList2.get(0);
             DateTime d2 =dateTimeList2.get(1);
             DateTime d3 =dateTimeList2.get(2);
             DateTime d4 =dateTimeList2.get(3);
             dateTimeList2.remove(d1);
             dateTimeList2.remove(d2);
             dateTimeList2.remove(d3);
             dateTimeList2.remove(d4);
             List<DateTime> dateTimes3 =   DateUtils2.formStrToDateTime(d1,d2);
             List<DateTime> dateTimes4 =   DateUtils2.formStrToDateTime(d3,d4);
             dateTimeList2.addAll(dateTimes3);
             dateTimeList2.addAll(dateTimes4);
             dateTimeList.addAll(dateTimes3);
             dateTimeList.addAll(dateTimes4);

         }
    }
}
