'use strict';

app.service('Database', function ($http, $q, baseUrl) {

  this.categories = {};
  this.brands = {};
  this.devices = {};
  this.current = {};

  this.setCategories = function(categories) {
    this.categories = categories;
  };

  this.getCategories = function() {
    return this.categories;
  };

  this.setCurrentCategory = function(category) {
    this.current.category = category;
  };
  this.getCurrentCategory = function() {
    return this.current.category;
  };

  this.setBrands = function(brands) {
    this.brands = brands;
  };

  this.getBrands = function() {
    return this.brands;
  };

  this.setCurrentBrand = function(brand) {
    this.current.brand = brand;
  };
  this.getCurrentBrand = function() {
    return this.current.brand;
  };

  this.setDevices = function(devices) {
    this.devices = devices;
  };

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

  this.setCurrentModel = function(model) {
    this.current.model = model;
  };

  this.getCurrentModel = function() {
    return this.current.model;
  };

  this.checkIfEntertainment = function(deviceType) {
    return deviceType === 'Televisions' || deviceType === 'Satellite Set Top Boxes'|| deviceType === 'Cable and Fios Set Top Boxes'|| deviceType === 'DVR' ? true: false;
  };

  this.retreiveDeviceTypes = function() {
    var self = this,
        d = $q.defer();

    $http.get('/installer/api/db/device_types').success(function(response) {
      console.log(response);
      if(response.result) {
        self.setCategories(response.types);
        d.resolve(response.types);
      } else {
        d.reject(response.error);
      }
    }).error(function (error) {
      d.reject(error);
    });

    return d.promise;
  };

  this.retrieveDeviceBrands = function(type) {

    var self = this,
        d = $q.defer();

    $http.get(baseUrl + '/db/brands/' + type).success(function(response) {
      if(response.result) {
        self.setBrands(response.brands);
        d.resolve(response.brands);
      } else {
        d.reject(response.error);
      }
    }).error(function (error) {
      d.reject(error);
    });

    return d.promise;
  };

  this.searchModels = function(type, brand, query) {
    console.log('query: ' + query);
    var self = this,
        d = $q.defer(),
        url = baseUrl + '/db/search/' + type + '/' + brand;
    
    url += !query || query === '' ?  ''  : '/' + query;

    $http.get(url).success(function(response) {
      if(response.result) {
        self.setDevices(response.devices);
        d.resolve(response.devices);
      } else {
        d.reject(response.error);
      }
    }).error(function (error) {
      d.reject(error);
    });

    return d.promise;
  };

  this.getDeviceInfo = function(type, brand, modelId, codeset, dbType) {
    var d = $q.defer();
    var url = baseUrl + '/db/device/' + type + '/' + brand + '/' + modelId + '/' + codeset + '/' + dbType;
    console.log(url);
    $http.get(url)
    .success(function(response) {
      console.log(response);
      if(response.result) {
        d.resolve(response.device);
      } else {
        d.reject(response.error);
      }
    })
    .error(function (error) {
      d.reject(error);
    });

    return d.promise;

  };

  this.clearData = function() {
    this.current = {};
  };
});