var angular = require('angular');
require('../modules');
require('jsUtil');
require('../services/CurrentTaskService');
require('../services/TaskRemoteService');
require('../services/taskList');
require('../services/taskDrafts');

'use strict';

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

module.controller('TaskGroupCtrl', function($scope, TaskRemoteService,
	$state, CommonService, taskList, taskDrafts, CurrentTaskService, $ionicScrollDelegate) {
	$scope.showCard = 'entire';

	$scope.loading = false;
	$scope.tasks = taskList;
	$scope.drafts = taskDrafts;

	$scope.goBack = function() {
		$state.go('mission.find');
	};
	/**
	 * 查看任务详情。
	 * @param  {String} taskId 任务id
	 */
	$scope.toTaskDetail = function(task) {
		CurrentTaskService.changeCurrentTask(task).then(function() {
			CommonService.setCookie('mission.taskDetail.parent', $state.current);
			$state.get('mission.taskDetail').parent = $state.current;
			$state.go('mission.taskDetail', {
				taskId: task.id
			});
		});
	};
	/**
	 * 搜索。
	 * @param  {String} searchText 搜索词
	 */
	$scope.$watch('search.text', function(searchText) {
		if (!searchText) {
			$scope.searchItems = [];
		} else {
			$scope.searchItems = jsUtil.filter($scope.showCard === 'entire' ? $scope.tasks.items : $scope.drafts.items, function(task) {
				return (task.title && task.title.indexOf(searchText) !== -1) || (task.content && task.content.indexOf && task.content.indexOf(searchText) !== -1);
			});
			$ionicScrollDelegate.$getByHandle('searchTaskScroll').scrollTop();
		}
	});

	$scope.onClickDraftTab = function() {
		$scope.showCard = 'drafts';
	};

	$scope.onClickListTab = function() {
		$scope.showCard = 'entire';
	};
	/**
	 * 创建任务。
	 */
	$scope.createTask = function() {
		CommonService.setCookie('mission.taskCreate.parent', $state.current);
		$state.get('mission.taskCreate').parent = $state.current;
		$state.go('mission.taskCreate');
	};
	/**
	 * 刷新列表页面。
	 */
	$scope.onRefresh = function() {
		if ($scope.showCard === 'entire') {
			$scope.onTaskRefresh();
		} else if ($scope.showCard === 'drafts') {
			$scope.onDraftRefresh();
		}
	};
	/**
	 * 刷新任务列表。
	 */
	$scope.onTaskRefresh = function() {
		$scope.loading = true;
		$scope.tasks.refresh().
		finally(function() {
			$scope.loading = false;
			$scope.$broadcast('scroll.refreshComplete');
		});
	};
	/**
	 * 刷新草稿列表页面
	 */
	$scope.onDraftRefresh = function() {
		$scope.loading = true;
		$scope.drafts.refresh().
		finally(function() {
			$scope.loading = false;
			$scope.$broadcast('scroll.refreshComplete');
		});
	};
	/**
	 * 加载更多。
	 */
	$scope.loadMore = function() {
		if ($scope.showCard === 'entire') {
			$scope.loadMoreTask();
		} else if ($scope.showCard === 'drafts') {
			$scope.loadMoreDraft();
		}
	};

	$scope.loadMoreTask = function() {
		$scope.tasks.loadMore($scope.tasks.number).
		finally(function() {
			$scope.$broadcast('scroll.infiniteScrollComplete');
			$scope.$broadcast('scroll.resize');
		});
	};
	/**
	 * 加载更多草稿任务。
	 */
	$scope.loadMoreDraft = function() {
		$scope.drafts.loadMore($scope.drafts.number).
		finally(function() {
			$scope.$broadcast('scroll.infiniteScrollComplete');
			$scope.$broadcast('scroll.resize');
		});
	};
	/**
	 * 解析日期格式。
	 * @param  {String} dateStr 日期字符串
	 */
	$scope.parseDateStr = function(dateStr) {
		return CommonService.smartListDateParser(dateStr);
	};
	/**
	 * 解析任务类型。
	 * @param  {String} typeId 任务类型id
	 */
	$scope.parseType = function(typeId) {
		return TaskRemoteService.getTypeById(typeId);
	};
});