'use strict';


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

  $scope.availableModels = availableModels;

  $scope.goBack = function () {
    $state.go('menu.programming.options');
  };

  $scope.finish = function () {

    var currentSystem = Systems.getCurrentSystem(),
        jsonData = JSON.stringify({ name: $scope.remote.modelType , type: $scope.remote.modelType });

    var setRemote = function(roomId, json) {
      return Rooms.setRemote(roomId, json).then(function(data) { 
        toastr.success('Successfully added the remote');
        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  });
      });
    };

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

});
