package com.dxhy.portrait.business.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import com.dxhy.common.enums.FplxEnum;

/**
 * @author dxhy
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
public class InvoiceUtil {
    public static final String HD =
        "111001571071,111001571072,131001570151,133011501118,133020691510,133021591000,134021522401,144031539110";
    private static final Pattern FPDM10_PATTERN = Pattern.compile("\\d{10}");
    private static final Pattern FPDM12_PATTERN = Pattern.compile("\\d{12}");
    private static final Pattern FPHM8_PATTERN = Pattern.compile("\\d{8}");
    private static final Pattern FPHM10_PATTERN = Pattern.compile("\\d{10}");
    private static final Pattern MONEY_PATTERN = Pattern.compile(
        "(^(([1-9]+\\d*)|(0{1}))(\\.\\d{1,2})?$)|(^-([1-9]+\\d*(\\.\\d{1,2})?|0\\.(0[1-9]{1}|[1-9]{1}\\d?))$)");
    private static String fplxdm;
    private static String fplxmc;
    private String fpdm;
    private String fphm;
    private String kprq;
    private String je;
    private String se;
    private String jym;

    public InvoiceUtil() {}

    public InvoiceUtil(String fpdm) {
        if ((fpdm != null) && (fpdm.length() >= 10)) {
            this.fpdm = fpdm;
            getFplx(fpdm);
        }
    }

    private void getFplx(String fpdm) {
        String invoiceTypeCode = "";

        invoiceTypeCode = fpdm.substring(7, 8);
        if (fpdm.length() == 12) {
            if ("111001571071,111001571072,131001570151,133011501118,133020691510,133021591000,134021522401,144031539110"
                .contains(fpdm)) {
                fplxdm = FplxEnum.DZP.getFplxDm();
                fplxmc = FplxEnum.DZP.getFplxMc(FplxEnum.DZP.getIndex());
            } else if (("0".equals(fpdm.substring(0, 1))) && ("11".equals(fpdm.substring(10, 12)))) {
                fplxdm = FplxEnum.DZP.getFplxDm();
                fplxmc = FplxEnum.DZP.getFplxMc(FplxEnum.DZP.getIndex());
            } else if (("0".equals(fpdm.substring(0, 1))) && ("12".equals(fpdm.substring(10, 12)))) {
                fplxdm = FplxEnum.TXF.getFplxDm();
                fplxmc = FplxEnum.TXF.getFplxMc(FplxEnum.TXF.getIndex());
            } else if ((!"0".equals(fpdm.substring(0, 1))) && ("2".equals(invoiceTypeCode))) {
                fplxdm = FplxEnum.JDC.getFplxDm();
                fplxmc = FplxEnum.JDC.getFplxMc(FplxEnum.JDC.getIndex());
            } else if (("0".equals(fpdm.substring(0, 1)))
                && (Arrays.asList("06", "07").contains(fpdm.substring(10, 12)))) {
                fplxdm = FplxEnum.JSP.getFplxDm();
                fplxmc = FplxEnum.JSP.getFplxMc(FplxEnum.JSP.getIndex());
            } else if (("0".equals(fpdm.substring(0, 1)))
                && (Arrays.asList("04", "05").contains(fpdm.substring(10, 12)))) {
                fplxdm = FplxEnum.PP.getFplxDm();
                fplxmc = FplxEnum.PP.getFplxMc(FplxEnum.PP.getIndex());
            } else {
                fplxdm = null;
                fplxdm = null;
            }
        } else if (fpdm.length() == 10) {
            if (("1".equals(invoiceTypeCode)) || ("5".equals(invoiceTypeCode))) {
                fplxdm = FplxEnum.ZP.getFplxDm();
                fplxmc = FplxEnum.ZP.getFplxMc(FplxEnum.ZP.getIndex());
            } else if (("6".equals(invoiceTypeCode)) || ("3".equals(invoiceTypeCode))) {
                fplxdm = FplxEnum.PP.getFplxDm();
                fplxmc = FplxEnum.PP.getFplxMc(FplxEnum.PP.getIndex());
            } else {
                fplxdm = null;
                fplxdm = null;
            }
        } else {
            fplxdm = null;
            fplxdm = null;
        }
    }

    public boolean fpdmValid() {
        boolean valid = false;
        if (StringUtils.isEmpty(this.fpdm)) {
            return false;
        }
        if ((FPDM10_PATTERN.matcher(this.fpdm).matches()) || (FPDM12_PATTERN.matcher(this.fpdm).matches())) {
            valid = true;
        }
        return valid;
    }

    public boolean fphmValid() {
        boolean valid = false;
        if (StringUtils.isEmpty(this.fphm)) {
            return false;
        }
        if ((FPHM8_PATTERN.matcher(this.fphm).matches()) || (FPHM10_PATTERN.matcher(this.fphm).matches())) {
            valid = true;
        }
        return valid;
    }

    public boolean jeValid() {
        boolean valid = false;
        if ((!StringUtils.isBlank(this.je)) && (MONEY_PATTERN.matcher(this.je).matches())) {
            valid = true;
        }
        return valid;
    }

    public boolean seValid() {
        boolean valid = false;
        if ((!StringUtils.isBlank(this.se)) && (MONEY_PATTERN.matcher(this.se).matches())) {
            valid = true;
        }
        return valid;
    }

    public boolean kprqValid(String dataFormat) {
        boolean valid = true;
        SimpleDateFormat format = null;
        if (dataFormat == null) {
            format = new SimpleDateFormat("yyyy-MM-dd");
        } else {
            format = new SimpleDateFormat(dataFormat);
        }
        try {
            format.parse(this.kprq);
        } catch (ParseException e) {
            valid = false;
        }
        return valid;
    }

    public boolean jymValid() {
        boolean valid = false;
        if ((!StringUtils.isBlank(this.jym)) && (this.jym.length() >= 6)) {
            valid = true;
        }
        return valid;
    }

    public String getFplxdm() {
        return fplxdm;
    }

    public String getFplxmc() {
        return fplxmc;
    }

    public String getFpdm() {
        return this.fpdm;
    }

    public void setFpdm(String fpdm) {
        this.fpdm = fpdm;
        getFplx(fpdm);
    }

    public String getFphm() {
        return this.fphm;
    }

    public void setFphm(String fphm) {
        this.fphm = fphm;
    }

    public String getKprq() {
        return this.kprq;
    }

    public void setKprq(String kprq) {
        this.kprq = kprq;
    }

    public String getJe() {
        return this.je;
    }

    public void setJe(String je) {
        this.je = je;
    }

    public String getSe() {
        return this.se;
    }

    public void setSe(String se) {
        this.se = se;
    }

    public String getJym() {
        return this.jym;
    }

    public void setJym(String jym) {
        this.jym = jym;
    }
}
