/**
 * widgetPasswordsCtrl Controller
 * Extracted from bookings module
 */

Bookings.controller('widgetPasswordsCtrl', ['$scope', 'PasswordsService', 'ErrorsHandler', '$timeout', '$uibModal', 'Keychain', 'Messenger', function ($scope, PasswordsService, ErrorsHandler, $timeout, $uibModal, Keychain, Messenger) {

	/**
	 * Repair parts
	 *
	 * @array
	 */

	$scope.passwords = PasswordsService.passwords;


	$scope.deletePassword = function (password) {
		Keychain.delete({ keychainId: password.id, task_id: $scope.task.id }, function () {
			PasswordsService.deletePassword(password);
			Messenger("The password has been changed successfully!");
		});
	};
	$scope.chooseExistingPassword = function () {
		var modalInstance = $uibModal.open({
			templateUrl: 'clients/modals/keychain.html',
			controller: 'KeychainModalCtrl',
			windowClass: 'modal-fixed',
			resolve: {
				TaskId: function () {
					return $scope.task.id;
				},
				Customer: function () {
					return {
						id: $scope.task.client_id,
						type: $scope.task.client_type
					};
				}
			}
		});

		modalInstance.result.then(function () {
			$scope.loadTaskPasswords();
		});
	};

	$scope.addPassword = function () {

		var modalInstance = $uibModal.open({
			templateUrl: 'bookings/modals/task-password.html',
			controller: 'TaskPasswordCtrl',
			windowClass: 'modal-fixed',
			resolve: {
				taskDevices: function () {
					return $scope.task.devices;
				},
				password: function () {
					return {};
				},
				keychainTypes: function () {
					return PasswordsService.types;
				}
			}
		});

		modalInstance.result.then(function (key) {

			var parameters = {
				client_type: $scope.task.client_type,
				client_id: $scope.task.client_id,
				task_id: $scope.task.id,
				password: key.password,
				username: key.username,
				type: key.type,
				device_id: key.device_id
			};

			Keychain.save(parameters, function (response) {
				$scope.loadTaskPasswords();
				Messenger("The password has been added successfully!");
			});
		});
	};

	$scope.changePassword = function (password) {

		var modalInstance = $uibModal.open({
			templateUrl: 'bookings/modals/task-password.html',
			controller: 'TaskPasswordCtrl',
			windowClass: 'modal-fixed',
			resolve: {
				taskDevices: function () {
					return $scope.task.devices;
				},
				password: function () {
					if (password.device_id) {
						password.device_id = parseInt(password.device_id, 10);
					}
					return password;
				},
				keychainTypes: function () {
					return PasswordsService.types;
				}
			}
		});

		modalInstance.result.then(function (keychain) {
			Keychain.update(keychain, function (response) {
				$scope.loadTaskPasswords();
				Messenger("The password has been changed successfully!");
			});
		});
	};


	$scope.getDeviceName = function (id) {

		var device = _.find($scope.task.devices, function (device) {
			return device.id == id;
		});

		return device ? device.name : 'General';
	};

	$scope.$watch('task.id', function (taskId) {
		$scope.loadTaskPasswords();
	});

	$scope.loadTaskPasswords = function () {
		if ($scope.task)
			Keychain.get({ task_id: $scope.task.id }, function (response) {
				PasswordsService.setPasswords(response.data);
			});
	};

	// Copy password to clipboard
	$scope.copyToClipboard = function (text) {
		if (!text) return;

		// Create a temporary textarea element
		var textarea = document.createElement('textarea');
		textarea.value = text;
		textarea.style.position = 'fixed';
		textarea.style.opacity = '0';
		document.body.appendChild(textarea);
		textarea.select();

		try {
			document.execCommand('copy');
			Messenger("Password copied to clipboard!");
		} catch (err) {
			Messenger("Failed to copy password", "error");
		}

		document.body.removeChild(textarea);
	};



}])
