/**
 * DeviceModalCtrl Controller
 * Extracted from bookings module
 */

Bookings.controller('DeviceModalCtrl', ['$scope', '$uibModalInstance', 'data', 'Customer', 'Device', 'Manufacturer', 'ModelNumber', 'ErrorsHandler', function ($scope, $uibModalInstance, data, Customer, Device, Manufacturer, ModelNumber, ErrorsHandler) {

	$scope.device = angular.copy(data) || { model_number: { manufacturer: {}, } };
	$scope.Customer = Customer || {};

	if (!$scope.device.model_number)
		$scope.device.model_number = { manufacturer: {} };
	$scope.helper = {
		model_number: angular.copy($scope.device.model_number),
		manufacturer: angular.copy($scope.device.model_number.manufacturer)
	};


	if ($scope.device.antivirus_expiration_date)
		$scope.helper.antivirus_expiration_date = new Date($scope.device.antivirus_expiration_date).toISOString();
	else
		$scope.helper.antivirus_expiration_date = null;


	var initialised = false;

	$scope.$watch('helper.manufacturer', function (value, oldValue) {

		if (initialised && value != oldValue) {
			$scope.helper.model_number = {};
		}
		else {
			initialised = true;
		}
	}, true);

	$scope.$watch('helper.model_number', function (value, oldValue) {

		if (initialised && value != oldValue) {

			if ($scope.helper.model_number.id) {
				$scope.device.model_number_id = value.id;
				$scope.device.model_number = $scope.helper.model_number;
			} else
				$scope.device.model_number = {
					id: null,
					model_number: $scope.helper.model_number.model_number,
					manufacturer_id: $scope.helper.manufacturer.id,
					manufacturer: {
						id: $scope.helper.manufacturer.id,
						name: $scope.helper.manufacturer.name
					}
				}
		}
		else {
			initialised = true;
		}

	}, true);

	$scope.$watch('helper.antivirus_expiration_date', function (value) {

		if (value) {
			$scope.device.antivirus_expiration_date = moment(value).format('YYYY-MM-DD');
		}

	}, true);

	$scope.manufacturerSearchSettings = {
		allowClear: false,
		query: function (options) {

			Manufacturer.get({ name: options.term, page: options.page }, function (response) {
				options.callback({
					more: response.current_page < response.last_page,
					results: response.data
				});
			});

		},
		formatResult: function (obj, container, query) {
			return obj.name;
		},
		formatSelection: function (obj) {
			return obj.name;
		}
	};
	$scope.modelNumberSearchSettings = {
		allowClear: false,
		query: function (options) {

			var manufacturer_id = ($scope.helper.manufacturer.id) ? $scope.helper.manufacturer.id : 0;

			ModelNumber.get({ name: options.term, page: options.page, manufacturer_id: manufacturer_id }, function (response) {
				options.callback({
					more: response.current_page < response.last_page,
					results: response.data
				});
			});

		},
		formatResult: function (obj, container, query) {
			return obj.model_number;
		},
		formatSelection: function (obj) {
			return obj.model_number;
		}
	};

	$scope.save = function () {
		if (!$scope.loading) {
			$scope.loading = true;
			var method = ($scope.device.id) ? Device['update'] : Device['save'];

			if (!$scope.device.id) {
				$scope.device.client_id = Customer.id;
				$scope.device.client_type = Customer.type;
			}

			method($scope.device, function (response) {
				$uibModalInstance.close(response);
			}, function (a, b) {
				$scope.loading = false;
				ErrorsHandler(a, b);
			});
		}
	};

	$scope.cancel = function () {
		$uibModalInstance.dismiss('cancel');
	};
}]);
