/**
 * assignTaskCtrl Controller
 * Extracted from bookings module
 */

Bookings.controller('assignTaskCtrl', ['$scope', '$element', 'Tasks', 'ErrorsHandler', '$http', 'apiUrl', '$paginator', '$routeParams', '$location', function ($scope, $element, Tasks, ErrorsHandler, $http, apiUrl, $paginator, $routeParams, $location) {

	/**
	 * The tasks filters.
	 *
	 * @type Array
	 */
	$scope.pills = [
		{ title: 'Open' },
		{ title: 'Closed' }
	];

	$paginator = angular.copy($paginator);
	$scope.paginator = $paginator;

	/**
	 * The filters navigation type
	 *
	 * @type String
	 */
	$scope.navType = 'pills';

	/**
	 * When pills are being loaded, update the id
	 * of the selected filter.
	 *
	 * @return void
	 */
	$scope.$watch('pills', function (value) {

		_.each(value, function (el, i) {
			if (el.active)
				$scope.selected_filter = i;
		});

	}, true);

	/**
	 * When the selected filter changes reload the tasks.
	 *
	 * @return void
	 */
	$scope.$watch('selected_filter', function (value) {
		if (typeof value != 'undefined')
			$scope.loadTasks();
	});

	$scope.$watch('search_filter', function (value) {
		if (typeof value != 'undefined')
			$scope.loadTasks();
	});

	/**
	 *	Reload the tasks when page changes
	 *
	 *	@return void
	 */
	$paginator.setOnPageChange(function (page) {
		$scope.loadTasks(page);
	});

	/**
	 * Shows the modal and loads the tasks.
	 *
	 * @return void
	 */
	$scope.show = function () {
		$scope.loadTasks();
		$element.modal('show');
	};

	/**
	 * Hides the modal
	 *
	 * @return void
	 */
	$scope.hide = function () {
		$element.modal('hide');
	};

	/**
	 * Loads all the tasks for the selected client/customer.
	 *
	 * @return void
	 */
	$scope.loadTasks = function (page) {
		if (!$scope.data) return false;
		if (!page) page = 1;

		var params = { client: true, client_id: $scope.data.client_id, client_type: $scope.data.client_type, limit: 10, project_free: true, user: true, page: page, textSearch: $scope.search_filter };
		if ($scope.selected_filter == '0') params.show = 'open';
		if ($scope.selected_filter == '1') params.completed = 1;

		Tasks.get(params, function (response) {
			$scope.tasks = response.data;
			$paginator.setFromResponse(response);
		}, ErrorsHandler);
	};

	/**
	 * Counts how many tasks have been checked.
	 *
	 * @return Integer
	 */
	$scope.selectedCount = function () {
		var total = 0;

		_.each($scope.tasks, function (el) {
			if (el.selected) total++;
		});

		return total;
	};

	/**
	 * Retrieves a list of task id that have been selected.
	 *
	 * @return Array
	 */
	$scope.getSelected = function () {
		var tasks = _.filter($scope.tasks, function (el) { return el.selected === true; });
		return _.pluck(tasks, 'id');
	};

	/**
	 * Assign the selected tasks to this project.
	 *
	 * @return void
	 */
	$scope.assign = function () {
		$http.post(apiUrl('tasks/helpers/assign-tasks-to-project'), { project_id: $scope.data.id, tasks: $scope.getSelected() })
			.success(function (response) {
				$scope.data.tasks = response;
				$scope.hide();
			})
			.error(ErrorsHandler);
	};

}])
