'use strict';
/**
 * @ngdoc service
 * @name Rooms
 * @description
 *  room 관련 기능
 */
app.service('Rooms', function ($rootScope, $http, $q, Systems) {

  this.scheEventEdit = false;
  this.scheEventData = null;

  /**
   * 현재 system 이 가지고 있는 room 들 중에서 선택한 room 의 device 정보 얻기
   * @memberof Rooms
   * @param {Number} id room ID
   * @returns {Array} devices 정보 객체 array
   */
  this.getDevices = function (id) {
    return Systems.getRoom(id).devices;
  };

  /**
   * 현재 system 이 가지고 있는 room 들 중에서 선택한 room 의 twoway device 정보 얻기
   * @memberof Rooms
   * @param {Number} id room ID
   * @returns {Array} twoway devices 정보 객체 array
   */
  this.getTwowayDevices = function (id) {
    return Systems.getRoom(id).twoway_devices;
  };

  /**
   * room 생성
   * @memberof Rooms
   * @param {String} data room name/base station type/base station name/MAC json string
   * @returns {Promise} Future object
   */
  this.create = function (data) {
    var d = $q.defer(),
        sysId = Systems.getCurrentSystem().id;

    // room 생성 API 호출
    $http.post('/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) {

  };

  /**
   * room 이름 변경
   * @memberof Rooms
   * @param {Number} roomId room ID
   * @param {String} data room name json string
   * @returns {Promise} Future object
   */
  this.edit = function (roomId, data) {
    var d = $q.defer(),
        sysId = Systems.getCurrentSystem().id;

    var url;
    if ($rootScope.virtualEditor) {
      url = '/api/dealer/room/edit/' + roomId;
    } else {
      url = '/api/room/edit/' + sysId + '/' + roomId;
    }

    // room 이름 변경 API 호출
    $http.post(url, 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;
  };

  /**
   * room 삭제
   * @memberof Rooms
   * @param {Number} roomId room ID
   * @returns {Promise} Future object
   */
  this.delete = function (roomId) {
    var d = $q.defer(),
        sysId = Systems.getCurrentSystem().id;

    // room 삭제 API 호출
    $http.post('/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;
  };

  /**
   * remote 를 room 에 set(기존에 있으면 해당 것을 replace)
   * @memberof Rooms
   * @param {Number} roomId room ID
   * @param {String} data MAC/remote type/remote name/search key json string
   * @returns {Promise} Future object
   */
  this.setRemote = function (roomId, data) {
    var d = $q.defer(),
        sysId = Systems.getCurrentSystem().id;

    // remote 를 room 에 set 하는 API 호출
    $http.post('/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;
  };

  /**
   * remote 를 해당 room 에서 삭제
   * @memberof Rooms
   * @param {Number} roomId room ID
   * @param {Object} data MAC address
   * @returns {Promise} Future object
   */
  this.delRemote = function (roomId, data) {
    var d = $q.defer(),
        sysId = Systems.getCurrentSystem().id;

    // remote 를 해당 room 에서 삭제하는 API 호출
    $http.post('/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;
  };

  /**
   * base station 추가/설정/삭제 동작
   * @memberof Rooms
   * @param {String} action action type
   * @param {Number} roomId room ID
   * @param {String} data parameters object json string
   * @returns {Promise} Future object
   */
  this.baseStations = function (action, roomId, data) {
    var d = $q.defer(),
        url = '/api/room/',
        sysId = Systems.getCurrentSystem().id;

    
    switch(action) {
      // slave base station 을 room 에 추가
      case 'add':
        url += 'add_bstation/';
        break;
      // base station 의 type, name, lightLevel 을 설정
      case 'edit':
        url += 'edit_bstation/';
        break;
      // base station 을 해당 room 에서 삭제
      case 'delete':
        url += 'del_bstation/';
        break;    
      default:
        break;
    }
    
    // action 에 따른 API 호출
    $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;
  };

  /**
   * Scheduled event 생성
   * @memberof Systems
   * @param {Number} systemId system ID
   * @param {Number} roomId room ID
   * @param {String} data scheduled event data json string
   * @returns {Promise} Future object
   */
  this.createScheduledEventMacro = function(systemId, roomId, data) {
    var url;
    if ($rootScope.virtualEditor) {
      url = '/api/dealer/room/create_event/' + roomId;
    } else {
      url = '/api/room/create_event/' + systemId + '/' + roomId;
    }

    // Scheduled event 생성 API 호출
    return $http.post(url, data).then(function (response) {
      return response;
    });
  };

  /**
   * Scheduled event 수정
   * @memberof Systems
   * @param {Number} systemId system ID
   * @param {Number} roomId room ID
   * @param {String} data scheduled event data json string
   * @returns {Promise} Future object
   */
  this.setScheduledEventMacro = function(systemId, roomId, data) {
    var url;
    if ($rootScope.virtualEditor) {
      url = '/api/dealer/room/set_event/' + roomId;
    } else {
      url = '/api/room/set_event/' + systemId + '/' + roomId;
    }

    // Scheduled event 생성 API 호출
    return $http.post(url, data).then(function (response) {
      return response;
    });
  };

  /**
   * scheduled event 정보 얻기
   * @memberof Rooms
   * @param {Number} systemId system ID
   * @param {Number} roomId room ID
   * @param {String} data event ID json string
   * @returns {Promise} Future object
   */
  this.getEvent = function (systemId, roomId, data) {
    var d = $q.defer();

    var url;
    if ($rootScope.virtualEditor) {
      url = '/api/dealer/room/get_event/' + roomId;
    } else {
      url = '/api/room/get_event/' + systemId + '/' + roomId;
    }

    // scheduled event 정보 얻기 API 호출
    $http.post(url, 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;
  };

  /**
   * scheduled event 목록 얻기
   * @memberof Rooms
   * @param {Number} systemId system ID
   * @param {Number} roomId room ID
   * @returns {Promise} Future object
   */
  this.getEvents = function (systemId, roomId) {
    var d = $q.defer();

    var url;
    if ($rootScope.virtualEditor) {
      url = '/api/dealer/room/get_events/' + roomId;
    } else {
      url = '/api/room/get_events/' + systemId + '/' + roomId;
    }

    // scheduled event 목록 얻기 API 호출
    $http.post(url).success(function (response) {
      if (response.result) {
        d.resolve(response.data);
      } else {
        d.reject(response.error);
      }
    }).error(function (error) {
      d.reject(error);
    });
    return d.promise;
  };

  /**
   * scheduled event 목록 저장
   * @memberof Rooms
   * @param {Number} systemId system ID
   * @param {Number} roomId room ID
   * @param {String} data scheduled events data json string
   * @returns {Promise} Future object
   */
  this.storeEvents = function (systemId, roomId, data) {
    var d = $q.defer();

    var url;
    if ($rootScope.virtualEditor) {
      url = '/api/dealer/room/store_events/' + roomId;
    } else {
      url = '/api/room/store_events/' + systemId + '/' + roomId;
    }

    // scheduled event 목록 저장 API 호출
    $http.post(url, 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;
  };

  /**
   * scheduled event 삭제
   * @memberof Rooms
   * @param {Number} systemId system ID
   * @param {Number} roomId room ID
   * @param {String} data event ID json string
   * @returns {Promise} Future object
   */
  this.deleteEvent = function (systemId, roomId, data) {
    var d = $q.defer();

    var url;
    if ($rootScope.virtualEditor) {
      url = '/api/dealer/room/del_event/' + roomId;
    } else {
      url = '/api/room/del_event/' + systemId + '/' + roomId;
    }

    // scheduled event 삭제 API 호출
    $http.post(url, 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;
  };

  /**
   * scheduled event state 변경
   * @memberof Rooms
   * @param {Number} systemId system ID
   * @param {Number} roomId room ID
   * @param {String} data event ID, default_state json string
   * @returns {Promise} Future object
   */
  this.changeEventState = function (systemId, roomId, data) {
    var d = $q.defer();

    var url;
    if ($rootScope.virtualEditor) {
      url = '/api/dealer/room/set_event_state/' + roomId;
    } else {
      url = '/api/room/set_event_state/' + systemId + '/' + roomId;
    }

    // scheduled event state 변경 API 호출
    $http.post(url, 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;
  };

  this.enableScheEventEdit = function() {
    this.scheEventEdit = true;
  };

  this.disableScheEventEdit = function() {
    this.scheEventEdit = false;
  };

  this.getScheEventEdit = function() {
    return this.scheEventEdit;
  };

  /**
   * scheduled event 저장
   * @memberof Rooms
   * @param {Object} data scheduled event data
   */
  this.setScheEventData = function(data) {
    this.scheEventData = data;
  };

  /**
   * scheduled event 얻기
   * @memberof Rooms
   * @returns {Object}
   */
  this.getScheEventData = function() {
    return this.scheEventData;
  };

 



});