package com.persagy.calendar.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
public class TimeUtil {

    private static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    private static final String DATE_PATTERN = "yyyy-MM-dd";
    private static final String DATE_CONTINUITY_PATTERN = "yyyyMMdd";
    private static final String year = "2022-";
    private static final String time = " 00:00:00";

    public static Long timeToLong(String dateTimeStr) {
        try {
            SimpleDateFormat df = new SimpleDateFormat(DATE_TIME_PATTERN);
            Date date = df.parse(dateTimeStr);
            long time = date.getTime();
            return time;
        }catch (Exception e) {
            log.error("日期格式化异常", e);
            return null;
        }
    }

    public static Long dateToLong(String dateTimeStr) {
        try {
            SimpleDateFormat df = new SimpleDateFormat(DATE_PATTERN);
            Date date = df.parse(dateTimeStr);
            long time = date.getTime();
            return time;
        }catch (Exception e) {
            log.error("日期格式化异常", e);
            return null;
        }
    }

    public static int getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        return year;
    }

    public static String getStringDate(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat(DATE_PATTERN);
        String dateString = formatter.format(date);
        return dateString;
    }

    public static Date dateStrToDate(String dateTimeStr) {
        try {
            SimpleDateFormat df = new SimpleDateFormat(DATE_PATTERN);
            Date date = df.parse(dateTimeStr);
            return date;
        }catch (Exception e) {
            log.error("日期格式化异常", e);
            return null;
        }
    }

    public static Date dateStrFromRpcToDate(String dateTimeStr) {
        try {
            SimpleDateFormat df = new SimpleDateFormat(DATE_TIME_PATTERN);
            Date date = df.parse(dateTimeStr + time);
            return date;
        }catch (Exception e) {
            log.error("日期格式化异常", e);
            return null;
        }
    }

    public static Long combineAndGetLong(String dateTimeStr) {
        dateTimeStr = year + dateTimeStr + time;
        return timeToLong(dateTimeStr);
    }

    public static List<String> getBetweenDays(String startTime, String endTime) {
        try {
            if(StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)){
                return null;
            }
            //1、定义转换格式
            SimpleDateFormat df  = new SimpleDateFormat(DATE_PATTERN);

            Date start = df.parse(startTime);
            Date end = df.parse(endTime);
            List<String> result = new ArrayList<>();

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            tempStart.add(Calendar.DAY_OF_YEAR, 1);
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            result.add(sdf.format(start));
            while (tempStart.before(tempEnd)) {
                result.add(sdf.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
            return result;
        }catch (Exception e) {
            log.error("日期转换异常", e);
            return null;
        }
    }

    //TODo 待优化
    public static String longTimeToString(Long time) {
        Date date = new Date(time);
        SimpleDateFormat sformat = new SimpleDateFormat(DATE_TIME_PATTERN);
        String timeStr = sformat.format(date);
        return timeStr;
    }

    public static Boolean timeStrIsAfter(String startTimeStr, String endTimeStr) {
        try {
            SimpleDateFormat df = new SimpleDateFormat(DATE_PATTERN);
            Date startTime=df.parse(startTimeStr);
            Date endTime=df.parse(endTimeStr);
            return startTime.after(endTime);
        }catch (Exception e) {
            log.error("字符串格式化成日期异常", e);
            return null;
        }
    }

    public static String dateAddDay(String dateTime, int day) {
        try {
            SimpleDateFormat sformat = new SimpleDateFormat(DATE_TIME_PATTERN);
            Date date = sformat.parse(dateTime);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);//设置起时间
            cal.add(Calendar.DATE, day);//增加一天
            String dateTimeAddDay = sformat.format(cal.getTime());
            return dateTimeAddDay;
        }catch (Exception e) {
            log.error("字符串格式化成日期异常", e);
            return null;
        }
    }

    /**
     * @param date(2015-09-25)
     * @return 功能：校验日期是否为自然年日期
     */
    public static boolean validateDate(String date) {
        boolean result = false;
        try {
            if (date != null && !"".equals(date) && date.length() == 10) {
                String year = date.substring(0, 4);
                String month = date.substring(5, 7);
                String day = date.substring(8, 10);
                if (year.matches("^[0-9]*[1-9][0-9]*$") && month.matches("^[0-9]*[1-9][0-9]*$") && day.matches("^[0-9]*[1-9][0-9]*$")) {
                    int y = Integer.parseInt(year);
                    int m = Integer.parseInt(month);
                    int d = Integer.parseInt(day);
                    if (y >= 1900 && y <= 2999) {
                        if (m >= 1 && m <= 12) {
                            if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
                                if (d >= 1 && d <= 31) {
                                    result = true;
                                }
                            } else if (m == 4 || m == 6 || m == 9 || m == 11) {
                                if (d >= 1 && d <= 30) {
                                    result = true;
                                }
                            } else if (m == 2) {
                                if (y % 4 == 0) {
                                    if (d >= 1 && d <= 29) {
                                        result = true;
                                    }
                                } else {
                                    if (d >= 1 && d <= 28) {
                                        result = true;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (RuntimeException e) {
            throw e;
        }
        return result;
    }

    public static boolean dateInsection(Date nowTime, Date startTime, Date endTime){
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        //date 2022-11-18  start 2022-11-16  end 2022-03-15
        if (date.after(begin) && end.before(begin)) {
            end.add(Calendar.YEAR, 1);
        }

        //date 2022-01-18  start 2022-11-16  end 2022-03-15
        if (date.before(begin) && end.before(begin)) {
            begin.add(Calendar.YEAR, -1);
        }

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

}
