/******************************************************************************
LG M2280DM module for SVSi Panel Builder scripting.

Example Startup Script:
myLG = LG.M2280DM(); // create object

*******************************************************************************/

	//define namespace if it doesn't exist
	if (!Modules.LG) Modules.LG = {};

	/* 
	* Command list displays in the dropdowns in the panel builder script editor under 'modules'
	*/
	module_command_list = [
		{name: 'Constructor', func: 'myModule = Modules.LG.M2280DM({ip: "169.254.1.2", port: 50004});', comment: '//create object'},
		{name: 'Monitor On', func: 'myModule.monitorOn();', comment: '//turn on monitor'},
		{name: 'Monitor Off', func: 'myModule.monitorOff();', comment: '//turn off monitor'},
		{name: 'Screen Mute On', func: 'myModule.screenMuteOn();', comment: '//turn on screen mute'},
		{name: 'Screen Mute Off', func: 'myModule.screenMuteOff();', comment: '//turn off screen mute'},
		{name: 'Video Mute On', func: 'myModule.videoMuteOn();', comment: '//turn on video mute'},
		{name: 'Aspect Ratio', func: 'myModule.aspectRatio("4:3");', comment: '//valid options for LG Monitor aspect ratio are normal, 4:3, wide screen,wide, 16:9, zoom, original, 14:9, just scan, full wide, and 10-1f for CinemaZoom'},
		{name: 'Volume Mute On', func: 'myModule.volumeMuteOn();', comment: '//turn on volume mute'},
		{name: 'Volume Mute Off', func: 'myModule.volumeMuteOff();', comment: '//turn off volume mute'},
		{name: 'Set Volume', func: 'myModule.setVolume("00");', comment: '//set monitor volume'},
		{name: 'Get Volume', func: 'myModule.getVolume(function(volume){ thevol = volume; });', comment: '//function passed in will run on complete'},
		{name: 'Set Contrast', func: 'myModule.setContrast("00");', comment: '//set monitor contrast'},
		{name: 'Get Contrast', func: 'myModule.getContrast(function(contrast){ thecont = contrast; });', comment: '//function passed in will run on complete'},
		{name: 'Set Brightness', func: 'myModule.setBrightness("00");', comment: '//set monitor brightness'},
		{name: 'Get Brightness', func: 'myModule.getBrightness(function(brightness){ thebright = brightness; });', comment: '//function passed in will run on complete'},
		{name: 'Set Color', func: 'myModule.setColor("00");', comment: '//set monitor color'},
		{name: 'Get Color', func: 'myModule.getColor(function(color){ thecolor = color; });', comment: '//function passed in will run on complete'},
		{name: 'Set Tint', func: 'myModule.setTint("00");', comment: '//set monitor tint'},
		{name: 'Get Tint', func: 'myModule.getTint(function(tint){ thetint = tint; });', comment: '//function passed in will run on complete'},
		{name: 'Set Sharpness', func: 'myModule.setSharpness("00");', comment: '//set monitor sharpness'},
		{name: 'Get Sharpness', func: 'myModule.getSharpness(function(sharpness){ thesharp = sharpness; });', comment: '//function passed in will run on complete'},
		{name: 'Set Treble', func: 'myModule.setTreble("00");', comment: '//set monitor treble'},
		{name: 'Get Treble', func: 'myModule.getTreble(function(treble){ thetreble = treble; });', comment: '//function passed in will run on complete'},
		{name: 'Set Bass', func: 'myModule.setBass("00");', comment: '//set monitor bass'},
		{name: 'Get Bass', func: 'myModule.getBass(function(bass){ thebass = bass; });', comment: '//function passed in will run on complete'},
		{name: 'Remote Control Lock On', func: 'myModule.remoteControlLockOn();', comment: '//turn on remote control lock'},
		{name: 'Remote Control Lock Off', func: 'myModule.remoteControlLockOff();', comment: '//turn off remote control lock'},
		{name: 'On Screen Display On', func: 'myModule.onscreenDisplayOn();', comment: '//turn on onscreen display'},
		{name: 'On Screen Display Off', func: 'myModule.onscreenDisplayOff();', comment: '//turn off onscreen display'},
		{name: 'Set Audio Balance', func: 'myModule.setAudioBalance("00");', comment: '//set monitor audio balance'},
		{name: 'Get Audio Balance', func: 'myModule.getAudioBalance(function(balance){ thebal = balance; });', comment: '//function passed in will run on complete'},
		{name: 'Set Color Temperature', func: 'myModule.setColorTemperature("00");', comment: '//set monitor color temperature'},
		{name: 'Get Color Temperature', func: 'myModule.getColorTemperature(function(temperature){ thetemp = temperature; });', comment: '//function passed in will run on complete'},
		{name: 'Set Energy Saving', func: 'myModule.setEnergySaving("00");', comment: '//set monitor energy saving'},
		{name: 'Get Energy Saving', func: 'myModule.getEnergySaving(function(energy){ theenergy = energy; });', comment: '//function passed in will run on complete'},
		{name: 'Set Backlight', func: 'myModule.setBacklight("00");', comment: '//set monitor backlight'},
		{name: 'Get Backlight', func: 'myModule.getBacklight(function(backlight){ thelight = backlight; });', comment: '//function passed in will run on complete'},
		{name: 'Monitor Auto Configure', func: 'myModule.monitorAutoConfigure();', comment: '//auto configure monitor'},
		{name: 'Set Channel', func: 'myModule.setChannel("00", "03");', comment: '//set monitor channel'},
		{name: 'Set Input', func: 'myModule.setInput("00");', comment: '//set monitor input'},
		{name: 'Set IR Key Code', func: 'myModule.setIRKeyCode("00");', comment: '//set IR key code'},
		{name: 'Add Program', func: 'myModule.programAdd();', comment: '//program add'},
		{name: 'Skip Program', func: 'myModule.programSkip();', comment: '//program skip'},
		
		{name: 'Print', func: 'myModule.print();', comment: '//Prints this module object and all of its properties to the Javascript console.'},
		{name: 'Popup', func: 'myModule.popup();', comment: '//Displays an alert dialog with this module object and all of its properties.'},
	];
	
	/* 
	* M2280DM function returns an object representing an M2280DM unit
	* ip is the ip address, port is the port to open socket on,
	* onError is a function to be called on errors
	*/
	Modules.LG.M2280DM = function(opts){
			
		if (undef(opts)) opts = {};
		
		var obj = {};
		
		//properties
		obj.ip = opts.hasOwnProperty('ip') ? opts.ip : '169.254.1.2';
		obj.port = opts.hasOwnProperty('port') ? opts.port : '50004';
		obj.serialport = opts.hasOwnProperty('serialport') ? opts.serialport : '50004';
		obj.onError = opts.hasOwnProperty('onError') ? opts.onError : function(err){console.log(err)};
		obj.serial_interface = opts.hasOwnProperty('serial_interface') ? opts.serial_interface : false;
		obj.ir_interface = opts.hasOwnProperty('ir_interface') ? opts.ir_interface : false;
		obj.protocol = opts.hasOwnProperty('protocol') ? opts.protocol : 'tcp';
		obj._updateInterval = false;
		obj.connection = false;
		
		var printout = function(data){console.log(data)};
		
		obj.monitorOn = function(callback){
			if (undef(callback)) callback = printout;
			if (this.protocol == 'tcp') tcpclient(this.ip, this.serialport, asciiToHex('ka 0 01\r'), callback);
			if (this.protocol == 'udp') udpclient(this.ip, this.serialport, asciiToHex('ka 0 01\r'), callback);
			if (this.protocol == 'telnet') telnetclient(this.ip, this.serialport, asciiToHex('ka 0 01\r'), callback);
		};
		
		obj.monitorOff = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('ka 0 00\r'), callback);
		};
		
		obj.screenMuteOn = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('kd 0 01\r'), callback);
		};
		
		obj.screenMuteOff = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('kd 0 00\r'), callback);
		};
		
		obj.videoMuteOn = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('kd 0 10\r'), callback);
		};
		
		obj.aspectRatio = function(ratio, callback){
			if (undef(callback)) callback = printout;
			switch (ratio){
				case 'normal': case '4:3':
					tcpclient(this.ip, this.serialport, asciiToHex('kc 0 01\r'), callback);
					break;
				case 'wide screen': case 'wide': case '16:9':
					tcpclient(this.ip, this.serialport, asciiToHex('kc 0 02\r'), callback);
					break;
				case 'zoom':
					tcpclient(this.ip, this.serialport, asciiToHex('kc 0 04\r'), callback);
					break;
				case 'original':
					tcpclient(this.ip, this.serialport, asciiToHex('kc 0 06\r'), callback);
					break;
				case '14:9':
					tcpclient(this.ip, this.serialport, asciiToHex('kc 0 07\r'), callback);
					break;
				case 'just scan':
					tcpclient(this.ip, this.serialport, asciiToHex('kc 0 09\r'), callback);
					break;
				case 'full wide':
					tcpclient(this.ip, this.serialport, asciiToHex('kc 0 0B\r'), callback);
					break;
				case '10': case '11': case '12': case '13': case '14': case '15':
				case '1a': case '1b': case '1c': case '1d': case '1e': case '1f':
					tcpclient(this.ip, this.serialport, asciiToHex('kc 0 '+ratio+'\r'), callback);
					break;
				default:
					console.log('valid options for LG Monitor aspect ratio are normal, 4:3, wide screen,\
						wide, 16:9, zoom, original, 14:9, just scan, full wide, and 10-1f for CinemaZoom');
					break;
			}
		};
		
		obj.volumeMuteOn = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('ke 0 00\r'), callback);
		};
		
		obj.volumeMuteOff = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('ke 0 01\r'), callback);
		};
		
		obj.setContrast = function(value, callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('kg 0 '+value+'\r'), callback);
		};
		
		obj.getContrast = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('kg 0 ff\r'), callback);
		};
		
		obj.setVolume = function(value, callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('kf 0 '+value+'\r'), callback);
		};
		
		obj.getVolume = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('kf 0 ff\r'), callback);
		};
		
		obj.setBrightness = function(value, callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('kh 0 '+value+'\r'), callback);
		};
		
		obj.getBrightness = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('kh 0 ff\r'), callback);
		};
		
		obj.setColor = function(value, callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('ki 0 '+value+'\r'), callback);
		};
		
		obj.getColor = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('ki 0 ff\r'), callback);
		};
		
		obj.setTint = function(value, callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('kj 0 '+value+'\r'), callback);
		};
		
		obj.getTint = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('kj 0 ff\r'), callback);
		};
		
		obj.setSharpness = function(value, callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('kk 0 '+value+'\r'), callback);
		};
		
		obj.getSharpness = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('kk 0 ff\r'), callback);
		};
		
		obj.setTreble = function(value, callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('kr 0 '+value+'\r'), callback);
		};
		
		obj.getTreble = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('kr 0 ff\r'), callback);
		};
		
		obj.setBass = function(value, callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('ks 0 '+value+'\r'), callback);
		};
		
		obj.getBass = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('ks 0 ff\r'), callback);
		};
		
		obj.remoteControlLockOn = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('km 0 01\r'), callback);
		};
		
		obj.remoteControlLockOff = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('km 0 00\r'), callback);
		};
		
		obj.onscreenDisplayOn = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('kl 0 01\r'), callback);
		};
		
		obj.onscreenDisplayOff = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('kl 0 00\r'), callback);
		};
		
		obj.setAudioBalance = function(value, callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('kt 0 '+value+'\r'), callback);
		};
		
		obj.getAudioBalance = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('kt 0 ff\r'), callback);
		};
		
		obj.setColorTemperature = function(value, callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('xu 0 '+value+'\r'), callback);
		};
		
		obj.getColorTemperature = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('xu 0 ff\r'), callback);
		};
		
		obj.setEnergySaving = function(value, callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('jq 0 '+value+'\r'), callback);
		};
		
		obj.getEnergySaving = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('jq 0 ff\r'), callback);
		};
		
		obj.monitorAutoConfigure = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('ju 0 01\r'), callback);
		};
		
		obj.setChannel = function(valueA, valueB, callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('kf 0 '+valueA+' '+valueB+'\r'), callback);
		};
		
		obj.setBacklight = function(value, callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('mg 0 '+value+'\r'), callback);
		};
		
		obj.getBacklight = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('mg 0 ff\r'), callback);
		};
		
		obj.setInput = function(value, callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('xb 0 '+value+'\r'), callback);
		};
		
		obj.setIRKeyCode = function(value, callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('mc 0 '+value+'\r'), callback);
		};
		
		obj.programAdd = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('mb 0 01\r'), callback);
		};
		
		obj.programSkip = function(callback){
			if (undef(callback)) callback = printout;
			tcpclient(this.ip, this.serialport, asciiToHex('mb 0 00\r'), callback);
		};
		
		obj.print = function(){
			console.log(this);
		};
		
		obj.popup = function(){
			var output = '';
			for (var property in this) {
				if (typeof this[property] != 'function') output += property + ': ' + this[property]+'; \n\n';
			}
			alert(output);
		};
		
		return obj;
		
	};
	