'use strict';
app.controller('timestampPlanDetailsController', ['$scope', '$http', '$modalInstance', '$timeout', '$modal', '$translate', 'blockUI', 'lib', 'fileUpload', 'modelId', function ($scope, $http, $modalInstance, $timeout, $modal, $translate, blockUI, lib, fileUpload, modelId) {

	$scope.model = null;
	$scope.renaming = false;
	$scope.newName = null;

	var init = function () {
		blockUI.start();
		$http.get('/Api/TimestampPlans/' + modelId).then(function (response) {
			$scope.model = response.data;
			blockUI.stop();
		}, function (response) {
			lib.showRestError(response, function () {
				$scope.close();
			});
		});
	};

	$scope.rename = function () {
		$scope.newName = $scope.model.name;
		$scope.renaming = true;
	};

	$scope.renameConfirm = function () {
		$http.put('/Api/TimestampPlans/' + modelId, {
			name: $scope.newName
		}).then(function (response) {
			$scope.model = response.data;
			$scope.renaming = false;
		}, lib.handleRestError);
	};

	$scope.renameCancel = function () {
		$scope.renaming = false;
	};

	$scope.remove = function () {
		$translate('tspdConfirmRemove', { name: $scope.model.name }).then(function (confirmMsg) {
			$translate(['areYouSure', 'yesRemove', 'cancel']).then(function (msgs) {
				var buttons = {};
				buttons[msgs.yesRemove] = {
					css: 'btn-danger',
					click: removeConfirmed
				};
				buttons[msgs.cancel] = {
					css: 'btn-default',
					click: function (modalInstance) {
						modalInstance.dismiss();
					}
				};
				lib.showMessageBox(confirmMsg, msgs.areYouSure, buttons);
			});
		});
	};

	var removeConfirmed = function (confirmModal) {
		blockUI.start();
		var url = '/Api/TimestampPlans/' + modelId;
		$http.delete(url).then(function (response) {
			blockUI.stop();
			confirmModal.close();
			$modalInstance.close();
		}, lib.handleRestError);
	};

	$scope.close = function () {
		$modalInstance.dismiss();
	};

	init();
}]);
