'use strict';

app.directive('selectVariable', function ($modal, $timeout) {
	return {
		restrict: 'A',
		scope: {
			system: '=',
			steps: '=',
			sfunc: '='
		},
    controller: function ($rootScope, $scope, $element, Systems, toastr) {
			$scope.specialFuncTypes = [{title:'Variable', value:0}, {title:'If/Else: Variable', value:1}];
			$scope.variables = [];
			$scope.title = '';
			var modal = $modal({scope: $scope, template: 'views/modal-tpls/select-variable.html', show: false});

      var roomId = null;
      if ($rootScope.virtualEditor) {
        roomId = Systems.getCurrentRoom();
      }

      Systems.getVariables(roomId).then(
				function(data){
					$scope.variables = data;

					if($scope.variables.length)
						$scope.sfunc.variable = $scope.variables[0];
					else
						$scope.sfunc.variable = null;
				},
				function(error){
					toastr.error(error);
					console.log('error=', error);
				}
			);

			$element.on('click', function(event) {
				event.preventDefault();
				$scope.title = $scope.specialFuncTypes[$scope.sfunc.type].title;
				modal.$promise.then(modal.show);
			});
		}
	};
});