var angular = require('angular');
require('../modules');
require('../../info/services/InfoReleaseService');
require('../../info/services/ModelService');

'use strict';

var module = angular.module('infoRelease');

module.controller('InfoReleaseMoreCtrl', function($scope, $state, $stateParams,
  $q, InfoReleaseService, $toast, Timer, $ionicViewSwitcher,
  ModelService, AppConfig, $location) {
  /**
   * enterSign: 进入列表页的方式
   * 1. group：从首页进入
   * 2. left：后退，页码减小
   * 3. right：前进，页码增加
   */
  var modelId = $stateParams.modelId,
    enterSign = $location.search().enterSign,
    pageNo = parseInt($location.search().pageNo || 0),
    needSyncDataFromRemote = enterSign == 'group',
    model,
    dataList;

  $scope.pageNo = pageNo;

  /**
   * 从缓存中获取所需的模块信息
   */
  function initModelData() {
    ModelService.getModelOfId(modelId).then(function(_model) {
      model = _model;
      $scope.title = model.name;
    });
  }

  var ctrl = this;
  ctrl.loadingState = 1;

  var delayTimerId = null;
  $scope.multiModule = false;

  $scope.$on('$ionicView.beforeEnter', function() {
    initModelData();
    $scope.refresh();
  });

  $scope.refresh = function() {
    ctrl.loadingState = 1;
    init().then(function(data) {
      ctrl.loadingState = !_.isEmpty(data) ? 2 : 4;
    }, function() {
      ctrl.loadingState = 3;
    });
  };

  function init() {
    return InfoReleaseService.getInfoListNew(modelId, pageNo + 1, needSyncDataFromRemote)
    .then(function(result) {
      $scope.pages = result.dataList.getPageSize();
      $scope.infos = result.page? (result.page.items || []) : [];
      dataList = result.dataList;
      if(!_.isEmpty($scope.infos) && modelId != $scope.infos[0].modelId) {//多模块
        $scope.multiModule = true;
      }

      if(!result.success) {
        $toast.showShortTop('无法从服务器端获取最新数据！');
      }
      creData();
      return $scope.infos;
    });
  }

  function creData() {
    if((model.mode == 'archivein' || model.mode == 'archiveout') && model.listUrl) {
      if(model.mode == 'archiveout') {
        $scope.fileCodeName = '文号';
        $scope.creTimeName = '签发时间';
        $scope.deptName = '承办处室';
      } else {
         $scope.fileCodeName = '来文字号';
         $scope.creTimeName = '起草时间';
         $scope.deptName = '来文单位';
      }
      $scope.mode = model.mode;
    }
  }

  /**
  * 回退
  * @return {Promise}
  */
  $scope.goBack = function(){
    $state.go('infoRelease-infoReleaseGroup');
  };

  /**
  * 向右滑动
  * @return {Promise}
  */
  $scope.onSwipeLeft = function() {
    var canSwipe = dataList != null && (!dataList.isEnd(pageNo + 1));
    if(canSwipe) {
      $ionicViewSwitcher.nextDirection('forward');
      onSwipe(pageNo + 1, 'right');
    }
  };

  function onSwipe(pageNo, enterSign) {
    if (model.mode == 'info' || !model.mode) {
      $state.go('infoRelease-infoReleaseMore', {
        modelId: modelId,
        modelName: model.name,
        pageNo: pageNo,
        enterSign: enterSign
      });
    } else if (model.mode == 'cggs') {
      $state.go('infoRelease-infoReleaseMoreCggs', {
        modelId: modelId,
        modelName: model.name,
        pageNo: pageNo,
        enterSign: enterSign
      });
    }else {
      $state.go('infoRelease-infoReleaseMoreOffice', {
        modelId: modelId,
        listUrl: model.listUrl,
        modelName: model.name,
        pageNo: pageNo
      });
    }
  }

  /**
   * 向左滑动
   * @return {Promise}
   */
  $scope.onSwipeRight = function() {
    if(pageNo !== 0) {
      $ionicViewSwitcher.nextDirection('back');
      onSwipe(pageNo - 1, 'left');
    } else {
      $scope.firstPageBoolean = true;
    }
  };

  /**
  * 跳转到一条信息的详情页面
  * @param  {SContent} info 一条信息
  * @return {Promise}
  */
  $scope.toDetail = function(info) {
    $ionicViewSwitcher.nextDirection('forward');
    if(model.mode == 'archivein' || model.mode == 'archiveout') {
      $state.go('info-infoDetailOffice', {
        id: info.id,
        pid: model.id,
        modelName: model.name
      });
    } else if(model.mode == 'cggs') {
      $state.go('info-infoDetailCggs', {
        modelId: info.modelId,
        infoId: info.infoId
      });
    }else {
      $state.go('info-infoDetail', {
        pid: modelId,
        modelId: info.modelId,
        infoId: info.infoId,
        modelName: model.name
      });
    }
  };

  /**
   * 刷新
   */
  $scope.onRefresh = function() {
    $scope.refreshx = true;
    $scope.loaderror = false;
    $scope.loading = false;
    $scope.noData = '加载中...';
    init().finally(function() {
      Timer.delay(function() {
        $scope.refreshx = false;
        if($scope.infos.length == 0) {
          $scope.noData = '无数据';
          $scope.loaderror = false;
        }
      }, 500);
    });
  };

});
