/**
 * ProjectsMainCtrl Controller
 * Extracted from bookings module
 */

Bookings.controller('ProjectsMainCtrl', ['$scope', '$ClientsManager', 'Page', 'Project', 'ErrorsHandler', '$location', 'Messenger', '$routeParams', 'Files', 'ActivityLog', '$filter', 'InvoiceBookingService', function ($scope, $ClientsManager, Page, Project, ErrorsHandler, $location, Messenger, $routeParams, Files, ActivityLog, $filter, InvoiceBookingService) {

	// ------------------------------------------------------------------------
	//
	//	SCOPE VARIABLES BELOW -->
	//
	// ------------------------------------------------------------------------
	$scope.files = [];
	$scope.ClientsManager = $ClientsManager;
	$scope.InvoiceService = InvoiceBookingService;

	/**
	 * All the boolean options in a dropdown menu
	 */
	$scope.boolean_options = [{ name: 'No', value: 0 }, { name: 'Yes', value: 1 }];

	// ------------------------------------------------------------------------
	//
	//	WATCHERS AND EVENT LISTENERS BELOW -->
	//
	// ------------------------------------------------------------------------

	if ($location.path().substring(0, 9) != '/projects') {
		// Automatically redirect to overview
		$location.path('projects/overview');
	}

	$scope.$watch('projectId', function (value) {

		$routeParams.projectId = value;

		if (value) {
			Page.setTitle('P ' + $scope.projectId);
			$scope.load();
		}
	});

	// ------------------------------------------------------------------------
	//
	//	FUNCTIONS BELOW THIS LINE -->
	//
	// ------------------------------------------------------------------------

	$scope.getProgress = function () {
		if (!$scope.data) return 0;

		var completed = 0;
		_.each($scope.data.tasks, function (el) {
			if (el.completed == '1') completed++;
		});

		var percentage = completed / $scope.data.tasks.length * 100;

		if (isNaN(percentage)) return 0;

		return percentage.toFixed(2);
	};

	$scope.load = function () {
		Project.get({ projectId: $scope.projectId }, function (response) {
			$scope.data = response;
			$ClientsManager.setClient(response.client);
			$ClientsManager.setClientType(response.client_type);
			$ClientsManager.unify(response.site, response.site_user);
		}, ErrorsHandler);

		Files.get({ objectId: $scope.projectId, objectType: 'project' }, function (response) {
			$scope.files = response.data;
		}, ErrorsHandler);
	};

	$scope.update = function () {
		Project.update($scope.data, function (response) {
			angular.extend($scope.data, response);
			Messenger("Changes have been successfully saved!");
		}, ErrorsHandler);
	};

	$scope.updateProjectValue = function (name, value) {
		var d = { id: $scope.data.id };
		d[name] = value;

		Project.update(d, function (response) {
			angular.extend($scope.data, response);
		}, ErrorsHandler);
	};

	$scope.showProjectLeader = function () {
		if (!$scope.data) return 'Not set';

		var selected = _.find($scope.users, function (el) { return el.id == $scope.data.assigned_to; });
		return ($scope.data.assigned_to && selected) ? selected.name : 'Not set';
	};

	$scope.cancelProject = function () {
		var d = { projectId: $scope.data.id };
		Project.delete(d, function (response) {
			Messenger('I have succesfully deleted the project!');
			window.setTimeout(function () { window.location.href = '/' }, 2000);
		}, ErrorsHandler);


	};

}])
