'use strict';

app.directive('homeBtnSetting', function() {
	return {
		restrict: 'EA',
		replace:true,
		scope: {
			system: '='
		},
		template: '<a ng-click="showHomeButtonSettingModal()">Change Home button behavior for remotes</a>',
		controller: function($scope, $modal, toastr, Systems) {
			$scope.homeBtn = {option:0};
			$scope.homeBtn.option = $scope.system.home_btn_option;
			var homeButtonSettingModal = $modal({
				scope: $scope,
				template: 'views/modal-tpls/home-btn-setting.html',
				show: false
			});

			$scope.showHomeButtonSettingModal = function () {
				homeButtonSettingModal.$promise.then(homeButtonSettingModal.show);       
			};

			$scope.setHomeButtonBehavior = function() {
				var data = JSON.stringify({ option: $scope.homeBtn.option});
				Systems.setHomeButton(data).then(function(data) {
					//console.log('data=', data);
					$scope.system.home_btn_option = angular.copy($scope.homeBtn.option);
					Systems.loadSystem($scope.system);
				}).catch(function(error) {
					toastr.error(error, 'Error');
				});
			};

		}
	};
});