var Invoice =  require("./invoice");
var system = require("../../system");
var ContextFactory = require("./contextFactory");
/**
 * 计算类
 * 
 * 组装的Invoice对象
 */
class Calculation{
  
  constructor(params){
    console.log("初始化计算类")
    this.contextFactory = new ContextFactory();
    this.invoice = new Invoice();
    this.init(params);
  }
  
  getInvoice(){
    return this.invoice;
  }

  /**
   * @param {*} params
   * calculation
   * @param.calNames   计算种类 是  增值税：valueAddedTax  附加税：additionalTax
   */
   init (params){
    if(params.calNames instanceof Array){
      for (let item of params.calNames) {
        if(item=="valueAddedTax"){
          this.invoice.setContextValueAddedTax(this.contextFactory.getInstance(system.trim(item)));
        }else if(item=="additionalTax"){
          this.invoice.setContextAdditionalTax(this.contextFactory.getInstance(system.trim(item)));
        }else{
          system.getResult(-1,`参数错误 非法的计算类型`);
        }
      }
    }else {
      if(system.trim(params.calNames) != "valueAddedTax" && system.trim(params.calNames) != "additionalTax" 
      && system.trim(params.calNames) != "individualIncomeTax"){
        system.getResult(-1,`参数错误 非法的计算类型`);
      }else{
        if(params.calNames=="valueAddedTax"){
          this.invoice.setContextValueAddedTax(this.contextFactory.getInstance(system.trim(params.calNames)));
        }else if(params.calNames=="additionalTax"){
          this.invoice.setContextAdditionalTax(this.contextFactory.getInstance(system.trim(params.calNames)));
        }else{
          system.getResult(-1,`参数错误 非法的计算类型`);
        }
      }
    }
  }
}

module.exports=Calculation;