/**
 * BookingPasswordsCtrl Controller
 * Extracted from bookings module
 */

Bookings.controller('BookingPasswordsCtrl', ['$scope', 'Device', '$ClientsManager', 'ErrorsHandler', '$http', 'apiUrl', 'Keychain', 'Messenger', function ($scope, Device, $ClientsManager, ErrorsHandler, $http, apiUrl, Keychain, Messenger) {
	$scope.data = [];
	$scope.data.client_devices = [];

	$scope.data.client_keychains = [];

	$scope.data.keychain = {
		device_id: null,
		username: '',
		password: '',
		type: 'windows',
		id: '0'
	};


	$scope.addKeychain = function (keychain) {

		//validation first
		$scope.task.keychains.push(angular.copy(keychain));
		$scope.data.keychain = {
			device_id: null,
			username: '',
			password: '',
			type: 'windows',
			id: '0'
		};
	};

	$scope.removeKeychain = function (device) {

		$scope.task.keychains = _.reject($scope.task.keychains, function (el) {
			return el === device;
		});
	};

	$scope.getDeviceName = function (id) {

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

		if (device)
			return device.name;

		return "No device";
	};

	//Load the client devices when the client change
	$scope.$watch(function () { return $ClientsManager.client; }, function () {

		if ($ClientsManager.getClient().id) {
			$scope.loadDevices($ClientsManager.getClient().id, $ClientsManager.getClient().type);
			$scope.loadClientPasswords();
		}

	}, true);

	$scope.$watch('task.devices', function (value) {
		_.each($scope.task.keychains, function (item) {
			if ($scope.belongsToADifferentDevice(item))
				$scope.removeKeychain(item);
		});
	});

	$scope.loadClientPasswords = function () {
		var client_type = $ClientsManager.getClient().type;
		var client_id = $ClientsManager.getClient().id;

		//Renew the client keys
		Keychain.get({ client_type: client_type, client_id: client_id }, function (response) {
			$scope.data.client_keychains = response.data;
		});
	};

	$scope.loadDevices = function (client_id, client_type) {
		if ($scope.loading_devices) return false;

		$scope.loading_devices = true;
		Device.get({ client_id: client_id, client_type: client_type }, function (response) {
			$scope.data.client_devices = response.data;
			$scope.loading_devices = false;
		}, ErrorsHandler);
	};

	$scope.isSelectedPassword = function (password) {


		if (!password)
			return false;

		return _.find($scope.task.keychains, function (item) {

			return item.id == password.id;

		}) ? true : false;

	};

	$scope.belongsToADifferentDevice = function (password) {

		if (!password || !password.device_id)
			return false;
		return _.find($scope.task.devices, function (item) {

			return item.id == password.device_id;

		}) ? false : true;

	};

	//Create the password and add it to the selected passwords list
	$scope.addPassword = function (password) {

		if (!password.username || !password.password)
			return;

		if (!$ClientsManager.getClient())
			return;

		var client_type = $ClientsManager.getClient().type;
		var client_id = $ClientsManager.getClient().id;
		var parameters = {
			client_type: client_type,
			client_id: client_id,
			password: password.password,
			username: password.username,
			type: password.type,
			device_id: password.device_id
		};

		Keychain.save(parameters, function (response) {
			Messenger("The password has been created successfully");
			$scope.addKeychain(response);

			$scope.loadClientPasswords();

		}, ErrorsHandler);

	};

	//Mark the password as disabled and remove it from the client passwords list
	$scope.removePassword = function (password) {

		Keychain.delete({ keychainId: password.id, disabled: true }, function () {

			Messenger("The password has been deleted successfully!");

			//Renew the client keys
			$scope.loadClientPasswords();
		});

	};

	// 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);
	};




}])
