'use strict';

app.controller('SelectBaseController', function ($scope, $state, toastr, availableModels, Systems, Rooms) {
  $scope.bs = {
    name: '',
    modelType: availableModels[0]
  };

  $scope.availableModels = availableModels;

  $scope.finish = function () {
    var currentSystem = Systems.getCurrentSystem(),
             jsonData = JSON.stringify({ type : $scope.bs.modelType, name: $scope.bs.name, lightLevel: 2 });

    
    var addBStation = function(roomId, json) {
      return Rooms.baseStations('add', roomId, json).then(function(data) { 
        toastr.success('Successfully added ' + $scope.bs.name + '.');
        return currentSystem.id;
      });
    };

    var updateSystem = function(id) {
      return Systems.updateSystem(id).then(function (data) {
        console.log(data);
        return $state.transitionTo('menu.programming.options', {}, { reload: true, inherit: true, notify: true  });
        //$state.go('menu.programming.option');
      });
    };

    addBStation($scope.room.id, jsonData)
      .then(updateSystem)
      .catch(function(error) {
        toastr.error(error, 'Error');
      });

  };
});