var angular = require('angular'),
  _ = require('jsUtil');
require('../modules');
require('./LinkerCache');
require('./linkerConfig');
require('./LinkerRemoteService');

angular
  .module('linker.services')
  .factory('LinkerService', LinkerService);

/** @ngInject */
function LinkerService(LinkerCache, linkerConfig, LinkerRemoteService, $q, CacheService) {

  var LinkerService = {
    queryUserByIdSync: queryUserByIdSync,
    queryDeptByIdSync: queryDeptByIdSync,
    queryAllDepts: LinkerRemoteService.queryAllDepts,
    getCurrentUser: getCurrentUser,
    setCurrentUser: setCurrentUser,
    logout: logout,
    hasDeptLevelOfCurrentUser: hasDeptLevelOfCurrentUser,
    inCurrentDept: inCurrentDept,
    queryUserById: LinkerRemoteService.queryUserById,
    queryDeptById: LinkerRemoteService.queryDeptById
  };

  var userId = window.localStorage.getItem('userId');
  setCurrentUser(userId);

  function queryUserByIdSync(userId) {
    var users = LinkerCache.getUserSync(userId);
    return users.length > 0 ? users[0] : null;
  }

  /**
   * 根据部门id查询部门信息 同步处理
   * @param  {String} deptId 部门id
   * @return {Promise}
   */
  function queryDeptByIdSync(deptId) {
    var depts = LinkerCache.getDeptSync(deptId);
    return depts.length > 0 ? depts[0] : null;
  }

  function setCurrentUser(userId) {
    LinkerService.currentUserId = userId;
    LinkerService.currentUser = {userId: userId};
    LinkerRemoteService.queryUserById(userId).then(function(user) {
      var deptIdCache = CacheService.get(userId);
      if(deptIdCache) {
        user.noteDeptId = deptIdCache;
        user.noteDeptName = setNoteDeptName(user, deptIdCache);
      }
      LinkerService.currentUser = user;
    });
  }

  /**
   * 获取对应的岗位的名称。
   */
  function setNoteDeptName(user, deptIdCache) {
    var flag = 0;
    var deptNames = user.deptName.split(',');
    _.each(user.deptIds, function(deptId, index) {
      if(deptId === deptIdCache) {
        flag = index;
      }
    });
    return deptNames[flag];
  }

  function getCurrentUser() {
    return LinkerService.currentUserId? LinkerRemoteService.queryUserById(LinkerService.currentUserId).then(function(user) {
      return user;
    }) : $q.when(null);
  }

  function hasDeptLevelOfCurrentUser(deptLevels) {
    var currentUser = LinkerService.currentUser,
      deptIds = currentUser.deptIds,
      depts = LinkerCache.getDeptSync(deptIds);

    if (depts) {
      depts = _.isArray(depts) ? depts : [depts];
      return depts.every(function(dept) {
        return deptLevels.indexOf(dept.deptLevel) !== -1;
      });
    } else {
      return false;
    }
  }

  function logout() {
    delete LinkerService.currentUser;
    delete LinkerService.currentUserId;
  }

  function inCurrentDept(_deptIds) {
    var loginUserDept = LinkerService.currentUser?
      LinkerService.currentUser.deptIds : [],
      deptIds = _deptIds.split(','),
      flag = false;
    for (var i = 0; i < loginUserDept.length; i++) {
      for (var j = 0; j < deptIds.length; j++) {
        if (loginUserDept[i] === deptIds[j]) {
          flag = true;
          break;
        }
      }
      if (flag) {
        break;
      }
    }
    return flag;
  }

  return LinkerService;

}
