import BigNumber from 'bignumber.js';
import moment from 'moment';

const  BopRules = window.BopRules

  function mergeRules(ruleA,ruleB){
    let rules = {}
    Object.assign(rules,ruleA)
  
    for(let key in ruleB){
      if(rules[key]){
        rules[key] =  [...rules[key],...ruleB[key]]
      }else{
        rules[key] =  [...ruleB[key]]
      }
    }
    return rules
  }

  function applyCommon(module){
    let rules = {}
    let suffix = module.substring(module.length - 3);
    if(this.model.mtabut.basflg){
      let bas = BopRules.checkRuleOfBAS().call({module:'BAS'+suffix}).call(this,this,moment,BigNumber)
      rules = bas;
    }
    if(this.model.mtabut.vrfflg){
      let vrf = BopRules.checkRuleOfVRF().call({module:'VRF'+suffix}).call(this,this,moment,BigNumber)
      rules = mergeRules(rules,vrf);
    }
    if(this.model.mtabut.dclflg){
      let dcl = BopRules.checkRuleOfDCL().call({module:'DCL'+suffix}).call(this,this,moment,BigNumber)
      rules = mergeRules(rules,dcl);
    }
    let mtabut = BopRules.checkRuleOfMTABUT().call({module:'MTABUT'+suffix}).call(this,this,moment,BigNumber)
    rules = mergeRules(rules,mtabut);
    return rules
  }

  BopRules.checkRuleOfdbaaddame=()=>function(module='DBAADD'){
    let rules = applyCommon.call(this,module)
    let self = BopRules.checkRuleOfDBA().call({module}).call(this,this,moment,BigNumber)
    rules = mergeRules(rules,self);
    return rules
  }

  BopRules.checkRuleOfdbbaddame=()=>function(module='DBBADD'){
    let rules = applyCommon.call(this,module)
    let self = BopRules.checkRuleOfDBB().call({module}).call(this,this,moment,BigNumber)
    rules = mergeRules(rules,self);
    return rules
  }

  BopRules.checkRuleOfdbcaddame=()=>function(module='DBCADD'){
    let rules = applyCommon.call(this,module)
    let self = BopRules.checkRuleOfDBC().call({module}).call(this,this,moment,BigNumber)
    rules = mergeRules(rules,self);
    return rules
  }

  BopRules.checkRuleOfdbdaddame=()=>function(module='DBDADD'){
    let rules = applyCommon.call(this,module)
    let self = BopRules.checkRuleOfDBD().call({module}).call(this,this,moment,BigNumber)
    rules = mergeRules(rules,self);
    return rules
  }

  BopRules.checkRuleOfdbeaddame=()=>function(module='DBEADD'){
    let rules = applyCommon.call(this,module)
    let self = BopRules.checkRuleOfDBE().call({module}).call(this,this,moment,BigNumber)
    rules = mergeRules(rules,self);
    return rules
  }

  BopRules.checkRuleOfdbfaddame=()=>function(module='DBFADD'){
    let rules = applyCommon.call(this,module)
    let self = BopRules.checkRuleOfDBF().call({module}).call(this,this,moment,BigNumber)
    rules = mergeRules(rules,self);
    return rules
  }


/*此处用于复用其他申报交易的函数
* 方式为动态构建一个this,将返回的规则路径映射为业务交易下的路径
*/
function convertSingle(bopFirstName,recName,buildFunc){
  var bopRecgrpName = recName.toLowerCase() + "grp";
  var trnName = recName.toLowerCase()+"add"
  //构建基础this对象
  var thiz = {
      model:{
          recgrp:this.model[bopFirstName][bopRecgrpName],
          mtabut:this.model[bopFirstName],
      },
      trnName:trnName,
      root:this,
      '$smsgbox':this.$smsgbox,
      '$msgbox':this.$msgbox,
      isCom:true,
      bopCode:bopFirstName,
      '$Api':this.$Api
      };
  var rules = buildFunc().call(thiz,recName.toUpperCase()+"COM");
  //重组rules,以解决key的问题
  var newRules = {}
  for(var key in rules){
      var newKey = key;
      if(key.startsWith("recgrp.")){
          newKey = bopFirstName + "." + bopRecgrpName + key.substring(6)
      }else if(key.startsWith("mtabut.")){
          newKey = bopFirstName + key.substring(6)
      }
      //新key赋值
      newRules[newKey] = rules[key]
  }
  return newRules
}  
  //映射组件模型下的规则
/*
BOPGAT DBA DBD     涉外/境内收入
BOPREM DBB DBE     境外/境内汇款
BOPPAY DBC DBF     对外/境内付款承兑
 */
  BopRules.checkRuleOfbopcoms=()=>function(bopName,recName){
    var buildFunc = null;
    switch (recName) {
        case 'DBA':buildFunc = BopRules.checkRuleOfdbaaddame;break;
        case 'DBD':buildFunc = BopRules.checkRuleOfdbdaddame;break;
        case 'DBB':buildFunc = BopRules.checkRuleOfdbbaddame;break;
        case 'DBE':buildFunc = BopRules.checkRuleOfdbeaddame;break;
        case 'DBC':buildFunc = BopRules.checkRuleOfdbcaddame;break;
        case 'DBF':buildFunc = BopRules.checkRuleOfdbfaddame;break;
    }
    var rules = convertSingle.call(this,bopName,recName,buildFunc);
    return rules
  }