/**
 * widgetSlaCtrl Controller
 * Extracted from bookings module
 */

Bookings.controller('widgetSlaCtrl', ['$scope', 'Sla', 'SlaLog', 'ErrorsHandler', '$ClientsManager', '$element', function ($scope, Sla, SlaLog, ErrorsHandler, $ClientsManager, $element) {

	/**
	 * All possible SLAs
	 *
	 * @type array
	 */
	$scope.slas = [];

	/**
	 * All the logs for this task SLA.
	 *
	 * @type array
	 */
	$scope.logs = [];

	/**
	 * The SLA specific for this client.
	 *
	 * @type object
	 */
	$scope.sla = {};

	/**
	 * A reference to the selected client.
	 *
	 * @type object
	 */
	$scope.client = $ClientsManager.getClient();

	/**
	 * Load all SLAs from the database.
	 *
	 * @return void
	 */
	$scope.load = function () {
		Sla.get({ limit: 0 }, function (res) {
			$scope.slas = res.data;

			// Determine the client's SLA
			$scope.sla = _.find($scope.slas, function (el) { return el.id == $scope.client.sla_id; });
		}, ErrorsHandler);
	};

	$scope.loadLogs = function () {
		SlaLog.get({ limit: 0, task_id: $scope.task.id }, function (response) {
			$scope.logs = response.data;
		}, ErrorsHandler);
	};

	/**
	 * Processes the initialization code,
	 * based on the user type.
	 *
	 * @return void
	 */
	$scope.$watch('client', function () {
		if ($ClientsManager.isClient() && $scope.client.sla_id) {
			$scope.load();
			$scope.loadLogs();
		}
	}, true);

	$scope.$on('task-updated', function () {
		$scope.loadLogs();
	});

	/**
	 * Calculates the time spent in seconds since the task was created.
	 *
	 * @return integer
	 */
	$scope.getTimeSpent = function () {
		var start_date = moment($scope.task.created_at).valueOf(),
			now = moment().valueOf(),
			time_spent = (now - start_date) / 1000;

		return time_spent;
	};

	/**
	 * Calculates the number of seconds left for the initial job pickup.
	 *
	 * @param  integer time_spent - the number of seconds that have passed since the job was booked in.
	 * @return integer
	 */
	$scope.getSecondsInitial = function () {
		var time_spent = $scope.getTimeSpent()
			, log;

		log = _.find($scope.logs, function (el) { return el.type == 'initial'; });

		var sla_time = $scope.sla.time_initial * 60;

		// This has finished, calculate the time when it ended
		if (log) return sla_time - log.duration;

		return sla_time - time_spent;
	};

	/**
	 * Calculates the number of seconds left for the job to be fixed.
	 *
	 * @param  integer time_spent - the number of seconds that have passed since the job was assigned.
	 * @return integer
	 */
	$scope.getSecondsFix = function () {
		var time_spent, log;

		// This gets the first log when the task has been assigned to a user
		log = _.find($scope.logs, function (el) { return el.type == 'initial'; });
		if (!log) return $scope.sla.time_fix * 60;

		time_spent = (moment().valueOf() - moment(log.created_at).valueOf()) / 1000;

		var sla_time = $scope.sla.time_fix * 60;

		return sla_time - time_spent;
	};

	$scope.checkInitialRunning = function () {
		var log = _.find($scope.logs, function (el) { return el.type == 'initial'; });
		return (log) ? false : true;
	};

	$scope.checkFixRunning = function () {
		var log = _.find($scope.logs, function (el) { return el.type == 'initial'; }),
			log_fix = _.find($scope.logs, function (el) { return el.type == 'fix'; });
		return (log && !log_fix) ? true : false;
	};

	$scope.initialSucceed = function () {
		var log = _.find($scope.logs, function (el) { return el.type == 'initial'; });
		return (log && log.missed == '0') ? true : false;
	};

	$scope.fixSucceed = function () {
		var log = _.find($scope.logs, function (el) { return el.type == 'fix'; });
		return (log && log.missed == '0') ? true : false;
	};

}])
