'use strict';


app.controller('SelectRoomController', function ($scope, $state, toastr, availableModels, Systems, Rooms ) {
  $scope.modelType = availableModels[0];
  $scope.availableModels = availableModels;

  $scope.finish = function() {
    var currentSystem = Systems.getCurrentSystem();
    var jsonData = JSON.stringify({ name: $scope.roomName,
                                    bstationName: $scope.modelType,
                                    type: $scope.modelType });
    
    var createRoom = function(json) {
      return Rooms.create(json).then(function(data) { 
        toastr.success('Successfully added ' + $scope.roomName);
        return currentSystem.id;
      });
    };

    var updateSystem = function(id) {
      return Systems.updateSystem(id).then(function (data) {
        console.log(data);
        //return $state.transitionTo('menu.advanced.add-room.link-remote', {}, { reload: true, inherit: true, notify: true  });
        $state.go('menu.advanced.options');
      });
    };

    createRoom(jsonData)
      .then(updateSystem)
      .catch(toastr.error);

  };
});
