package com.fdkankan.ucenter.util;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.fdkankan.common.util.SecurityUtil;
import com.fdkankan.ucenter.common.MailUtil;
import com.fdkankan.ucenter.entity.IncrementType;
import com.fdkankan.ucenter.entity.ScenePro;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

public class DateUserUtil {

    public static final String DEF_FMT = "yyyy-MM-dd HH:mm:ss";
    public static final String DAY_FMT = "yyyy-MM-dd";
    public static final String M_FMT = "yyyy-MM-dd HH:mm";

    public static String getLastTime(String time1,String time2){
        return getDate(time1).getTime() >getDate(time2).getTime() ? time1 : time2;
    }

    public static Date getDate(String time){
        try {
            return new SimpleDateFormat(DEF_FMT).parse(time);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Date();
    }
    public static String getDate(Date time){
        return new SimpleDateFormat(DEF_FMT).format(time);
    }
    public static String getDate(Date time,String fmt){
        return new SimpleDateFormat(fmt).format(time);
    }
    public static String getZeroDate(){
        return new SimpleDateFormat(DAY_FMT).format(new Date()) +" 00:00:00";
    }
    public static String getLastDate(){
        return new SimpleDateFormat(DAY_FMT).format(new Date()) +" 23:59:59";
    }

    public static String getDayTime(String time) {
        try {
            Date date = new SimpleDateFormat(DEF_FMT).parse(time);
            return new SimpleDateFormat(DAY_FMT).format(date);
        }catch (Exception e){
            e.printStackTrace();
        }
       return time;
    }

    public static DateTime getDateTime(Date userTime,IncrementType incrementType,Integer count) {
        DateTime dateTime = new DateTime();
        if(userTime.getTime() >new Date().getTime()){
            dateTime = new DateTime(userTime);
        }
        if(count == null){
            count = 1;
        }
        switch (incrementType.getValidTimeType()){
            case 0 : return dateTime.plusYears(incrementType.getValidTime() * count);
            case 1 : return dateTime.plusMonths(incrementType.getValidTime() * count);
            case 2 : return dateTime.plusDays(incrementType.getValidTime() * count);
            default: return dateTime;
        }
    }

    public static Long getLastNowTime() {
        Date date1 = new Date();
        Date date2 = DateUserUtil.getDate(getLastDate());
        return ( date2.getTime() - date1.getTime() ) /1000;
    }

    public static String getMDate(String time) {
        try {
            Date date = new SimpleDateFormat(DEF_FMT).parse(time);
            return new SimpleDateFormat(M_FMT).format(date);
        }catch (Exception e){
            e.printStackTrace();
        }
        return time;
    }

    public static Long between(Date endDate, Date newDate) {
        long between = DateUtil.between(endDate, newDate, DateUnit.DAY);
        if(between == 0 && endDate.getTime() > newDate.getTime()){
            return 1L;
        }
        if(endDate.getTime() < newDate.getTime()){
            return 0L;
        }
        return between;
    }
    public static Date getMonthDate(Date endDate, Integer month) {

        LocalDateTime localDateTime = endDate.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime().plusMonths(month);
        return  Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static String AddMinute(Date date, Integer minute){
        SimpleDateFormat sdf = new SimpleDateFormat(DEF_FMT);
        Calendar nowTime = Calendar.getInstance();
        nowTime.setTime(date);
        nowTime.add(Calendar.MINUTE, ~minute);
        return sdf.format(nowTime.getTime());
    }
    public static String AddMinute(String date, Integer minute){
        SimpleDateFormat sdf = new SimpleDateFormat(DEF_FMT);
        try {
            Date time =  sdf.parse(date);
            return AddMinute(time,minute);
        }catch (Exception e){
            e.printStackTrace();
        }
        return date;
    }

    public static String getLastZeroTime(String endTime) {
        try {
            Date date = DateUserUtil.getDate(endTime);
            String date1 = DateUserUtil.getDate(date, DateUserUtil.DAY_FMT);
            return date1 + " 23:59:59";
        }catch ( Exception e){
            e.printStackTrace();
        }
        return endTime;
    }

    public static void main(String[] args) {
        System.out.println(SecurityUtil.MD5(":\u0011\u001B\u0018\u0015\u0007\u0011\u00064DGFE"));
    }

    public static Date getHoursTime(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, 1);
        return calendar.getTime();
    }
}
