package com.tofly.xrtymis.custom.utils;

import com.tofly.xrtymis.custom.em.EDatePartern;
import com.tofly.xrtymis.custom.exception.BizException;
import org.apache.commons.lang3.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 项目名  reimbursement
 * Created by fu.
 * Created at 2021/3/23
 * 描述:日期工具类
 */
public class DateUtils {
    private static final Logger logger = LoggerFactory.getLogger(DateUtils.class);

    private static FastDateFormat fastDateFormat = FastDateFormat.getInstance("yyyy-MM-dd");
    private static FastDateFormat fastDateTimeFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
    private static FastDateFormat fastDateTime = FastDateFormat.getInstance("yyyyMMddHHmmss");
    private static FastDateFormat fastTimeFormat = FastDateFormat.getInstance("HH:mm");

    public static String formatTime(Date date) {
        return fastTimeFormat.format(date);
    }

    public static String formatDate(Date date) {
        return formatDate(date, true);
    }

    public static String formatDate(Date date, boolean hasLine) {
        String dateStr = fastDateFormat.format(date);
        if (hasLine) {
            return dateStr;
        } else {
            return dateStr.replaceAll("-", "");
        }
    }

    public static String formatCurrentDate(boolean hasLine) {
        String date = fastDateFormat.format(new Date());
        if (hasLine) {
            return date;
        } else {
            return date.replaceAll("-", "");
        }
    }

    public static String formatCurrentDate() {
        return formatCurrentDate(true);
    }

    public static String formatCurrentDateTime() {
        return fastDateTimeFormat.format(new Date());
    }

    public static String formatDateTime(Date date) {
        return fastDateTimeFormat.format(date);
    }
    public static String fastDateTime(Date date) {
        return fastDateTime.format(date);
    }

    public static Date parseCurrentDate(String date) throws ParseException {
        return fastDateFormat.parse(date);
    }

    public static Date parseCurrentDateTime(String dateTime) throws ParseException {
        return fastDateTimeFormat.parse(dateTime);
    }

    /**
     * 将日期转换成String
     */
    public static String formatDateToString(Date date, String pattern) {
        FastDateFormat format = FastDateFormat.getInstance(pattern);
        return format.format(date);
    }

    /**
     * 获取后一天日期
     */
    public static String getAfterDay(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        return sdf.format(calendar.getTime());
    }

    public static long stringToLong(String strTime, String formatType) throws BizException {
        Date date = stringToDate(strTime, formatType); // String类型转成date类型
        if (date == null) {
            return 0;
        } else {
            return dateToLong(date);
        }
    }

    /**
     * String2Date
     */
    public static Date stringToDate(String strTime, String formatType) throws BizException {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(formatType);
            return formatter.parse(strTime);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BizException(e.getMessage());
        }
    }

    public static long dateToLong(Date date) {
        return date.getTime();
    }

    /**
     * 转成：时:分
     */
    public static String stringToHHMM(String time) throws BizException {
        try {
            Date d = DateUtils.stringToDate(time, EDatePartern.YYYYMMDDHHMMSS.getKey());
            return DateUtils.formatDateToString(d, EDatePartern.HH_MM.getKey());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BizException(e.getMessage());
        }
    }

    /**
     * 转成：月-日
     */
    public static String stringToMMDD(String time) throws BizException {
        try {
            Date d = DateUtils.stringToDate(time, EDatePartern.YYYYMMDDHHMMSS.getKey());
            return DateUtils.formatDateToString(d, EDatePartern.MM_DD.getKey());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BizException(e.getMessage());
        }
    }

    /**
     * 转成：年-月
     */
    public static String stringToYYYYMM(String time) throws BizException {
        try {
            Date d = DateUtils.stringToDate(time, EDatePartern.YYYYMMDDHHMMSS.getKey());
            return DateUtils.formatDateToString(d, EDatePartern.YYYY_MM.getKey());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BizException(e.getMessage());
        }
    }

//    public static void main(String[] args) throws BizException {
//        Calendar calendar = Calendar.getInstance();
//        Date currentTime = calendar.getTime();
//        System.out.println(formatDateToString(getCurrent30MinuteTOzero(currentTime), EDatePartern.YYYYMMDDHHMMSS.getKey()));
//        for (int i = 0; i < 9; i++) {
//            currentTime = getBefore30Minutes(getCurrent30MinuteTOzero(currentTime));
//            System.out.println(formatDateToString(currentTime, EDatePartern.YYYYMMDDHHMMSS.getKey()));
//        }
//    }

    /**
     * 根据当前时间四舍五入取半个小时的整数时间
     */
    public static Date getCurrent30MinuteTOzero(Date currentTime) throws BizException {
        try {
            String hm = stringToHHMM(formatDateToString(currentTime, EDatePartern.YYYYMMDDHHMMSS.getKey()));
            String temp = hm.substring(0, hm.indexOf(":")) + ":30";
            String times = null;
            long hmnew = DateUtils.stringToLong(String.valueOf(hm) + ":00", EDatePartern.HH_MM_SS.getKey());
            long tempnew = DateUtils.stringToLong(String.valueOf(temp) + ":00", EDatePartern.HH_MM_SS.getKey());
            if (hmnew < tempnew) {
                times = hm.substring(0, hm.indexOf(":")) + ":00";
            } else if (hmnew == tempnew) {
                times = hm;
            } else {
                times = temp;
            }

            String newTime = formatDateToString(currentTime, EDatePartern.YYYY_MM_DD.getKey()) + " " + times + ":00";
            currentTime = stringToDate(newTime, EDatePartern.YYYYMMDDHHMMSS.getKey());
            return currentTime;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BizException(e.getMessage());
        }
    }

    /**
     * 获取前半个小时的时间
     */
    public static Date getBefore30Minutes(Date time) throws BizException {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar cal = Calendar.getInstance();
            cal.setTime(time);
            cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE) - 30);
            return cal.getTime();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BizException(e.getMessage());
        }
    }

    /**
     * 获取前后个小时的时间
     */
    public static Date getAfter30Minutes(Date time) throws BizException {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar cal = Calendar.getInstance();
            cal.setTime(time);
            cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE) + 30);
            return cal.getTime();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BizException(e.getMessage());
        }
    }

    public static Date getAllDay(){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        return cal.getTime();
    }



}
