/******************************************************************************
@owner: SVSi
@author: Arthur Johnson
@function: Parsers for nCmd responses
*******************************************************************************/

	function vSeries(ip, port, onError){
			
		var obj = {};
		
		obj._ip = ip;
		obj.ip = ip;
		obj.rawstatus = '';
		obj.error = false;
		obj.port = !undef(port) ? port : '50001';
		obj.onError = !undef(onError) ? onError : noop;
		
		obj.setPort = function(port){
			obj.port = port;
		}

		obj.getStatus = function(async){
		
			if (async){
				nCmdAsync('tcpclient '+obj.ip+' '+obj.port+' '+asciiToHex('getStatus'),
					'_data1', function(data){
						obj.rawstatus = data;
						var str = obj.rawstatus;
						while (str.indexOf(';') != -1){
							obj[str.substring(0, str.indexOf(':')).toLowerCase()] =
								str.substring(str.indexOf(':')+1, str.indexOf(';'));
							str = str.substring(str.indexOf(';')+1);
						}
						if (obj.rawstatus === '') obj.error = 'Empty response returned. Maybe no connection to unit?';
						if (obj.error && obj.onError) obj.onError(data, obj.error);
					});
			}
			else {
				obj.rawstatus = nCmd('tcpclient '+obj.ip+' '+obj.port+' '+asciiToHex('getStatus'));
				var str = obj.rawstatus;
				while (str.indexOf(';') != -1){
					obj[str.substring(0, str.indexOf(':')).toLowerCase()] =
						str.substring(str.indexOf(':')+1, str.indexOf(';'));
					str = str.substring(str.indexOf(';')+1);
				}
				if (obj.rawstatus === '') obj.error = 'Empty response returned. Maybe no connection to unit?';
				if (obj.error && onError) onError(response);
			}
			
		}
		
		obj.switchTo = function(stream){
			nCmd('tcpclient '+obj._ip+' '+asciiToHex('set:'+stream));
		}
		
		obj.print = function(){
			console.log(obj);
		}
		
		obj.popup = function(){
			var output = '';
			for (var property in obj) {
				if (typeof obj[property] != 'function') output += property + ': ' + obj[property]+'; \n\n';
			}
			alert(output);
		}
		
		return obj;
		
	}
	
	var nSeries = vSeries;
	
	function Projector(ip, port, onError){
			
		var obj = {};
		
		obj._ip = ip;
		obj.ip = ip;
		obj.error = false;
		obj.port = !undef(port) ? port : '4998';
		obj.onError = !undef(onError) ? onError : noop;
		
		obj.setPort = function(port){
			obj.port = port;
		}
		
		obj.getRelayStatus = function(async){
			
			if (async){
				nCmdAsync('tcpclient '+obj.ip+' '+obj.port+' '+asciiToHex('getstate,'+'1:1'+'\r')
					+' '+asciiToHex('getstate,'+'1:2'+'\r')+' '+asciiToHex('getstate,'+'1:3'+'\r'),
					'_data1', function(data){
						obj.relay1state = parseInt(data.substring(10,11));
						obj.relay2state = parseInt(data.substring(22,23));
						obj.relay3state = parseInt(data.substring(data.length-2));
					});
			}
			else {
				obj.rawstatus = nCmd('tcpclient '+obj.ip+' '+obj.port+' '+asciiToHex('getstate,'+'1:1'+'\r')
					+' '+asciiToHex('getstate,'+'1:2'+'\r')+' '+asciiToHex('getstate,'+'1:3'+'\r'));
				obj.relay1state = parseInt(obj.rawstatus.substring(10,11));
				obj.relay2state = parseInt(obj.rawstatus.substring(22,23));
				obj.relay3state = parseInt(obj.rawstatus.substring(obj.rawstatus.length-2));
			}
			
		}
		
		obj.setRelay1 = function(state){
			nCmd('tcpclient '+obj.ip+' '+obj.port+' '+asciiToHex('setstate,1:1,'+state+'\r'));
		}
		obj.setRelay2 = function(state){
			nCmd('tcpclient '+obj.ip+' '+obj.port+' '+asciiToHex('setstate,1:2,'+state+'\r'));
		}
		obj.setRelay3 = function(state){
			nCmd('tcpclient '+obj.ip+' '+obj.port+' '+asciiToHex('setstate,1:3,'+state+'\r'));
		}
		
		obj.up = function(){
			nCmd('tcpclient '+obj.ip+' '+obj.port+' '+asciiToHex('setstate,1:1,1\r'));
			nCmd('tcpclient '+obj.ip+' '+obj.port+' '+asciiToHex('setstate,1:2,1\r'));
			nCmd('tcpclient '+obj.ip+' '+obj.port+' '+asciiToHex('setstate,1:3,0\r'));
			nCmd('tcpclient '+obj.ip+' '+obj.port+' '+asciiToHex('setstate,1:2,0\r'));
		}
		
		obj.down = function(){
			nCmd('tcpclient '+obj.ip+' '+obj.port+' '+asciiToHex('setstate,1:3,1\r'));
			nCmd('tcpclient '+obj.ip+' '+obj.port+' '+asciiToHex('setstate,1:1,1\r'));
			nCmd('tcpclient '+obj.ip+' '+obj.port+' '+asciiToHex('setstate,1:2,0\r'));
			nCmd('tcpclient '+obj.ip+' '+obj.port+' '+asciiToHex('setstate,1:3,0\r'));
		}
		
		obj.print = function(){
			console.log(obj);
		}
		
		obj.popup = function(){
			var output = '';
			for (var property in obj) {
				if (typeof obj[property] != 'function') output += property + ': ' + obj[property]+'; ';
			}
			alert(output);
		}
		
		return obj;
		
	}
	
	function LightController(ip, port, onError){
			
		var obj = {};
		
		obj._ip = ip;
		obj.ip = ip;
		obj.error = false;
		obj.port = !undef(port) ? port : '2101';
		obj.onError = !undef(onError) ? onError : noop;
		
		obj.setPort = function(port){
			obj.port = port;
		}
		
		obj.getStatus = function(async){
			
			if (async){
				nCmdAsync('tcpclient '+obj.ip+' '+obj.port+' '+'fd 01 00 01 20',
					'_data1', function(data){
						if (data.substr(0,1) == 'U') data = data.substring(1);
						obj.channel1 = asciiToHex(data.substr(0,1));
						obj.channel2 = asciiToHex(data.substr(1,1));
						obj.channel3 = asciiToHex(data.substr(2,1));
						console.log('brightstatus: ', asciiToHex(data.substr(0,1))+' '
							+asciiToHex(data.substr(1,1))+' '+asciiToHex(data.substr(2,1)));
						console.log('bs2: ', data);
					});
			}
			else {
				var data = nCmd('tcpclient '+obj.ip+' '+obj.port+' '+'fd 01 00 01 20');

				if (data.substr(0,1) == 'U') data = data.substring(1);
				obj.channel1 = asciiToHex(data.substr(0,1));
				obj.channel2 = asciiToHex(data.substr(1,1));
				obj.channel3 = asciiToHex(data.substr(2,1));

			}
			
		}
		
		obj.setBrightness = function(brightness, channel, buffer){
			var brightness = !undef(brightness) ? brightness : '00';
			var channel = !undef(channel) ? channel : '00';
			var buffer = !undef(buffer) ? buffer : '01';
			if (brightness.length === 1) brightness = '0' + brightness;
			if (channel.length === 1) channel = '0' + channel;
			if (buffer.length === 1) buffer = '0' + buffer;
			nCmd('tcpconnectdelay 10\n'+
				'tcpclient '+obj.ip+' '+obj.port+' '+'fd '+channel+' '+brightness+' '+buffer);
		}
		
		obj.setBrightnessRT = function(brightness, channel, buffer){
			var ms = (new Date).getTime();
			if (ms % 5 !== 0) return;
			var brightness = !undef(brightness) ? brightness : '00';
			var channel = !undef(channel) ? channel : '00';
			var buffer = !undef(buffer) ? buffer : '01';
			if (brightness.length === 1) brightness = '0' + brightness;
			if (channel.length === 1) channel = '0' + channel;
			if (buffer.length === 1) buffer = '0' + buffer;
			ws.send('tcpclient '+obj.ip+' '+obj.port+' '+'fd '+channel+' '+brightness+' '+buffer);
		}
		
		obj.setBrightnessAsync = function(brightness, channel, buffer){
			var ms = (new Date).getTime();
			if (ms % 500 !== 0) return;
			var brightness = !undef(brightness) ? brightness : '00';
			var channel = !undef(channel) ? channel : '00';
			var buffer = !undef(buffer) ? buffer : '01';
			nCmdAsync('tcpconnectdelay 1\n'+
				'tcpclient '+obj.ip+' '+obj.port+' '+'fd '+channel+' '+brightness+' '+buffer);
		}
		
		obj.effect = function(){
			nCmdAsync('tcpconnectdelay 1\n'+
				'tcpclient '+obj.ip+' '+obj.port+' '+'fd a0 fd 04');
		}
		
		obj.stopeffect = function(){
			nCmdAsync('tcpconnectdelay 1\n'+
				'tcpclient '+obj.ip+' '+obj.port+' '+'fd 96');
		}

		obj.print = function(){
			console.log(obj);
		}
		
		obj.popup = function(){
			var output = '';
			for (var property in obj) {
				if (typeof obj[property] != 'function') output += property + ': ' + obj[property]+'; ';
			}
			alert(output);
		}
		
		return obj;
		
	}
	
	function asciiToHex(str){
		var arr = [];
		for (var i = 0, l = str.length; i < l; i ++) {
			var hex = Number(str.charCodeAt(i)).toString(16);
			arr.push(hex.length > 1 && hex || "0" + hex);
		}
		return arr.join(' ');
	}
	
	function hexToAscii(hex) {
		var _hex = hex.toString();
		var str = '';
		for (var i = 0; i < _hex.length; i += 2)
			str += String.fromCharCode(parseInt(_hex.substr(i, 2), 16));
		return str;
	}
	
	