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


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

module.factory('TreeDeptUserService', function(LinkerCache, linkerConfig) {

  function TreeDeptUserService(config) {
    this.treeUsers = []; // 树上的用户
    this.treeDepts = []; // 树上的部门
    this.treeDeptMap = {};
    this.notTreeUserIds = '';
    this.notTreeDeptIds = '';
    this.config = config;

    var self = this;
    this.treeDepts.pushAll = function(depts) {
      depts = depts ? depts : [];
      if (Object.prototype.toString.call(depts) !== '[object Array]') { 
        depts = [depts];
      }
      _.each(depts, function(dept) {
        self.setFilterAllDept(dept);
      });
    };
  }

  TreeDeptUserService.prototype.addTopDept = function() {
    var depts = LinkerCache.getAllDepts();
    var self = this;
    _.each(depts, function(dept) {
      if (dept.deptId === linkerConfig.topDeptId) {
        self.treeDepts.pushAll(dept);
      }
    });
  };

  TreeDeptUserService.prototype.filterUserDept = function() {
    if (this.treeUsers.length > 0) {
      return ;
    }

    var selectType = this.config.selectType;
    if (selectType.user) {
      this.filterAllUser(this.config.filterFn);
      this.addTopDept();
    } else if (selectType.dept) {
      this.filterAllDept(this.config.filterFn);
      this.addTopDept();
    }
  };

  TreeDeptUserService.prototype.setFilterAllDept = function(dept) {
    if (dept && !this.treeDeptMap[dept.deptId]) {
      this.treeDeptMap[dept.deptId] = dept;
      this.treeDepts.push(dept);
    }
  };

  TreeDeptUserService.prototype.filterAllUser = function(filterFn) {
    var allUsers = LinkerCache.getAllUsers();
    var self = this;
    _.each(allUsers, function(user) {
      var depts = LinkerCache.getDeptSync(user.deptIds);
      var allDepts = LinkerCache.getAllDeptSync(user.deptIds);
      if (filterFn(user, depts, allDepts)) {
        self.treeUsers.push(user);
        self.treeDepts.pushAll(allDepts);
      } else {
        self.notTreeUserIds = self.notTreeUserIds + ',|' + user.userId + '|';
      }
    });
  };

  TreeDeptUserService.prototype.filterAllDept = function(filterFn) {
    var self = this;
    var depts = LinkerCache.getAllDepts();
    _.each(depts, function(dept) {
      var allDepts = LinkerCache.getAllDeptSync(dept.deptId);
      if (filterFn(dept, allDepts)) {
        self.treeDepts.pushAll(allDepts);
      } else {
        self.notTreeDeptIds = self.notTreeDeptIds + ',|' + dept.deptId + '|';
      }
    });
  };

  /**
  * 获取指定部门下面的部门信息
  *
  * @param {String} 部门ID
  */    
  TreeDeptUserService.prototype.getSubDept = function(deptId) {
    var subDepts = [];
    _.each(this.treeDepts, function(treeDept) {
      if (treeDept.superId === deptId) {
        subDepts.push(treeDept);
      }
    });
    return subDepts;
  };

  TreeDeptUserService.prototype.getSubUser = function(deptId) {
    var subUsers= [];
    _.each(this.treeUsers, function(treeUser) {
      if (!treeUser.deptId) { return; }
      _.each(treeUser.deptId.split(','), function(value, index) {
        if (value === deptId) {
          if(treeUser.orderNo) {
            treeUser.orderValue = parseInt(treeUser.orderNo.split(',')[index]);
          }
          subUsers.push(treeUser);
        }
      });
    });
    return subUsers;
  };

  TreeDeptUserService.prototype.getDeptByDeptId = function(deptId) {
    var dept;
    _.each(this.treeDepts, function(treeDept) {
      if (treeDept.deptId === deptId) {
        dept = treeDept;
      }
    });
    return dept;
  };

  /**
   * 获取指定id的部门的人员树
   *
   * @param {String} deptId 部门id
   * @param {Config} config 获取配置信息
   *
   * @return {Promise}
   */
  TreeDeptUserService.prototype.getSubUserDept = function(deptId) {
    this.filterUserDept();
    var dept = this.getDeptByDeptId(deptId);
    var users = [], depts = [];
    if (this.config.selectType.user) {
      _.each(this.getSubUser(deptId), function(user) {
        users.push(user);
      });
    }

    _.each(this.getSubDept(deptId), function(dept) {
      depts.push(dept);
    });

    dept.users = users;
    dept.depts = depts;
    return dept;
  };

  return TreeDeptUserService;
});
