package com.brilliance.isc.common.transaction.newcheck.bopset.rmbbop;

import com.brilliance.isc.bo.Cnycty;
import com.brilliance.isc.common.rmbbop.vo.Rmb2122;
import com.brilliance.isc.common.transaction.newcheck.AbstractModuleValidator;
import com.brilliance.isc.common.transaction.newcheck.ValidResult;
import com.brilliance.isc.common.transaction.newcheck.Validate;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.mda.dao.BchMapper;
import com.brilliance.isc.mda.dao.CnyctyMapper;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Rmb2122Validator extends AbstractModuleValidator<Rmb2122> {


    @Autowired
    private CnyctyMapper cnyctyMapper;

    @Autowired
    private BchMapper bchMapper;

    @Validate(path="rmbbop.rmb2122.recgrp.ads.vouchercountrycode")
    public ValidResult checkvouchercountrycodeN1000(Rmb2122 rmb2122) {
        String countryCod = rmb2122.getRecgrp().getAds().getVouchercountrycode();
        if(StringUtils.isEmpty(countryCod)){
            return errorMandatory();
        }else {
            Cnycty cnycty = cnyctyMapper.getCnyctyByCode(countryCod);
            if(MdaUtils.isEmpty(cnycty)){
                return ValidResult.build(false, "请输入正确的担保人国别cod");
            }
            return null;
        }
    }


    @Validate(path="rmbbop.rmb2122.recgrp.ads.voucheecountrycode")
    public ValidResult checkvoucheecountrycodeN1000(Rmb2122 rmb2122) {
        String countryCod = rmb2122.getRecgrp().getAds().getVoucheecountrycode();
        if(StringUtils.isEmpty(countryCod)){
            return errorMandatory();
        }else {
            Cnycty cnycty = cnyctyMapper.getCnyctyByCode(countryCod);
            if(MdaUtils.isEmpty(cnycty)){
                return ValidResult.build(false, "请输入正确的担保权人国别cod");
            }
            return null;
        }
    }

    @Validate(path="rmbbop.rmb2122.recgrp.ads.debtorcountrycode")
    public ValidResult checkdebtorcountrycodeN1000(Rmb2122 rmb2122) {
        String countryCod = rmb2122.getRecgrp().getAds().getDebtorcountrycode();
        if(StringUtils.isEmpty(countryCod)){
            return errorMandatory();
        }else {
            Cnycty cnycty = cnyctyMapper.getCnyctyByCode(countryCod);
            if(MdaUtils.isEmpty(cnycty)){
                return ValidResult.build(false, "请输入正确的债务人国别cod");
            }
            return null;
        }
    }
    @Validate(path="rmbbop.rmb2122.recgrp.ads.propertycountrycode")
    public ValidResult checkPropertycountrycodeN1000(Rmb2122 rmb2122) {
        String propertycountrycode = rmb2122.getRecgrp().getAds().getPropertycountrycode();
        if(StringUtils.isEmpty(propertycountrycode)){
            return null;
        }else {
            Cnycty cnycty = cnyctyMapper.getCnyctyByCode(propertycountrycode);
            if(MdaUtils.isEmpty(cnycty)){
                return ValidResult.build(false, "请输入正确的担保物权登记国别cod");
            }
            return null;
        }
    }



    @Validate(path="rmbbop.rmb2122.recgrp.ads.reversecountrycode")
    public ValidResult checkreversecountrycodeN1000(Rmb2122 rmb2122) {
        String reversecountrycode = rmb2122.getRecgrp().getAds().getReversecountrycode();
        if(StringUtils.isEmpty(reversecountrycode)){
            return null;
        }else {
            Cnycty cnycty = cnyctyMapper.getCnyctyByCode(reversecountrycode);
            if(MdaUtils.isEmpty(cnycty)){
                return ValidResult.build(false, "请输入正确的反担保人国别cod");
            }
            return null;
        }
    }


    @Validate(path="rmbbop.rmb2122.recgrp.ads.voucherswiftbic")
    public ValidResult checkvoucherswiftbicN1000(Rmb2122 rmb2122) {
        //担保权人国别为中国，担保权人类型为IB03\\04时，填写大额行别，否则填写9位组织机构代码或18位统一社会信用代码, 担保权人国别为非中国且担保权人类型为A301或302时填写SWIFT BIC，否则选填，可填写特殊机构赋码或LEI码
        //担保人国别为中国，担保人类型为IB03\\04时，填写大额行别，否则填写9位组织机构代码或18位统一社会信用代码, 担保人国别为非中国且担保人类型为A301或302时填写SWIFT BIC，否则选填，可填写特殊机构赋码或LEI码
        //#if PBLMOD\\DECTYP = "A" or PBLMOD\\DECTYP = "M" then
        if( ! MdaUtils.isEmpty(rmb2122.getRecgrp().getAds().getVouchercountrycode()) )
        {
            if( MdaUtils.compareTo(rmb2122.getRecgrp().getAds().getVouchercountrycode(),"CHN") == 0 )
            {
                if( MdaUtils.isEmpty(rmb2122.getRecgrp().getAds().getVoucherswiftbic()) )
                {
                    return errorMandatory();
                }
                else
                {
                    if( ! MdaUtils.isEmpty(rmb2122.getRecgrp().getAds().getVouchertype()) )
                    {
                        if( MdaUtils.compareTo(rmb2122.getRecgrp().getAds().getVouchertype(),"IB03") == 0 || MdaUtils.compareTo(rmb2122.getRecgrp().getAds().getVouchertype(),"IB04") == 0 )
                        {
                            if( Strings.len(rmb2122.getRecgrp().getAds().getVoucherswiftbic()) != 12 )
                            {
                                return ValidResult.build(false, "请填写长度为12位的大额行号");
                            }else {
                               String voucherswiftbic = rmb2122.getRecgrp().getAds().getVoucherswiftbic();
                                int bch = bchMapper.selectByLhanum(voucherswiftbic);
                                if(MdaUtils.compareTo(bch,0) == 0){
                                    return ValidResult.build(false, "请输入正确的12位的大额行号");
                                }else {
                                    return null;
                                }
                            }
                        }
                        else
                        {
                            if( Strings.len(rmb2122.getRecgrp().getAds().getVoucherswiftbic()) == 9 || Strings.len(rmb2122.getRecgrp().getAds().getVoucherswiftbic()) == 18  )
                            {
                                return null;
                            }else {
                                return ValidResult.build(false, "请填写9位组织机构代码或18位统一社会信用代码");
                            }
                        }
                    }else {
                        return ValidResult.build(false, "请填写担保人类型");
                    }
                }
            }
            else
            {
                if( MdaUtils.compareTo(rmb2122.getRecgrp().getAds().getVouchertype(),"A301") == 0 || MdaUtils.compareTo(rmb2122.getRecgrp().getAds().getVouchertype(),"A302") == 0 )
                {
                    if( this.checkRMBBIC(rmb2122.getRecgrp().getAds().getVoucherswiftbic()) )
                    {
                        return null;
                    }
                    else
                    {
                        return ValidResult.build(false, "请正确填写11位 SWIFT BIC");
                    }
                }else
                {
                    return null;
                }
            }
        }
            return ValidResult.build(false, "请先填写国别");

    }

    @Validate(path="rmbbop.rmb2122.recgrp.ads.voucheeorgcode")
    public ValidResult checkvoucheeorgcodeN1000(Rmb2122 rmb2122) {
        //担保权人国别为中国，担保权人类型为IB03\\04时，填写大额行别，否则填写9位组织机构代码或18位统一社会信用代码, 担保权人国别为非中国且担保权人类型为A301或302时填写SWIFT BIC，否则选填，可填写特殊机构赋码或LEI码
        //担保人国别为中国，担保人类型为IB03\\04时，填写大额行别，否则填写9位组织机构代码或18位统一社会信用代码, 担保人国别为非中国且担保人类型为A301或302时填写SWIFT BIC，否则选填，可填写特殊机构赋码或LEI码
        //#if PBLMOD\\DECTYP = "A" or PBLMOD\\DECTYP = "M" then
        if( ! MdaUtils.isEmpty(rmb2122.getRecgrp().getAds().getVoucheecountrycode()) )
        {
            if( MdaUtils.compareTo(rmb2122.getRecgrp().getAds().getVoucheecountrycode(),"CHN") == 0 )
            {
                if( MdaUtils.isEmpty(rmb2122.getRecgrp().getAds().getVoucheeorgcode()) )
                {
                    return errorMandatory();
                }
                else
                {
                    if( ! MdaUtils.isEmpty(rmb2122.getRecgrp().getAds().getVoucheetype()) )
                    {
                        if( MdaUtils.compareTo(rmb2122.getRecgrp().getAds().getVoucheetype(),"IB03") == 0 || MdaUtils.compareTo(rmb2122.getRecgrp().getAds().getVoucheetype(),"IB04") == 0 )
                        {
                            if( Strings.len(rmb2122.getRecgrp().getAds().getVoucheeorgcode()) != 12 )
                            {
                                return ValidResult.build(false, "请填写长度为12位的大额行号");
                            }else {
                                String voucheeorgcode = rmb2122.getRecgrp().getAds().getVoucheeorgcode();
                                int bch = bchMapper.selectByLhanum(voucheeorgcode);
                                if(MdaUtils.compareTo(bch,0) == 0){
                                    return ValidResult.build(false, "请输入正确的12位的大额行号");
                                }else {
                                    return null;
                                }
                            }
                        }
                        else
                        {
                            if( Strings.len(rmb2122.getRecgrp().getAds().getVoucheeorgcode()) == 9 || Strings.len(rmb2122.getRecgrp().getAds().getVoucheeorgcode()) == 18  )
                            {
                                return null;
                            }else {
                                return ValidResult.build(false, "请填写9位组织机构代码或18位统一社会信用代码");
                            }
                        }
                    }else {
                        return ValidResult.build(false, "请填写担保权人类型");
                    }
                }
            }
            else
            {
                if( MdaUtils.compareTo(rmb2122.getRecgrp().getAds().getVoucheetype(),"A301") == 0 || MdaUtils.compareTo(rmb2122.getRecgrp().getAds().getVoucheetype(),"A302") == 0 )
                {
                    if( this.checkRMBBIC(rmb2122.getRecgrp().getAds().getVoucheeorgcode()) )
                    {
                        return null;
                    }
                    else
                    {
                        return ValidResult.build(false, "请正确填写11位 SWIFT BIC");
                    }
                }else
                {
                    return null;
                }
            }
        }

        return ValidResult.build(false, "请先填写国别");
    }






    public static boolean checkRMBBIC(String argBIC)
    {

        //! Check if BIC is correct
        //  check if length of BIC is 8 or 11 characters
        if( Strings.len(argBIC) != 8 && Strings.len(argBIC) != 11 && Strings.len(argBIC) > 0 )
        {
            return false;
            //return ; //   ####   goto done
        }
        String bic = Strings.trim(argBIC);
        String let = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String num = "0123456789";
        // position 1-4 Bank-Code    A-Z are allowed
        // position 5-6 Country-Code A-Z are allowed
        int  i = 1;
        for(;i <= 6;i++)
        {
            if( Strings.pos(let,Strings.mid(bic,i,1)) == 0 )
            {
                // if substring is not found, return value is 0
                return false;
            }
        }
        // position 7 Location-Code   A-Z and 2-9 are allowed
        if( Strings.pos(let,Strings.mid(bic,i,1)) == 0 && Strings.pos("23456789",Strings.mid(bic,7,1)) == 0 )
        {
            return false;
        }
        // position 8 Location-Code   A-Z exept O and 0-9 are allowed
        if( Strings.pos("ABCDEFGHIJKLMNPQRSTUVWXYZ",Strings.mid(bic,8,1)) == 0 && Strings.pos(num,Strings.mid(bic,8,1)) == 0 )
        {
            return false;
        }
        // position 9-11 Branch-Code  A-Z and 0-9 are allowed
        if( Strings.len(bic) > 8 )
        {
            for(i = 9;i <= 11;i++)
            {
                if( Strings.pos(let,Strings.mid(bic,i,1)) == 0 && Strings.pos(num,Strings.mid(bic,i,1)) == 0 )
                {
                    return false;
                }
            }
        }
        return true;
        //    ####    label done

    }

}
