/**
 * ChangeAlarmCtrl Controller
 * Extracted from bookings module
 */

Bookings.controller('ChangeAlarmCtrl', ['$scope', '$uibModalInstance', 'data', '$window', function ($scope, $uibModalInstance, data, $window) {

	$scope.title = 'Set reminder';
	$scope.field_name = 'Reminder date';
	$scope.times = [];
	$scope.date = {
		date: data.alarm_date ? moment(data.alarm_date).toDate() : moment().toDate(),
		time: moment(data.alarm_date).format('HH:mm')
	};

	var addPadding = function (i) {
		return (i < 10) ? '0' + i : i;
	};

	// Build the hours and minutes
	for (var i = 0; i < 24; i++) {
		for (var j = 0; j < 60; j += 15) {
			$scope.times.push(addPadding(i) + ':' + addPadding(j));
		}
	}

	$scope.save = function () {
		var timestamp = moment($scope.date.date).format('YYYY-MM-DD') + ' ' + $scope.date.time + ':00';

		if (moment().valueOf() > moment(timestamp).valueOf()) {
			$window.alert('The start date should be lower than the end date');
			return false;
		}

		$uibModalInstance.close(timestamp);
	};

	$scope.cancel = function () {
		$uibModalInstance.dismiss('cancel');
	};

}]);
