package com.tax.file.utils;

import com.yxcheng.token.utils.DateUtils;
import org.apache.commons.lang.StringUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class InvoiceUtil {

    public static final String HD = "111001571071,111001571072,131001570151,133011501118,133020691510,133021591000,134021522401,144031539110";

    public static Pattern patternFpdm = Pattern.compile("^[0-9]{10}|[0-9]{12}$");

    public static Pattern patternFpHm = Pattern.compile("^[0-9]{8}$");

    public static boolean isLengthCorrectForInvKind(String invKind) {
        Matcher matcher = patternFpdm.matcher(invKind);
        return matcher.matches();
    }

    public static boolean isIncomeInvoiceForInvKind(String invKind){
        String fplxdm = getFplx(invKind);
        if(StringUtils.isBlank(fplxdm))return false;
        if("01".equals(fplxdm) || "03".equals(fplxdm))return false;
        return true;
    }

    public static boolean isLengthCorrectForInvNum(String invNum) {
        Matcher matcher = patternFpHm.matcher(invNum);
        return matcher.matches();
    }


    public static boolean isInvDateFormatCorrect(String sDate) {
        int legalLen = 10;
        if ((sDate == null) || (sDate.length() != legalLen)) {
            return false;
        }

        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = formatter.parse(sDate);
            return sDate.equals(formatter.format(date));
        } catch (Exception e) {
            return false;
        }
    }


    public static boolean isIncomeMonthFormatCorrect(String sDate) {
        int legalLen = 6;
        if ((sDate == null) || (sDate.length() != legalLen)) {
            return false;
        }

        DateFormat formatter = new SimpleDateFormat("yyyyMM");
        try {
            Date date = formatter.parse(sDate);
            return sDate.equals(formatter.format(date));
        } catch (Exception e) {
            return false;
        }
    }

    public static boolean isIncomeMonthCorrect(String incomeMonth,String invDateStr) {
        DateFormat formatter = new SimpleDateFormat("yyyyMM");
        DateFormat inDateformatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date incomeMonthDate = formatter.parse(incomeMonth);
            Date invDate = inDateformatter.parse(invDateStr);
            Date now = new Date();

            int incomeMonthInt = getYearMonth(incomeMonthDate);
            int invDateInt = getYearMonth(invDate);
            int nowInt = getYearMonth(now);
            if(incomeMonthInt > nowInt){
                return  false;
            }else if(invDateInt>incomeMonthInt){
                return  false;
            }
            return  true;
        } catch (Exception e) {
            return false;
        }

    }

    public static int getYearMonth(Date date){
        //传入日期
        Calendar calder = Calendar.getInstance();
        calder.setTime(date);//设置时间
        int year = calder.get(Calendar.YEAR);//获取年份
        int month=calder.get(Calendar.MONTH);//获取月份
        //返回年份乘以100加上月份的值，因为月份最多2位数，
        // 所以年份乘以100可以获取一个唯一的年月数值
        return year*100+month;
    }

    public static boolean isIncomeCorrect(String isIncome){
        return "1".equals(isIncome) || "0".equals(isIncome);
    }


    private static String getFplx(String fpdm){

        String invoiceTypeCode = "";

        invoiceTypeCode = fpdm.substring(7, 8);
        String fplxdm = null;
        String fplxmc = null;

        if(fpdm.length() == 12){
            if(HD.indexOf(fpdm)>=0){
                fplxdm = FplxEnum.DZP.getFplxDm();
                fplxmc = FplxEnum.DZP.getFplxMc(FplxEnum.DZP.getIndex());
            }else if(fpdm.substring(0, 1).equals("0")&&
                    fpdm.substring(10, 12).equals("11")){//12位发票代码：第1位为0且第11-12位为11
                fplxdm = FplxEnum.DZP.getFplxDm();
                fplxmc = FplxEnum.DZP.getFplxMc(FplxEnum.DZP.getIndex());
            }else if(fpdm.substring(10, 12).equals("12")){//电子普通发票代码为12位，发票代码第11-12位为12
                fplxdm = FplxEnum.TXF.getFplxDm();
                fplxmc = FplxEnum.TXF.getFplxMc(FplxEnum.TXF.getIndex());
            }else if(!fpdm.substring(0, 1).equals("0")&&
                    "2".equals(invoiceTypeCode)){//12位发票代码：第1位不是0，且第8位是2
                fplxdm = FplxEnum.JDC.getFplxDm();
                fplxmc = FplxEnum.JDC.getFplxMc(FplxEnum.JDC.getIndex());
            }else if(fpdm.substring(0, 1).equals("0")&&
                    (fpdm.substring(10, 12).equals("06")||fpdm.substring(10, 12).equals("07"))){//12位发票代码：第1位为0且第11-12位为06或07
                fplxdm = FplxEnum.JSP.getFplxDm();
                fplxmc = FplxEnum.JSP.getFplxMc(FplxEnum.JSP.getIndex());
            }else if(fpdm.substring(0, 1).equals("0")&&
                    (fpdm.substring(10, 12).equals("04")||fpdm.substring(10, 12).equals("05"))){//12位发票代码：第1位为0，第11-12位票种，04代表普通发票（二联）、05代表普通发票（五联）
                fplxdm = FplxEnum.PP.getFplxDm();
                fplxmc = FplxEnum.PP.getFplxMc(FplxEnum.PP.getIndex());
            }else if(fpdm.substring(0, 1).equals("0")&&
                    (fpdm.substring(10, 12).equals("17"))){//发票代码编码规则调整为：第1位为0，第11-12位为17
                fplxdm = FplxEnum.ESC.getFplxDm();
                fplxmc = FplxEnum.ESC.getFplxMc(FplxEnum.ESC.getIndex());
            }else {
                fplxdm = null;
                fplxdm = null;
            }
        }else if(fpdm.length() == 10){
            if("1".equals(invoiceTypeCode) || "5".equals(invoiceTypeCode)){//10位发票代码：第8位为1或者5
                fplxdm = FplxEnum.ZP.getFplxDm();
                fplxmc = FplxEnum.ZP.getFplxMc(FplxEnum.ZP.getIndex());
            }else if("6".equals(invoiceTypeCode) || "3".equals(invoiceTypeCode)){//10位发票代码：第8位为3或者6
                fplxdm = FplxEnum.PP.getFplxDm();
                fplxmc = FplxEnum.PP.getFplxMc(FplxEnum.PP.getIndex());
            }else {
                fplxdm = null;
                fplxdm = null;
            }
        }else {
            fplxdm = null;
            fplxdm = null;
        }
        return fplxdm;
    }

    public static void main(String[] args) {
//        String n = InvoiceUtil.getFplx("3302164160");
//        System.out.println(n);

       // System.out.println(InvoiceUtil.isInvDateCorrect("1991-02-02"));
       // System.out.println(InvoiceUtil.isIncomeMonthCorrect("1991-02-02"));


        System.out.println(InvoiceUtil.isIncomeMonthCorrect("202005","2020-04-15"));
    }


}
