'use strict';
/**
 * @ngdoc service
 * @name Database
 * @description
 *  UDML(IR, IP Database)로부터 데이터를 얻는 기능
 */
app.service('Database', function ($http, $q) {

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

	/**
	 * entertainment device 인지 체크
	 * @memberof Database
	 * @param {String} deviceType device type name
	 * @returns {Boolean}
	 */
	this.checkIfEntertainment = function(deviceType) {
		return deviceType === 'Televisions' || deviceType === 'Satellite Set Top Boxes'|| deviceType === 'Cable and Fios Set Top Boxes'|| deviceType === 'DVR' ? true: false;
	};

	/**
	 * categories (device_types) 저장
	 * @memberof Database
	 * @param {Object} categories
	 */
	this.setCategories = function(categories) {
		this.categories = categories;
	};

	/**
	 * categories (device_types) 얻기
	 * @memberof Database
	 * @returns {Object}
	 */
	this.getCategories = function() {
		return this.categories;
	};

	/**
	 * 현재 category (device_type) 저장
	 * @memberof Database
	 * @param {String} category device type
	 */
	this.setCurrentCategory = function(category) {
		this.current.category = category;
	};

	/**
	 * 현재 category (device_type) 얻기
	 * @memberof Database
	 * @returns {String} device type
	 */
	this.getCurrentCategory = function() {
		return this.current.category;
	};

	/**
	 * brands 저장
	 * @memberof Database
	 * @param {Object} brands
	 */
	this.setBrands = function(brands) {
		this.brands = brands;
	};

	/**
	 * brands 얻기
	 * @memberof Database
	 * @returns {Object}
	 */
	this.getBrands = function() {
		return this.brands;
	};

	/**
	 * 현재 brand 저장
	 * @memberof Database
	 * @param brand
	 */
	this.setCurrentBrand = function(brand) {
		this.current.brand = brand;
	};

	/**
	 * 현재 brand 얻기
	 * @memberof Database
	 * @returns {*}
	 */
	this.getCurrentBrand = function() {
		return this.current.brand;
	};

	/**
	 * devices 저장
	 * @memberof Database
	 * @param {Object} devices
	 */
	this.setDevices = function(devices) {
		this.devices = devices;
	};

	/**
	 * devices 얻기
	 * @memberof Database
	 * @returns {Object}
	 */
	this.getDevices = function() {
		return this.devices;
	};

	/**
	 * 현재 momdel 저장
	 * @memberof Database
	 * @param model
	 */
	this.setCurrentModel = function(model) {
		this.current.model = model;
	};

	/**
	 * 현재 model 얻기
	 * @memberof Database
	 * @returns {*}
	 */
	this.getCurrentModel = function() {
		return this.current.model;
	};

	/**
	 * device category 얻기
	 * @memberof Database
	 * @returns {Promise} Future object
	 */
	this.retreiveDeviceTypes = function() {
		var self = this,
				d = $q.defer();

		// device category 얻기 API 호출
		$http.get('/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;
	};

	/**
	 * brand 목록 얻기
	 * @memberof Database
	 * @param {String} type device type
	 * @returns {Promise} Future object
	 */
	this.retrieveDeviceBrands = function(type) {

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

		// brand 목록 얻기 API 호출
		$http.get('/api/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;
	};

	/**
	 * model 목록 얻기
	 * @memberof Database
	 * @param {String} type device type
	 * @param {String} brand brand name
	 * @returns {Promise} Future object
	 */
	 	this.retrieveModels = function(type, brand) {
		console.log('type=', type, 'brand=', brand);
		var self = this,
				d = $q.defer(),
				url = '/api/db/devices/' + type + '/' + brand;

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

		return d.promise;
	};

	/**
	 * device 목록 얻기
	 * @memberof Database
	 * @param {String} type device type
	 * @param {String} brand brand name
	 * @param {String} query search query
	 * @returns {Promise} Future object
	 */
	this.searchModels = function(type, brand, query) {
		console.log('query: ' + query);
		var self = this,
				d = $q.defer(),
				url = '/api/db/search/' + type + '/' + brand;
		
		url += !query || query === '' ?  ''  : '/' + query;

		// device 목록 얻기 API 호출
		$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;
	};

	/**
	 * popular device 목록 얻기
	 * @memberof Database
	 * @param {String} type device type
	 * @param {String} brand brand name
	 * @returns {Promise} Future object
	 */
	this.searchPopularModels = function(type, brand) {
		console.log('type=', type, 'brand=', brand);
		var self = this,
			d = $q.defer(),
			url = '/api/db/search_popular/' + type + '/' + brand;
		
		// device 목록 얻기 API 호출
		$http.get(url).success(function(response) {
			if(response.result) {
				console.log('response=', response);
				self.setDevices(response.devices);
				d.resolve(response.devices);
			} else {
				d.reject(response.error);
			}
		}).error(function (error) {
			d.reject(error);
		});

		return d.promise;
	};

	/**
	 * all device 목록 얻기
	 * @memberof Database
	 * @param {String} type device type
	 * @param {String} brand brand name
	 * @returns {Promise} Future object
	 */
	this.searchAllModels = function(type, brand) {
		var self = this,
			d = $q.defer(),
			url = '/api/db/search_all/' + type + '/' + brand;

		// device 목록 얻기 API 호출
		$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;
	};

	/**
	 * 해당 device 데이터 얻기 - 2015.2.18 현재 프로토콜 문서 정의 내용과 아래 코드의 파라미터가 다름. 문의필요.
	 * @memberof Database
	 * @param {String} type device type
	 * @param {String} brand brand name
	 * @param {Number} modelId
	 * @param codeset
	 * @param dbType
	 * @returns {Promise} Future object
	 */
	this.getDeviceInfo = function(type, brand, modelId, codeset, dbType) {
		var d = $q.defer();
		var url = '/api/db/device/' + type + '/' + brand + '/' + modelId + '/' + codeset + '/' + dbType;
		console.log(url);

		// 해당 device 데이터 얻기 API 호출
		$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;

	};

	/**
	 * 현재 데이터 초기화
	 * @memberof Database
	 */
	this.clearData = function() {
		this.current = {};
	};
});