package com.tax.file.service.async.apply.util;

import com.tax.file.constants.FileConstants;
import com.tax.file.model.LackDateFile;
import com.tax.file.model.TkFile;
import com.tax.file.model.TkTaxUser;
import com.yxcheng.token.utils.DateUtils;

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

public class ApplyUtil {
    public static  int tokenTimes = 10;

    //是否在当前周期内
    public static boolean isInCurrentCycleDate(Date date) {
        Date[] nationDates = getCycleDates(FileConstants.applyCycle);
        date = DateUtils.addDate(date,2);
        if(date.after(nationDates[0]) && date.before(nationDates[1]))return true;
        return false;
    }


    public static int getDayCycle(Date date){
        return DateUtils.getDayCycle(FileConstants.applyCycle,date);
    }


    public static Date[] getNationDates(){
        return DateUtils.getNationDates(FileConstants.applyCycle);
    }


    public static Date[] getCycleDates(){
        return getCycleDates(FileConstants.applyCycle);
    }


    public static Date[] getCycleDates(String nationCycle){
        Date[] nationDates = DateUtils.getNationDates(nationCycle);
        if(nationDates!=null)return nationDates;

        Date[] dates = new Date[2];
        Date now = new Date();
        if(nationDates==null){
            dates[1] = DateUtils.getPreLastDay(now, 1);
            dates[0] = DateUtils.getApplyStartDate(dates[1], nationCycle);
        }
        return dates;
    }


    public static boolean isLastMonthFix(){
        Date[] nationDates = DateUtils.getNationDates(FileConstants.applyCycle);
        return nationDates == null;
    }


    public static Date getPreApplyDate(Date date){
        return DateUtils.getPreApplyDate(date,FileConstants.preApplyRange);
    }

    public static boolean isRunning(TkTaxUser parentTaxUser) {
        return true;
    }

    public static boolean isFinish(TkTaxUser tkTaxUser) {
        return DateUtils.isCommonDayCycle(FileConstants.applyCycle,tkTaxUser.getApplyDownDate()) && "2".equals(tkTaxUser.getApplyStatus());
    }

    public static boolean isCommonDayCycle(Date date){
        return DateUtils.isCommonDayCycle(FileConstants.applyCycle,date);
    }

    public static Date getPreviousCycleDate() {
        Date[] cycleDates = getCycleDates();
        return DateUtils.addDate(cycleDates[0],-1);
    }


    public static void main(String[] args) {
        //System.out.println(ApplyUtil.getCycleDates()[1]);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DATE,8);
        calendar.set(Calendar.MONTH,9);
        System.out.println(ApplyUtil.getDayCycle(calendar.getTime()));


        Date now = new Date();
        Date preDateEnd = DateUtils.getPreLastDay(now,1);
        Date preDateStart = DateUtils.getPreFirstDay(now,1);
        System.out.println(preDateStart);
        System.out.println(DateUtils.date2Str(ApplyUtil.getPreviousCycleDate(), DateUtils.time_sdf));
    }

    public static boolean isBeyondTaxNationDay(Date applyDate) {
        if(applyDate == null) return false;
        try {
            return  DateUtils.dayCompare(applyDate,new Date()) > FileConstants.beyondTaxNationDay;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    public static String getKey(String key, TkFile entity){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return key + entity.getTaxno() + entity.getInvSpec()  + DateUtils.date2Str(entity.getInvDateStart(),sdf) + DateUtils.date2Str(entity.getInvDateEnd(),sdf);
    }

    public static String getKey(String key, LackDateFile entity){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return key + entity.getTaxno() + entity.getInvSpec()  + DateUtils.date2Str(entity.getInvDateStart(),sdf) + DateUtils.date2Str(entity.getInvDateEnd(),sdf);
    }


}
