/**
 * widgetClientCtrl Controller
 * Extracted from bookings module
 */

Bookings.controller('widgetClientCtrl', ['$ClientsManager', '$scope', 'ErrorsHandler', 'Repair', 'SiteUser', '$routeParams', '$rootScope', '$http', '$timeout', 'CustomerForm', '$uibModal', function ($ClientsManager, $scope, ErrorsHandler, Repair, SiteUser, $routeParams, $rootScope, $http, $timeout, CustomerForm, $uibModal) {

	/**
	 * The client data.
	 *
	 * @type object
	 */
	$scope.client = $ClientsManager.getClient();

	/**
	 * Scope reference to the clients manager.
	 *
	 * @type object
	 */
	$scope.cManager = $ClientsManager;

	/**
	 * Reference to the site.
	 *
	 * @type object
	 */
	$scope.site = {};

	/**
	 * Reference to the site user data.
	 *
	 * @type object
	 */
	$scope.site_user = {};

	/**
	 * Determines if the direct dial value has changed.
	 *
	 * @type boolean
	 */
	$scope.changed_direct_dial = false;

	/**
	 * Determines if the user's primary phone value has changed.
	 *
	 * @type boolean
	 */
	$scope.changed_user_phone = false;

	/**
	 * Determines if the user's mobile phone value has changed.
	 *
	 * @type boolean
	 */
	$scope.changed_mobile_phone = false;

	/**
	 * Settings for the customer popup form.
	 *
	 * @return void
	 */
	CustomerForm.onUpdate = function (data) {
		// Close the modal
		$scope.showCustomerForm = false;

		// Clear the fake route parameter
		$routeParams.customerId = null;

		// Update the data
		$ClientsManager.setClient(data);
	};

	/**
	 * Watch for when the client object is ready
	 * and make a reference to the site object.
	 *
	 * @return void
	 */
	$scope.$watch('client', function () {
		if ($scope.client) {
			$scope.site = _.find($scope.client.sites, function (el) { return el.id == $scope.task.site_id; });
		}
	}, true);

	/**
	 * Watch for when the site object is ready
	 * and make a reference to the site user object.
	 *
	 * @return void
	 */
	$scope.$watch('site', function () {
		if ($scope.site) {
			$scope.site_user = _.find($scope.site.users, function (el) { return el.id == $scope.task.site_user_id; });
		}
	}, true);

	/**
	 * Saves the direct dial's value to the database.
	 *
	 * @return void
	 */
	$scope.saveDirectDial = function () {
		Repair.update({ id: $scope.task.id, direct_dial: $scope.task.direct_dial }, function (res) {
			$scope.changed_direct_dial = false;
		}, ErrorsHandler);
	};

	/**
	 * Saves the direct dial's value to the database.
	 *
	 * @return void
	 */
	$scope.saveUserPhone = function (type) {
		SiteUser.update({ id: $scope.site_user.id, site_id: $scope.site_user.site_id, phone: $scope.site_user.phone, mobile: $scope.site_user.mobile }, function (res) {
			if (type == 'phone')
				$scope.changed_user_phone = false;
			else
				$scope.changed_mobile_phone = false;
		}, ErrorsHandler);
	};

	$scope.editCustomer = function () {
		// Fake change route
		$routeParams.customerId = $ClientsManager.getClient().id;
		$rootScope.$broadcast('$routeChangeSuccess');
		$scope.showCustomerForm = true;
	};

	$scope.dial = function (number, ev) {
		// Replace all spaces
		number = number.replace(' ', '');

		// If no number, then stop the call
		if (!number) return false;

		// Change button to loading state
		var btn = angular.element(ev.target).button('loading');

		// Reset button text after 750ms
		$timeout(function () {
			btn.button('reset');
		}, 750);

		$http.get('/dial/' + number)
			.error(ErrorsHandler);
	};

	$scope.changeClient = function () {
		var modalInstance = $uibModal.open({
			templateUrl: 'bookings/modals/change-client.html',
			controller: 'ChangeClientCtrl',
			resolve: {
				task: function () {
					return $scope.task;
				}
			}
		});

		modalInstance.result.then(function (client) {
			angular.extend($scope.client, client || {});
			$ClientsManager.setClientType(client.type);
		});
	};

	$scope.sms = function () {
		var modalInstance = $uibModal.open({
			templateUrl: 'bookings/modals/sms.html',
			controller: 'SmsCtrl',
			resolve: {
				task: function () {
					return $scope.task;
				},
				mobile: function () {
					return $scope.client.mobile;
				}
			}
		});
	};

}]);
