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

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

  module.factory('BaseAuthService', function() {

    function BaseAuthService(base, subflag, creUserId){
      this.base = base;
      this.subflag = subflag;
      this.creUserId = creUserId;
      this.workflow = base.workflow || {};
      this.currentUser = base.loginUser;
      this.record = base.record;
    }

    BaseAuthService.prototype = {

      /**
       * 判断流程状态是否为草稿
       * @return {Boolean}
       */
      isDraft: function() {
        var subflag = this.subflag || '0';
        return subflag === '0';
      },

      /**
       * 判断流程状态是否为流程中
       * @return {Boolean}
       */
      isProcessing: function() {
        var subflag = this.subflag || '0';
        return subflag === '1';
      },

      /**
       * 判断流程状态是否结束
       * @return {Boolean}
       */
      isEnd: function() {
        var subflag = this.subflag || '0';
        return subflag === '2' || subflag === '3';
      },

      /**
       * 是否是撤办状态。
       * @return {Boolean}
       */
      isCancelDo: function() {
        return this.subflag === '5';
      },

      isAdmin: function() {
        return this.currentUser.userId === '1';
      },

      /**
       * 登录人是否是创建人
       * @return {Boolean}
       */
      isCreator: function(){
        return this.creUserId === this.currentUser.userId;
      },

      /**
       * 判断是否为当前办理人
       * @return {Boolean}
       */
      isWriter: function() {
        var writeUserId = this.workflow.writeUserId || '';
            return writeUserId.indexOf(this.currentUser.userId) !== -1;
      },

      /**
       * 判断这个按钮是否能用
       * @param  {string}  buttonId 按钮id
       * @return {Boolean}
       */
      isInAuth: function(buttonId) {
        var buttonAuth = this.workflow.buttonAuth;
        if (buttonAuth) {
            return !!buttonAuth[buttonId];
        } else {
            return false;
        }
      },

      /**
       * 判断是否已经填写过意见
       * 或者已经有临时签字
       */
      hasWrittenSign: function() {
        var signField = this.workflow.currentSignField,
          signProperty = signField;
        var tempSign = this.workflow.currentSignValue;

        if (signProperty) {
          signProperty = signProperty.toLowerCase().replace(/_(\w)/g, function(match, firstLatter) {
            return firstLatter.toUpperCase();
          });
        }

        return (signProperty && !!this.record[signProperty]) || tempSign;
      },

      needSign: function() {
        return !this.hasWrittenSign();
      }
    };
    return BaseAuthService;
  });
