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

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

var TransferFlag = {
  'yes': '1',
  'no': '0'
};

/**
 * 工作流请求中的各个参数设置
 */
module.factory('WorkflowRequest', function () {
  function WorkflowRequest(params, notions, transferflag, cruUser) {
    _.apply(this, params);
    this.setNotions(notions);
    this.setDeptId(cruUser);
    this.setTransferflag(transferflag);
    if (params.needSign !== false || (this.idea && !/^(\^\#\^)+$/.test(this.idea))) {
      //已填写临时意见，则生成新的签名
      this.setSign(transferflag, cruUser, notions);
    }
    delete this.needSign;
  }

  WorkflowRequest.prototype = {
    setNotions: function (notions) {
      this.idea = _.map(notions, function (message) {
        return WorkflowRequest.transferStr(message).replace(/'/g, '’');
      }).join('^#^');
    },

    setTransferflag: function (transferflag, cruUser, notions) {
      transferflag = transferflag || TransferFlag.no;
      this.transferflag = transferflag;
    },

    setDeptId: function (cruUser) {
      this.deptid = cruUser.noteDeptId;
    },

    setSign: function (transferflag, cruUser, notions) {
      if (transferflag === TransferFlag.yes) {
        this.sign = WorkflowRequest.getSign(cruUser);
        var isNotionEmpty = notions.every(function (notion) { return !_.isEmpty(notion); });
        if (isNotionEmpty) {
          this.sign = 'FALSE';
        }
      } else {
        this.sign = WorkflowRequest.getSign(cruUser);
      }
    }
  };

  var transferWords = /[<>\"\'\%\&\\\^\|\/]/g;

  WorkflowRequest.transferStr = function (str) {
    var result = str || '';
    if (str !== null) {
      result = result.replace(transferWords, function (word) {
        return 'chr(' + word.charCodeAt(0) + ')';
      });
    }
    return result;
  };

  /**
   * 获取签字信息。
   *
   * @param {User} cruUser 签字人
   *
   * @return {String} 返回签字信息。
   */
  WorkflowRequest.getSign = function (cruUser) {
    var time = _.formatDate(new Date(), 'Y-m-d'),
      userId = cruUser.userId,
      userName = cruUser.userName,
      deptId = cruUser.noteDeptId,
      deptName = cruUser.noteDeptName;

    return '[' + deptId + '*' + userId + ']' +
      '|' + deptName + '|:|' + userName +
      '|' + time;
  };

  /**
   * 创建结束流程的请求
   *
   * @param  {Object} params
   * @param  {Array} notions 意见
   * @param  {User} cruUser  当前办理人
   * @return {FinishWorkflowRequest}
   */
  WorkflowRequest.generateFinishWorkflowRequest = function (params, notions, cruUser) {
    var request = new WorkflowRequest(params, notions, '0', cruUser);
    var finishWorkflowRequest = {
      flowType: request.filetypeid,
      recordId: request.recordid,
      subflag: request.subflag,
      deptId: request.deptid,
      deptName: WorkflowRequest.getDeptName(cruUser, request.deptid),
      userId: cruUser.userId,
      userName: cruUser.userName,
      title: request.title,
      idea: request.idea,
      flag: params.flag || '0'
    };

    return finishWorkflowRequest;
  };

  WorkflowRequest.getDeptName = function (user, deptId) {
    var deptIds = user.deptId.split(','),
      deptNames = user.deptName.split(','),
      deptName;
    _.each(deptIds, function (_deptId, key) {
      if (deptId === _deptId) {
        deptName = deptNames[key];
      }
    });
    return deptName;
  };

  return WorkflowRequest;
});
