'use strict';

var angular = require('angular'),
  _ =  require('jsUtil');
require('../modules');
require('../services/InfoDisplayService');
require('../../info/services/ModelService');


var module = angular.module('infoDisplay.directives');
module.directive('sinoInfoList', function() {
  return {
    restrict: 'E',
    templateUrl: 'views/infoDisplay/sinoInfoList.tpl.html',
    replace: true,
    controller: 'sinoInfoListCtrl',
    controllerAs: 'infoListCtrl'
  };
})
.controller('sinoInfoListCtrl', function(InfoDisplayService, Timer,
  $attrs, $scope, $ionicScrollDelegate, $timeout, $state, ModelService) {
  var service = InfoDisplayService;
  var modelId = $scope.$eval($attrs.modelId);
  var scrollTop = 0;
  var screenHeight = window.screen.height - 44 - 40;
  var ctrl = this;

  ctrl.init = init;
  ctrl.refresher = refresher;
  ctrl.moreDataCanBeLoaded = moreDataCanBeLoaded;
  ctrl.loadMore = loadMore;
  ctrl.onScroll = onScroll;
  ctrl.toDetail = toDetail;

  $scope.$watch('modelId', function() {

    ModelService.getModelOfId(modelId).then(function(data) {
      ctrl.model = data;
    }).then(function() {
      init();
    });;
  });

  function init() {
    ctrl.loadingState = 1;
    getInfo(0).then(function(data) {
      if(!_.isEmpty(data.dataList)) {
        ctrl.loadingState = 2;
      } else {
        ctrl.loadingState = 4;
      }
    }, function() {
      ctrl.loadingState = 3;
    });
  }

  /**
   * 根据modelId获取该模块下的所有信息
   * @param  {[String]} modelId 模块ID
   * @return {[array]} 信息列表
   */
  function getInfo(pageNo) {
    return service.getInfoByModelId(ctrl.model, pageNo).then(function(data) {
      var infos = (ctrl.model.mode == 'archivein' || ctrl.model.mode == 'archiveout') ? organizationData(data.dataList) : data.dataList;
      repeatData(infos);
      ctrl.number = data.number;
      ctrl.totalNumber = data.totalNumber;
      return data;
    });
  }

  function repeatData(items) {
    var item;
    for(var i = 0, len = items.length; i < len; i++) {
      item = items[i];
      item.trackById = item.infoId + item.modelId + i;
    }
    ctrl.infos = items;
  }

  /**
   * 将公文的数据组织一下，采用信息发布的模板展示。
   */
  function organizationData(data) {
    _.each(data, function(item) {
      item.proTime = item.creTime;
      item.notion = item.needGrade;
      item.infoId = item.id;
    });
    return data;
  }

  function toDetail(item) {
    if(ctrl.model.mode == 'archivein' || ctrl.model.mode == 'archiveout') {
        $state.go('info-infoDetailOffice', {
            id: item.id,
            pid: ctrl.model.id,
            modelName: ctrl.model.name
        });
    } else {
        $state.go('info-infoDetail', {
            pid: ctrl.model.id,
            modelId: item.modelId,
            infoId: item.infoId,
            modelName: ctrl.model.name
        });
    }
  }

  /**
   * 下滑刷新
   */
  function refresher() {
    Timer.delay(function() {
      getInfo(0).then(function() {
        $scope.$broadcast('scroll.refreshComplete');
      });
    }, 800);
  }


  function moreDataCanBeLoaded() {
    return ctrl.number >= 0 && ctrl.number < ctrl.totalNumber;
  }

  ctrl.loading = true;
  function loadMore() {
    //ctrl.number是当前的页码数，从1开始，而pageNo是从0开始
    if(ctrl.loading) {
      ctrl.loading = !ctrl.loading;
      getInfo(ctrl.number).then(function() {
        ctrl.loading = !ctrl.loading;
        $scope.$broadcast('scroll.infiniteScrollComplete');
        $scope.$broadcast('scroll.resize');
      });
    }

  }

  function onScroll() {
    $timeout(function() {
      if (scrollView && scrollView.getScrollPosition()) {
        scrollTop = scrollView.getScrollPosition().top;
        console.log('scrollTop=' + scrollTop);
      }
    }, 500);
  }

});

