'use strict';
//dependent upon on Bootstrap

app.directive('selectRoom', function () {
  return {
    restrict: 'E',
    require: 'ngModel',
    replace: true,
    scope: {
      ngModel: '=',
      rooms: '=',
      disable: '='
    },
    template: '<div class="scrollable-window padding-left-sm">' +
              '<div class="radio" ng-repeat="room in rooms track by room.id">' + 
                '<label>' +
                  '<input type="radio" name="room" ng-model="ngModel.room" ng-if="!disable" ng-value="room">' + 
                  '<input type="radio" name="room" ng-model="ngModel.room" ng-if="disable" ng-disabled="room.devices.length === 0" ng-value="room">' +
                  '<strong>{{room.name}}</strong>' +
                  '<p><small class="text-muted" ng-if="room.devices.length === 0">This room has no devices yet.</small></p>' +
                '</label>' +
              '</div></div>',
    link: function (scope, element, attrs, ngModelCtrl) {
      scope.$watch('ngModel.room.id',
        function(newValue, oldValue) {
          if (newValue) {
            ngModelCtrl.$setValidity('roomSelected', true);
          } else {
            ngModelCtrl.$setValidity('roomSelected', false);
          }
        });
    }
  };
});