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

var module = angular.module('flowmodulebase.services');


/**
* 含有流程模块的按钮组件服务类
*/
module.factory('FlowButtonService', function() {
  var constants = {
    randNum: 4 // 一行几个
  };

  function Button(id, title, className, order, onClick, show, confirm, warrant) {
    this.id = id;
    this.title = title;
    this.className = className || 'ion-grid';
    this.order = order;
    this.onClick = onClick;
    this.show = show || false;
    this._confirm = confirm;
    this.warrant = warrant;
    this.refreshConfirm();
  }

  Button.prototype.refreshConfirm = function() {
    if (this._confirm) {
      var confirmConfig = localStorage['appconfig_forceConfirm_' + this.id];
      this.confirm = confirmConfig !== 'false';
    }
  };

    var editButton = new Button('edit', '详情', 'ion-grid', 0, null, false, null);
    var notionButton = new Button('notion', '意见', 'ion-chatbox-working', 1, null, false, null);
    var sendButton = new Button('send', '发送', 'ion-android-send', 2, null, false, true, true);
    var lxwButton = new Button('lxw', '例行文', 'ion-android-done-all', 3, null, false, true);
    var saveButton = new Button('save', '保存', 'ion-android-archive', 4, null, false, null, true);
    var moreButton = new Button('more', '更多', 'ion-more', 5, null, false, null);
    var deleteButton = new Button('delete', '删除', 'ion-ios-trash', 6, null, false, true);
    var approveButton = new Button('approve', '审批通过', 'ion-ios-checkmark', 7, null, false, true, true);
    var notApproveButton = new Button('notApprove', '审批不通过', 'ion-ios-close', 8, null, false, true, true);
    var flowLogButton = new Button('flowLog', '流程记录', 'ion-ios-recording', 9, null, false, null);
    var flowDrawButton = new Button('flowDraw', '流程图', 'ion-shuffle', 10, null, false, null);
    var transferButton = new Button('transfer', '转办', 'ion-arrow-return-right', 11, null, false, true, true);
    var recycleButton = new Button('recycle', '收回重办', 'ion-reply', 12, null, false, true, true);
    var finishButton = new Button('finish', '结束流程', 'ion-ios-checkmark', 13, null, false, true, true);
    var recoveryProcessButton = new Button('recoveryProcess', '恢复流程', 'ion-refresh', 14, null, false, true, true);
    var cancelDoButton = new Button('cancelDo', '撤办', 'ion-reply-all', 15, null, false, true, true);
    var reuseProcessButton = new Button('reuseProcess', '重启流程', 'ion-share', 16, null, false, true, true);
    var dispenseButton = new Button('dispense', '内部分发', 'ion-share', 17, null, false, true, true);

  var buttons = [editButton, saveButton, sendButton, notionButton, moreButton, flowLogButton, deleteButton,
    flowDrawButton, transferButton, recycleButton, finishButton, recoveryProcessButton,
    cancelDoButton, reuseProcessButton, approveButton, notApproveButton, dispenseButton, lxwButton];

  var buttonObject = {};

  /**
  * 初始化方法
  */
  function init() {
    initButtonObject();
  }
  init();

  function initButtonObject() {
    _.forEach(buttons, function(value) {
      buttonObject[value.id] = value;
    });
    return buttonObject;
  }

  function refreshButtons() {
    for (var i = 0, len = buttons.length; i < len; i++) {
      buttons[i].refreshConfirm();
    }
  }

  function initDefaultClickEvent(scope) {
    flowLogButton.onClick = scope.openFlowLog;
    flowDrawButton.onClick = scope.openWorkflowDrawing;
    recycleButton.onClick = scope.recycle;
    reuseProcessButton.onClick = scope.reuseProcess;
    recoveryProcessButton.onClick = scope.recoveryProcess;
    cancelDoButton.onClick = scope.cancelDo;
    dispenseButton.onClick = scope.dispense;
  }

  function FlowButtonService(config, scope) {
    //initDefaultClickEvent(scope);
    this.showButtons = [];
    this.moreButton = moreButton;
    //this.refresh(config);
  }

  FlowButtonService.prototype = {
    refreshButtons: refreshButtons,
    refresh: function(config) {
      this.showButtons = [];
      var that = this;
      if (config) {
        _.forEach(config.commonButtons, function(value) {
          that.addButton(value);
        });
      }
      this.refreshMore(this.showButtons);
    },

    refreshMore: function () {
      if (this.showButtons.length > constants.randNum) {
        this.moreButton.show = true;
      } else {
        this.moreButton.show = false;
      }
    },

    addButton: function(buttonConfig) {
      if (!buttonConfig.show) { return; }
      var buttonOrigin = buttonObject[buttonConfig.id];
      if (buttonOrigin)  {
        var button = _.copy(buttonOrigin);
        button.show = buttonConfig.show;
        button.onClick = buttonConfig.onClick ? buttonConfig.onClick : buttonOrigin.onClick;
        button.confirm = buttonConfig.confirm ? buttonConfig.confirm : buttonOrigin.confirm;
        button.title = buttonConfig.title ? buttonConfig.title : buttonOrigin.title;
        button.warrant = buttonConfig.warrant || buttonOrigin.warrant;
      } else{
        button = new Button(buttonConfig.id, buttonConfig.title, buttonConfig.className,
        buttonConfig.order, buttonConfig.onClick, buttonConfig.show, buttonConfig.confirm, buttonConfig.warrant);
      }
      this.showButtons.push(button);
      this.refreshMore();

    },

    clear: function() {
      this.showButtons = [];
      this.moreButton.show = false;
    }

  };

  return FlowButtonService;
});
