/**
 * CustomerDevicesCtrl Controller
 * Extracted from bookings module
 */

Bookings.controller('CustomerDevicesCtrl', ['$scope', 'Device', 'ErrorsHandler', '$ClientsManager', '$paginator', '$uibModal', '$timeout', function ($scope, Device, ErrorsHandler, $ClientsManager, $paginator, $uibModal, $timeout) {

	var search_timeout = 200;
	/**
	 * A list of client devices.
	 *
	 * @type Array
	 */
	$scope.devices = [];

	/**
	 * A reference to the clients manager.
	 *
	 * @type Object
	 */
	$scope.clientManager = $ClientsManager;

	/**
	 * Which devices should be display, possible
	 * options are: 'all', 'selected'.
	 *
	 * @type String
	 */
	$scope.view = 'all';

	/**
	 * Filter that will get applied to the ng-repeat that renders the devices.
	 */
	$scope.search = '';

	/**
	 * A list of selected devices,
	 * only storing their IDs
	 *
	 * @type Array
	 */
	if (!_.isArray($scope.selected_devices)) $scope.selected_devices = [];

	/**
	 * A reference to the paginator
	 *
	 * @type Object
	 */
	$paginator = angular.copy($paginator);
	$scope.paginator = $paginator;

	$paginator.setOnPageChange(function (page) {
		$scope.loadDevices($ClientsManager.getClient().id, $ClientsManager.getClientType(), page);
	});

	/**
	 * Watches for when the client changes and
	 * reloads the devices.
	 *
	 * @return void
	 */
	$scope.$watch('clientManager.client', function () {
		if ($ClientsManager.isSelected()) {
			$scope.loadDevices($ClientsManager.getClient().id, $ClientsManager.getClientType());
		}
	}, true);

	$scope.$watch('selected_devices', function () {
		_.each($scope.devices, function (device) {
			var d = _.find($scope.selected_devices, function (el) { return el.id == device.id; });
			device.selected = (d) ? true : false;
		});

		_.each($scope.selected_devices, function (device) {
			if (!device.selected)
				device.selected = true;
		});
	}, true);

	$scope.$watch('devices', function () {
		_.each($scope.devices, function (device) {
			var d = _.find($scope.selected_devices, function (el) { return el.id == device.id; });
			device.selected = (d) ? true : false;
		});
	}, true);


	$scope.$watch('view', function (value) {
		if (value == 'all')
			$scope.filter = {};
		else
			$scope.filter = { selected: true };
	});

	$scope.$watch('search', function (value, oldValue) {
		if (value != oldValue && $scope.client_id && $scope.client_type) {

			if (search_timeout)
				$timeout.cancel(search_timeout);

			search_timeout = $timeout(function () {
				$scope.loadDevices();
			}, 250);
		}
	});

	$scope.$on('task-updated', function () {
		$scope.changed = false;
	});

	/**
	 * Loads devices for a particular client.
	 *
	 * @return void
	 */
	$scope.loadDevices = function (client_id, client_type, page) {

		if ($scope.loading_devices) return false;

		if (!page) page = 1;

		if (client_id && client_type) {
			$scope.client_id = client_id;
			$scope.client_type = client_type;
		}

		$scope.loading_devices = true;

		Device.get({ client_id: $scope.client_id, client_type: $scope.client_type, page: page, query: $scope.search, limit: 5 }, function (response) {
			$scope.devices = response.data;
			$scope.loading_devices = false;
			$paginator.setFromResponse(response);
		}, ErrorsHandler);
	};

	/**
	 * Displays the modal for editing a device.
	 *
	 * @return void
	 */
	$scope.showDeviceModal = function (device) {
		var modalInstance = $uibModal.open({
			templateUrl: 'bookings/modals/device.html',
			controller: 'DeviceModalCtrl',
			size: 'lg',
			resolve: {
				Customer: function () {
					return $ClientsManager.getClient();
				},
				data: function () {
					return device || { type: 'laptop' };
				}
			}
		});

		modalInstance.result.then(function (response) {
			if (device) {
				angular.extend(device, response || {});
				device.model_number = response.model_number;
			}
			else {
				$scope.devices.push(response);
				$scope.selectDevice(response);
			}
		});
	};

	/**
	 * Selects or deselects a device once clicked on it.
	 *
	 * @return void
	 */
	$scope.selectDevice = function (device) {
		device.selected = !device.selected;
		$scope.changed = true;

		if (device.selected) {
			$scope.selected_devices.push(device);
		} else {
			$scope.selected_devices = _.reject($scope.selected_devices, function (el) { return el.id == device.id; });
		}
	};

	$scope.getDevices = function () {
		return ($scope.view == 'all') ? $scope.devices : $scope.selected_devices;
	};

	/**
	 * Returns the appropriate class to be used, based
	 * on the device type.
	 *
	 * @return String
	 */
	$scope.getDeviceClass = function (device) {
		switch (device.type) {
			case 'laptop': return 'fa fa-laptop';
			case 'pc': return 'fa fa-desktop';
			case 'mac': return 'fa fa-apple';
			case 'tablet': return 'fa fa-tablet';
			case 'hdd': return 'fa fa-hdd-o';
		}

		return 'fa fa-laptop';
	};

}])
