var angular = require('angular'),
  _ = require('jsUtil');
require('../modules');
require('ngIonic');
require('../services/LinkerTreeDirectService');
require('../../../commons/services/Timer');
require('../services/LinkerService');
require('../../../commons/services/AppConfig');

/**
 * sino-linker-tree指令用来展示联系人的部门人员
 *
 * 示例：
 *
 * ```
 * <sino-linker-tree
 * dept="selfDeptCtrl.dept"
 * loading-state="selfDeptCtrl.loadingState"
 * top-dept-id="selfDeptCtrl.topDeptId"
 * detail-fn="selfDeptCtrl.getDetail(node)"
 * refresh-fn="selfDeptCtrl.fetchUserAndDept()">
 * </sino-linker-tree>
 * ```
 *
 * 属性：
 * * ***dept***: 当前部门下的子部门以及人员的数据
 * * ***loading-state***: 加载状态
 * * ***top-dept-id***: 顶级部门的id
 * * ***detail-fn***: 进去子部门的回调函数
 * * ***refresh-fn***: 刷新的回调方法
 */

angular
  .module('linker.directives')
  .directive('sinoLinkerTree', sinoLinkerTree);


/** @ngInject */
function sinoLinkerTree($ionicScrollDelegate) {
  return {
    restrict: 'E',
    templateUrl: 'views/linker/directives/sinoLinkerTree.tpl.html',
    scope: {
      topDeptId: '=', //顶级部门id
      dept: '=',
      loadingState: '=',
      detailFn: '&',
      refreshFn: '&'
    },
    link: function(scope, element, attrs) {
      scope.getDetail = getDetail;
      scope.refresh = refresh;
      scope.onScroll = onScroll;

      var positions = {}; //记录点击进下一部门的位置记录
      var _currentDept = scope.getDetail; // 当前部门

      var deptNavs = scope.deptNavs = [];

      scope.$watch('dept', function() {
        if(scope.dept) {
          if(scope.dept.deptId === scope.topDeptId) {
            addDeptNav(scope.dept);
          }
          scope.currentDept = scope.dept;
          if(_currentDept.deptId === scope.currentDept.deptId) {
            $ionicScrollDelegate.$getByHandle('linkerTreeScrollView')
            .scrollTo(0, positions[scope.currentDept.deptId] || 0);
          }
        }
      }, true);

      scope.$on('linkerTreeLeave', function() {
        deptNavs = scope.deptNavs = [];
        positions = {};
      });

      // 头部部门导航
      function addDeptNav(node) {
        var deptId = node.deptId || node.id,
          deptNavsCount = deptNavs.length,
          prevDept = deptNavsCount > 0? deptNavs[deptNavsCount - 1] : null,
          prevDeptId = prevDept? prevDept.deptId || prevDept.deptId : null;
        node.text = node.deptName;
        if (deptId === scope.topDeptId) {
          scope.topDept = node;
        } else if (prevDeptId !== deptId) {
          deptNavs.push(node);
        }
      }

      function getDetail(node) {
        if(node.deptId !== scope.topDeptId) {
          addDeptNav(node);
        }
        _currentDept = node;
        scope.currentDept.users = scope.currentDept.depts = [];
        scope.detailFn({node:node});
      }

      function refresh() {
        scope.refreshFn();
      }

      function onScroll(scrollTop) {
        //防止将上一单位的位置计入下一单位
        if(scope.currentDept && scope.currentDept.deptId == _currentDept.deptId) {
          positions[_currentDept.deptId] = scrollTop;
        }
      }

    }
  };
}
