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

var module = angular.module('flow.config');

/**
* 工作流配置
*
* 配置参数：
* flowType {String} 流程类型
* moreOrder {MoreObject} 多选的状态
* 
* 参数解释

* MoreObject: {
*    'tongshiban': {Boolean} 同时办显示状态 true显示，false不显示，默认true
*    'shunxuban': {Boolean} 顺序办显示状态，true显示 false不显示 默认true
*  }
*/
module.factory('WorkflowConfig', function() {
  var workflowConfigs = [];

  var workflowConfigObject = {};

  return {
    setConfig: function(workflowConfig) {
      this.setMoreOrderDefaultValue(workflowConfig);
      this.removeWorkflowConfig(workflowConfig);
      workflowConfigs.push(workflowConfig);
      workflowConfigObject[workflowConfig.flowType] = workflowConfig;
    },

    setMoreOrderDefaultValue: function(workflowConfig) {
      if (workflowConfig.moreOrder) {
        if (workflowConfig.moreOrder.shunxuban !== false) {
          workflowConfig.moreOrder.shunxuban = true;
        } 
        if (workflowConfig.moreOrder.tongshiban !== false) {
          workflowConfig.moreOrder.tongshiban = true;
        }
      }
    },

    removeWorkflowConfig: function(workflowConfig) {
      var keyValue;
      _.each(workflowConfigs, function(value, key) {
        if (value.flowType === workflowConfig.flowType) {
          keyValue = key;
        }
      });
      if (keyValue) {
        workflowConfigs.splice(keyValue, 1);
      }
    },

    getConfigs: function() {
      return workflowConfigs;
    },

    getConfig: function(flowType) {
      return workflowConfigObject[flowType];
    }
  };
});
