/**
 * BookingsRepairCtrl Controller
 * Extracted from bookings module
 */

Bookings.controller('BookingsRepairCtrl', ['$scope', '$route', '$routeParams', '$location', 'repairsForm', '$ClientsManager', 'BookingsLocationManager', 'apiUrl', '$http', 'ErrorsHandler', 'TaskTemplatesService', 'Messenger', function ($scope, $route, $routeParams, $location, repairsForm, $ClientsManager, BookingsLocationManager, apiUrl, $http, ErrorsHandler, TaskTemplatesService, Messenger) {

	// ------------------------------------------------------------------------
	//
	//	SCOPE VARIABLES BELOW -->
	//
	// ------------------------------------------------------------------------

	$scope.data = repairsForm.data;

	// working with the shared service managers
	$scope.clientManager = $ClientsManager;
	$scope.locationManager = BookingsLocationManager;

	$scope.modal = repairsForm.modal;
	$scope.contract = {};
	$scope.form = repairsForm;
	$scope.routeParams = $routeParams;

	// added by nadeem 13.2.17 - if the user use the add task to project button
	// then this will pass the project id forward to the back-end
	// the crated task will then be correctly associated with the correct project
	if (typeof ($scope.$parent.projectId != 'undefined')) {
		$scope.data.project_id = $scope.$parent.projectId;
	}

	/**
	 * Bind the task templates typeahead remote options to the scope.
	 *
	 * @object
	 */
	$scope.templatesRemoteOptions = TaskTemplatesService;

	$scope.templates = {

		"selected": null,

		"lists": {
			"Selected Templates": repairsForm.data.templates,
			"Available Templates": []
		}
	};


	$scope.changeOfList = function (currentList, index) {

		if (currentList == "Available Templates") {
			$scope.templates.lists["Selected Templates"].push($scope.templates.lists[currentList][index]);
			$scope.templates.lists[currentList].splice(index, 1);
		}

		if (currentList == "Selected Templates") {
			$scope.templates.lists["Available Templates"].push($scope.templates.lists[currentList][index]);
			$scope.templates.lists[currentList].splice(index, 1);
		}

	};

	/**
	*Gets the templates for repairs tasks
	*/
	TaskTemplatesService.getTemplates('repair').then(function (response) {

		$scope.templates.lists['Available Templates'] = response.data;
	});

	// ------------------------------------------------------------------------
	//
	//	WATCHERS AND EVENT LISTENERS BELOW -->
	//
	// ------------------------------------------------------------------------

	$scope.$watch('data.site_user_id', function () {
		$scope.data.direct_dial = BookingsLocationManager.getUserDDI($scope.data.site_user_id);
	});

	$scope.$watch('data.site_id', function () {
		$scope.data.direct_dial = BookingsLocationManager.getUserDDI($scope.data.site_user_id);
	});



	// ------------------------------------------------------------------------
	//
	//	FUNCTIONS BELOW THIS LINE -->
	//
	// ------------------------------------------------------------------------

	$scope.redirectToSales = function () {
		window.location = '/sales#/sales/new/booking/' + $ClientsManager.getClientType() + '/' + $ClientsManager.getClient().id + '/' + $scope.data.id;
	};

	/**
	 * Checks to see wether a task id exists and it's of a given type.
	 *
	 * @param  id - the id of the task.
	 * @return void
	 */
	$scope.validateTaskId = function (id) {
		if (id) {
			$http.get(apiUrl('tasks/helpers/validate-task-id/' + id), { params: { type: 'repair' } })
				.success(function (response) {
					if (!response.success)
						$scope.invalid_task_id = true;
					else
						$scope.invalid_task_id = false;
				});
		} else {
			$scope.invalid_task_id = false;
		}
	};

	$scope.formIsValid = function () {

		if (!$scope.clientManager.isSelected())
			return false;

		if (!$scope.data.devices || $scope.data.devices.length == 0)
			return false;

		if ($scope.data.has_password == '1' && $scope.data.keychains.length == 0)
			return false;

		return true;
	};



}]);
