package com.sv.service.api.util;

import com.enums.VipTypeEnum;

import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.Date;

public class DateUtilCard {

    public static Date getYear(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, 1); //增加一年
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        return calendar.getTime();
    }

    public static Date getDay(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1); //增加一天
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        return calendar.getTime();
    }

    public static Date getMouth(Date date,String cardType){
        Integer mouth = 1;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
//        if (n.intValue() == 2){
        if (cardType.equals(VipTypeEnum.getByValue(2).getName())){
            mouth = 6;
        }else if (cardType.equals(VipTypeEnum.getByValue(3).getName())){
            mouth = 3;
        }
        calendar.add(Calendar.MONTH, mouth); //增加对应的月
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        return calendar.getTime();
    }

    /**
     * 获取当前时间年月日
     * @param date
     * @return
     */
    public static Date getTime(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        return calendar.getTime();
    }

    /**
     * 获取当前时间，格式HH:mm:ss
     */
    public static LocalTime nowTime(){
        LocalTime time = LocalTime.now().withNano(0);
        return time;
    }

    /**
     * 时间转换
     */
    public static Date date(Date date,LocalTime starTime){
        Date lessonDate = null;
        try {
            SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
            String da = f.format(date);
            String lessonTime = da+" " + starTime+":00";
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            lessonDate = format.parse(lessonTime);
        }catch (Exception e){
            e.printStackTrace();
        }
        return lessonDate;
    }

    /**
     * 获取当月的天数
     * @return
     */
    public static int getCurrentMonthDay() {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 根据年份，月份查看天数
     * @param year
     * @param month
     * @return
     */
    public static int getDaysByYearMonth(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }
}
