'use strict';
/**
 * @ngdoc controller
 * @name SelectRoomController
 * @description
 *  base station 목록 표시 후 base station 선택과 함께 room 생성 화면
 */
app.controller('SelectRoomController', function ($rootScope, $scope, $state, toastr, searchResults, Systems, Rooms ) {
  $scope.searchKey = searchResults.searchKey;
  $scope.addRoom = {'name': null, 'baseStation': null};
  $scope.baseStations = searchResults.searchedDevices;

  $scope.bsType = 'BASESTATIONS';

  $scope.finish = function() {
    var currentSystem = Systems.getCurrentSystem();
    var baseStation = $scope.addRoom.baseStation;
    var jsonData = JSON.stringify({ name: $scope.addRoom.name, 
                                    bstationName: baseStation.name + ' ' + baseStation.mac.slice(10,12), 
                                    bstationMac: baseStation.mac, 
                                    type: baseStation.name });

    // room 생성
    var createRoom = function(json) {
      return Rooms.create(json).then(function(data) { 
        toastr.success('Successfully added ' + $scope.addRoom.name + '.');
        return Systems.getCurrentSystem().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);

  };
});

app.controller('LinkRemoteController', function ($scope, $state, Systems ) {
  
  $scope.doNext = function() {
    $state.go('menu.advanced.add-remote.select');
  };
});
