var angular = require('angular');
require('../modules');
require('../../../commons/services/AppConfig');
var _ = require('jsUtil');

angular
  .module('toberead.services')
  .provider('TobereadZFConfig', TobereadZFConfig)
  .factory('TobereadConfig', function(AppConfig, TobereadZFConfig) {
    return TobereadConfig.generateInstance(AppConfig, TobereadZFConfig);
  });

/**
* hiddenZFButtons中存放的是要隐藏转发按钮的模块的流程类型
*/

/** @ngInject */
function TobereadZFConfig() {
  this.hiddenZFButtons = [];

  this.setHiddenZFButtons = function(value) {
    this.hiddenZFButtons = value;
  };

  this.$get = [function() {
    return this;
  }];
}

function TobereadConfig(AppConfig, TobereadZFConfig) {
  this.AppConfig = AppConfig;
  this.configsWithTypeId = {};
  this.configsWithRelationTable = {};
  this.dispenseField = {
    'archives_in': 'dispense',
    'archives_out': 'dispense',
    'archives_sign': 'dispense'
  };
  this.hiddenZFButtons = TobereadZFConfig.hiddenZFButtons;
}

TobereadConfig.prototype = {
  get: function(toberead) {
    var configWithRelationTable = this.configsWithRelationTable[toberead.relationTable],
      configWithTypeId = this.configsWithTypeId[toberead.typeId],
      moduleConfig = this.AppConfig.getModuleConfigByFlowType(toberead.typeId),
      config;

    if (configWithRelationTable) {
      config = configWithRelationTable.get(toberead);
    }
    if (!config && configWithTypeId) {
      config = configWithTypeId.get(toberead);
    }
    if (!config && moduleConfig) {
      config = moduleConfig;
    }

    return config;
  },

  set: function(typeId, configOrConfigGenerator) {
    if (_.isArray(typeId)) {
      for (var i = 0, len = typeId.length; i < len; i++) {
        this.set(typeId[i], configOrConfigGenerator);
      }
    } else {
      if (this.configsWithTypeId[typeId]) {
        console.error('待阅配置重复定义错误：typeId[' + typeId + ']已定义，定义内容为：' + this.configsWithTypeId[typeId]);
      } else {
        this.configsWithTypeId[typeId] =
          {get: this.getConfigGetter(configOrConfigGenerator)};
      }
    }
  },

  getConfigGetter: function(configOrConfigGenerator) {
    var configGetter = _.isString(configOrConfigGenerator)? function() {
      return {
        tobereadFormTplHtml: configOrConfigGenerator
      };
    } : _.isObject(configOrConfigGenerator)? function() {
      return configOrConfigGenerator;
    } : configOrConfigGenerator;
    return configGetter;
  },

  setByRelationTable: function(relationTable, configOrConfigGenerator) {
    var configGetter = this.getConfigGetter(configOrConfigGenerator);
    if (this.configsWithRelationTable[relationTable]) {
      console.error('待阅配置重复定义错误：relationTable[' + relationTable + ']已定义，定义内容为：' + this.configsWithRelationTable[relationTable]);
    } else {
      this.configsWithRelationTable[relationTable] = {get: configGetter};
    }
  },

  getDispenseField: function(tableName) {
    return this.dispenseField[tableName] || 'dispense';
  },

  /**
   * 保存业务表分发记录的字段
   * @param {String} tableName 业务表
   * @param {String} field     保存该业务表中数据内部分发后记录分发信息的业务表的字段名
   */
  setDispenseField: function(tableName, field) {
    if(!field) {return;}
    this.dispenseField[tableName] = field;
  }
};

TobereadConfig.generateInstance = function(AppConfig, TobereadZFConfig) {
  return new TobereadConfig(AppConfig, TobereadZFConfig);
};
