'use strict';

app.directive('addRooms', function (){
  return {
    restrict: 'EA',
    require: 'ngModel',
    replace: true,
    scope: {
      ngModel: '=',
      modelTypes: '='
    },
    templateUrl: 'views/installer/new/rooms/rooms.html',
    controller: function ($scope, Rooms) {
      $scope.selected = {};
      $scope.selected.room = $scope.ngModel.rooms[0];

      $scope.createNewRoom = function() {
        return Rooms.generateOne();
      };

      $scope.$watchCollection('ngModel.rooms', function(newValue, oldValue) {
        if (newValue.length != oldValue.length) {
          $scope.selected.room = $scope.ngModel.rooms[$scope.ngModel.rooms.length - 1];
        }
      });
    } 
  };
});
