var angular = require('angular');
require('../modules');
require('./userList');
require('./deptList');
require('./userListOfSameDept');

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

module.factory('userDeptList', function(userList, deptList, $q) {

  function sync() {
    return $q.all([deptList.sync(), userList.sync()]);
  }

  function refresh() {
    return $q.all([deptList.refresh(), userList.refresh()]);
  }

  function clear() {
    return $q.all([userList.clear(), deptList.clear()]);
  }

  function isInitSyncData() {
    return $q.all([userList.getVersion(), deptList.getVersion()])
    .then(function(versions) {
      return versions[0] !== -1 && versions[1] !== -1;
    });
  }

  return {
    sync: sync,
    refresh: refresh,
    clear: clear,
    isInitSyncData: isInitSyncData
  };
});
