'use strict';

app.service('Rooms', function ($http, $q, Systems) { 

  this.getDevices = function (id) {
    return Systems.getRoom(id).devices;
  };

  this.create = function (data) {
    var d = $q.defer(),
        sysId = Systems.getCurrentSystem().id;

    $http.post('/urcassistant/api/room/create/' + sysId, data).success(function (response) {
      if (response.result) {
        d.resolve(response.result);
      } else {
        d.reject(response.error);
      }
    }).error(function (error) {
      d.reject(error);
    });
    return d.promise;
  };

  //TODO: Add these functions to service

  this.get = function (id) {

  };

  this.fetch = function (id) {

  };

  this.edit = function (roomId, data) {
    var d = $q.defer(),
        sysId = Systems.getCurrentSystem().id;

    $http.post('/urcassistant/api/room/edit/' + sysId + '/' + roomId, data).success(function (response) {
      if (response.result) {
        d.resolve(response.data);
      } else {
        d.reject(response.error);
      }
    }).error(function (error) {
      d.reject(error);
    });
    return d.promise;
  };

  this.delete = function (roomId) {
    var d = $q.defer(),
        sysId = Systems.getCurrentSystem().id;

    $http.post('/urcassistant/api/room/delete/' + sysId + '/' + roomId).success(function (response) {
      if (response.result) {
        d.resolve(response.result);
      } else {
        d.reject(response.error);
      }
    }).error(function (error) {
      d.reject(error);
    });
    return d.promise;
  };

  this.setRemote = function (roomId, data) {
    var d = $q.defer(),
        sysId = Systems.getCurrentSystem().id;

    $http.post('/urcassistant/api/room/set_remote/' + sysId + '/' + roomId, data).success(function (response) {
      console.log(response);
      if (response.result) {
        d.resolve(response.result);
      } else {
        d.reject(response.error);
      }
    }).error(function (error) {
      d.reject(error);
    });
    return d.promise;
  };

  this.delRemote = function (roomId, data) {
    var d = $q.defer(),
        sysId = Systems.getCurrentSystem().id;

    $http.post('/urcassistant/api/room/del_remote/' + sysId + '/' + roomId, data).success(function (response) {
      console.log(response);
      if (response.result) {
        d.resolve(response.result);
      } else {
        d.reject(response.error);
      }
    }).error(function (error) {
      d.reject(error);
    });
    return d.promise;
  };


  this.baseStations = function (action, roomId, data) {
    var d = $q.defer(),
        url = '/urcassistant/api/room/',
        sysId = Systems.getCurrentSystem().id;

    
    switch(action) {
      case 'add':
        url += 'add_bstation/';
        break;
      case 'edit':
        url += 'edit_bstation/';
        break;
      case 'delete':
        url += 'del_bstation/';
        break;    
      default:
        break;
    }
    

    $http.post(url + sysId + '/' + roomId, data).success(function (response) {
      console.log(response);
      if (response.result) {
        d.resolve(response.result);
      } else {
        d.reject(response.error);
      }
    }).error(function (error) {
      d.reject(error);
    });
    return d.promise;
  };

 



});