/**
 * PagesSaleLeadCtrl Controller
 * Extracted from bookings module
 */

Bookings.controller('PagesSaleLeadCtrl', ['$scope', '$rootScope', '$ClientsManager', 'SaleLead', 'Tasks', 'ErrorsHandler', '$location', 'Messenger', '$AuthUser', '$uibModal', function ($scope, $rootScope, $ClientsManager, SaleLead, Tasks, ErrorsHandler, $location, Messenger, $AuthUser, $uibModal) {

	/**
	 * The sale data
	 *
	 * @object
	 */
	$scope.data = {};

	/**
	 * The number of stars visible in the rating bar.
	 */
	$scope.max_stars = 5;

	/**
	 * All the possible statuses.
	 *
	 * @type array
	 */
	$scope.statuses = [];

	/**
	 * A local reference to the clients manager service.
	 *
	 * @object
	 */
	$scope.ClientsManager = $ClientsManager;

	$scope.$watch('taskId', function (value) {
		if (value) $scope.loadData();
	});

	/**
	 * Loads the repair data, by sending an ajax request.
	 *
	 * @returns void
	 */
	$scope.loadData = function () {
		SaleLead.get({ saleLeadId: $scope.taskId }, function (res) {
			$scope.data = res;
			$scope.data.onsite_end = moment(res.onsite_end).add('hours', 2).toDate();
			$ClientsManager.setClient(res.client);
			$ClientsManager.setClientType(res.client_type);
			$ClientsManager.unify(res.site, res.site_user);
		}, ErrorsHandler);
	};

	/**
	 * Hovering over the rating.
	 */
	$scope.hoveringOver = function (value) {
		$scope.overStar = value;
		$scope.percentage = 100 * (value / $scope.max_stars);
	};

	/**
	 * Display a modal with the subscribers
	 *
	 * @return null
	 */
	$scope.showSubscribers = function () {
		var modalInstance = $uibModal.open({
			templateUrl: 'modals/subscribers.html',
			controller: 'SubscribersCtrl',
			resolve: {
				task: function () {
					return $scope.data;
				},
				users: function () {
					return $scope.users;
				}
			}
		});
	};

	/**
	 * Unassign this sale lead
	 *
	 * @return null
	 */
	$scope.unassign = function () {
		SaleLead.update({ id: $scope.data.id, assigned_to: null, updated_at: $scope.data.updated_at }, function (response) {
			angular.extend($scope.data, response || {});
			$scope.data.onsite_end = moment(response.onsite_end).add('hours', 2).toDate();
			$rootScope.$broadcast('task-updated', response);
		}, ErrorsHandler);
	};

	/**
	 * Subscribe to this task
	 *
	 * @return null
	 */
	$scope.subscribe = function () {
		$scope.data.subscribers.push($AuthUser.data);

		Tasks.update({ id: $scope.data.id, subscribers: $scope.data.subscribers }, function (response) {
			angular.extend($scope.data, response || {});
			$scope.data.onsite_end = moment(response.onsite_end).add('hours', 2).toDate();
			$rootScope.$broadcast('task-updated', response);
			Messenger("You have succesfully subscribed to this task.");
		}, ErrorsHandler);
	};

	/**
	 * Unsubscribe from this task
	 *
	 * @return null
	 */
	$scope.unsubscribe = function () {
		$scope.data.subscribers = _.reject($scope.data.subscribers, function (el) { return el.id == $AuthUser.data.id; });

		Tasks.update({ id: $scope.data.id, subscribers: $scope.data.subscribers }, function (response) {
			angular.extend($scope.data, response || {});
			$scope.data.onsite_end = moment(response.onsite_end).add('hours', 2).toDate();
			$rootScope.$broadcast('task-updated', response);
			Messenger("You have succesfully un-subscribed from this task.");
		}, ErrorsHandler);
	};

	/**
	 * Updates the repair data
	 */
	$scope.save = function (ev) {
		var data = angular.copy($scope.data);
		data.onsite_end = moment($scope.data.onsite_end).format("YYYY-MM-DD");

		SaleLead.update(data, function (res) {
			$scope.data = res;
			$scope.data.onsite_end = moment(res.onsite_end).add('hours', 2).toDate();
			Messenger("I've succesfully saved the data!");
		}, ErrorsHandler);
	};

	/**
	 * Set a new value
	 */
	$scope.updateValue = function (params) {
		angular.extend(params, { id: $scope.data.id, updated_at: $scope.data.updated_at });

		SaleLead.update(params, function (response) {
			angular.extend($scope.data, response || {});
			$scope.data.onsite_end = moment(response.onsite_end).add('hours', 2).toDate();
			$rootScope.$broadcast('task-updated', response);
			Messenger("I've succesfully saved the job!");
		}, ErrorsHandler);
	};

}]);
