package com.brilliance.isc.common.authority;


import com.brilliance.isc.common.api.bch.service.BchService;
import com.brilliance.isc.common.transaction.vo.BaseTransactionVo;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.common.vo.SimpleModuleVo;
import com.brilliance.isc.constant.ErrorCode;
import com.brilliance.isc.exception.RestBizException;
import com.brilliance.isc.vo.model.Gcdgrp;
import com.brilliance.isc.vo.model.Gidgrp;
import com.brilliance.isc.vo.model.Ncdgrp;
import com.brilliance.isc.vo.model.Nidgrp;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Component
public class TrnCodeAuthorityComplex implements ApplicationContextAware {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final String GIDGRP = "gidgrp";
    private final String GCDGRP = "gcdgrp";

    private final String NIDGRP = "nidgrp";
    private final String NCDGRP = "ncdgrp";

    private ApplicationContext applicationContext;

    @Resource
    private BchService bchService;

    public void validateTrnCode(String objinr,String objtyp,String inifrm){
       String  frm = inifrm.toUpperCase();
        if(MdaUtils.isEmpty(objinr) || MdaUtils.isEmpty(objtyp)){
            logger.info("无objtype与objinr，无法验证");
            return;
        }
        logger.info("判断是否有交易进入权限，数据-objtyp:{},objinr:{},frm:{}",objinr,objtyp,frm);
        Map<String,TrnCodeAuthorityService> trnCodeMap = applicationContext.getBeansOfType(TrnCodeAuthorityService.class);
        if(trnCodeMap == null ){
            return ;
        }
        trnCodeMap.values().forEach(trnCodeAuthorityService -> {
            if(trnCodeAuthorityService.isTransCodeSupport(frm)){
               String msg = trnCodeAuthorityService.getTrnCodeAuthority(objtyp,objinr,frm);
                if(!MdaUtils.isEmpty(msg)){
                    throw new RestBizException(ErrorCode.BIZ_STATE_CHANGE,"无法进入当前业务交易:"+msg);
                }
            }
        });
    }

    public void validateTrnCode(BaseTransactionVo baseTransactionVo){
        String trnName = baseTransactionVo.getTransName();
        SimpleModuleVo simpleModuleVo = getSimpleVo(baseTransactionVo);
        if(simpleModuleVo == null){
            return;
        }
        validateTrnCode(simpleModuleVo.getObjinr(),simpleModuleVo.getObjtyp(),trnName.toUpperCase());
    }

    public SimpleModuleVo getSimpleVo(BaseTransactionVo baseTransactionVo){
        String transName = baseTransactionVo.getTransName().toUpperCase();
        //索赔登记判断GID
        if("GITCRQ|GITPCR".contains(transName)){
            Gidgrp gidgrp = Utils.getFieldValue(baseTransactionVo,GIDGRP);
            if(gidgrp != null && !MdaUtils.isEmpty(gidgrp.getRec().getInr())){
                return SimpleModuleVo.buildModule(gidgrp.getRec().getInr(),"GID") ;
            }
            return null;
        }
        //索赔登记判断NID
        if("NITCRQ|NITPCR".contains(transName)){
            Nidgrp nidgrp = Utils.getFieldValue(baseTransactionVo,NIDGRP);
            if(nidgrp != null && !MdaUtils.isEmpty(nidgrp.getRec().getInr())){
                return SimpleModuleVo.buildModule(nidgrp.getRec().getInr(),"NID") ;
            }
            return null;
        }

        Gcdgrp gcdgrp = Utils.getFieldValue(baseTransactionVo,GCDGRP);
        if(gcdgrp != null && !MdaUtils.isEmpty(gcdgrp.getRec().getInr())){
            return SimpleModuleVo.buildModule(gcdgrp.getRec().getInr(),"GCD") ;
        }
        Ncdgrp ncdgrp = Utils.getFieldValue(baseTransactionVo,NCDGRP);
        if(ncdgrp != null && !MdaUtils.isEmpty(ncdgrp.getRec().getInr())){
            return SimpleModuleVo.buildModule(ncdgrp.getRec().getInr(),"NCD") ;
        }
        Gidgrp gidgrp = Utils.getFieldValue(baseTransactionVo,GIDGRP);
        if(gidgrp != null && !MdaUtils.isEmpty(gidgrp.getRec().getInr())){
            return SimpleModuleVo.buildModule(gidgrp.getRec().getInr(),"GID") ;

        }
        Nidgrp nidgrp = Utils.getFieldValue(baseTransactionVo,NIDGRP);
        if(nidgrp != null && !MdaUtils.isEmpty(nidgrp.getRec().getInr())){
            return SimpleModuleVo.buildModule(nidgrp.getRec().getInr(),"NID") ;
        }
        logger.info("无法读取objtyp与objinr");
        //相关转换，后续交易继续补充
        return null;
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
