/**
 * PagesRmaCtrl Controller
 * Extracted from bookings module
 */

Bookings.controller('PagesRmaCtrl', ['$scope', '$ClientsManager', 'Rma', 'ErrorsHandler', '$location', 'Messenger', 'RmaItem', 'Page', function ($scope, $ClientsManager, Rma, ErrorsHandler, $location, Messenger, RmaItem, Page) {

	/**
	 * The repair data
	 *
	 * @object
	 */
	$scope.data = {};

	/**
	 * All the possible repair statuses.
	 *
	 * @type array
	 */
	$scope.statuses = [];

	/**
	 * A local reference to the clients manager service.
	 *
	 * @object
	 */
	$scope.clientManager = $ClientsManager;

	/**
	 * The total price.
	 *
	 * @type Float
	 */
	$scope.total = 0;


	// --------------------------
	// Functions below
	// --------------------------

	/**
	 * Watch the labour and update the total price
	 * when this changes.
	 *
	 * @return void
	 */
	$scope.$watch('data.labour', function () {
		$scope.total = parseFloat($scope.data.labour);
	});

	/**
	 * Watch when the taskId becomes active and load the page.
	 *
	 * @return null
	 */
	$scope.$watch('taskId', function (value) {
		if (value) {
			Page.setTitle($scope.taskId);
			$scope.loadData();
		}
	});

	/**
	 * Loads the repair data, by sending an ajax request.
	 *
	 * @returns void
	 */
	$scope.loadData = function () {
		Rma.get({ taskId: $scope.taskId }, function (res) {
			$scope.data = res;
			$ClientsManager.setClient(res.client);
			$ClientsManager.setClientType(res.client_type);

		}, ErrorsHandler);
	};

	/**
	 * Updates the repair data
	 */
	$scope.update = function (ev) {
		Rma.update($scope.data, function (res) {
			$scope.data = res;
			$scope.labour_changed = false;
			Messenger("I've succesfully saved the remote support job!");
		}, ErrorsHandler);
	};

	/**
	 * Change the user who the repair is assigned to.
	 *
	 * @param  integer id - the id of the new user.
	 * @return void
	 */
	$scope.assignTo = function (id) {
		Rma.update({ id: $scope.data.id, assigned_to: id }, function (res) {
			$scope.data.assigned_to = id;
			$scope.data.user = res.user;
		}, ErrorsHandler);
	};

	/**
	 * Retrieves the current selected status object.
	 *
	 * @return object
	 */
	$scope.getStatus = function () {
		return _.find($scope.statuses, function (el) { return el.id == $scope.data.status_id; });
	};

	/**
	 * Returns the status name.
	 *
	 * @return string
	 */
	$scope.getStatusName = function () {
		var status = $scope.getStatus();

		return (status) ? status.name : '';
	};

	/**
	 * Updates the current task status.
	 *
	 * @param  integer id - status id
	 * @return void
	 */
	$scope.updateStatus = function (id) {
		Rma.update({ id: $scope.data.id, status_id: id }, function (res) {
			$scope.data.status_id = id;
		}, ErrorsHandler);
	};

	/**
	 * Adds a new item to be rma'ed
	 *
	 * @param Object item
	 * @return void
	 */
	$scope.addItem = function (item) {
		RmaItem.save({
			task_id: $scope.taskId,
			stock_id: item.id,
			quantity: 1
		}, function (response) {
			$scope.data.items.push(response);
		}, ErrorsHandler);
	};

	/**
	 * Removes an item from the rma list.
	 *
	 * @param  Object item
	 * @return void
	 */
	$scope.removeItem = function (item) {
		RmaItem.remove({ taskId: item.task_id, itemId: item.id }, function (response) {
			$scope.data.items = _.reject($scope.data.items, function (el) { return el.id == item.id; });
		}, ErrorsHandler);
	};

	/**
	 * Updates an item.
	 *
	 * @param Object item
	 * @return void
	 */
	$scope.updateItem = function (item) {
		RmaItem.update(item, function (response) {
			var itm = _.find($scope.data.items, function (el) { return el.id == item.id; });
			if (itm)
				angular.extend(itm, response);
		}, ErrorsHandler);
	};
}]);
