<script type="text/x-red" data-help-name="Logger">
	<p>
	Log message for a set number of times before turning off.
	Allows for dynamic traces without the need to deploy.
	Prevents console and log being flood by loads of messages be having a set limit.
	</p>
</script>

<script type="text/x-red" data-template-name="Logger">

	<div class="form-row">
		<label for="node-input-name"><i class="fa fa-tag"></i> Name </label>
		<input type="text" id="node-input-name" placeholder="Name">
	</div>
	<div class="form-row">
		<label for="node-input-count"><i class="fa fa-tag"></i> Number of messages </label>
 		<input type="number" min="1" max="100000" step="10" id="node-input-count">
	</div>
	<div class="form-row">
		<input type="checkbox" id="node-input-sendOutputLog" style="display: inline-block; width: auto; vertical-align: top;">
		<label for="node-input-sendOutputLog" style="width: auto">Send to log port</label>
	</div>
	<div class="form-row">
		<input type="checkbox" id="node-input-active" style="display: inline-block; width: auto; vertical-align: top;">
		<label for="node-input-active" style="width: auto">Initially On</label>
	</div>
	<div class="form-row">
		<input type="checkbox" id="node-input-sendConsoleLog" style="display: inline-block; width: auto; vertical-align: top;">
		<label for="node-input-sendConsoleLog" style="width: auto">Send to debug console</label>
	</div>

</script>

<script type="text/javascript">
   	const nodeName="Logger";
	RED.nodes.registerType(nodeName,{
		category: 'function',
		defaults: {
			name: {value:"",required:false},
			count: {value:100,required:true},
			sendOutputLog: {value:false,required:true},
			sendConsoleLog: {value:false,required:true},
			active: {value:true,required:true}
		},
		inputs:1,
		inputLabels: "Message",
		outputs:2,
		outputLabels: ["Out","Log"],
		icon: "setting.png",
		label: function() {
			return this.name||this._(nodeName);
		},
		labelStyle: function() {
			return "node_label_italic";
		},
		oneditprepare: function() {
			if(typeof this.sendOutputLog === 'undefined') {
				this.sendOutputLog = false;
				$("#node-config-input-sendOutputLog").prop("checked",false);
			}
			if(typeof this.sendConsoleLog === 'undefined') {
				this.sendConsoleLog = false;
				$("#node-config-input-sendConsoleLog").prop("checked",false);
			}
			if(typeof this.active === 'undefined') {
				this.active = true;
				$("#node-config-input-active").prop("checked",true);
			}
		},
		oneditsave: function() {
		},
		oneditresize: function(size) {
		},
		button: {
			enabled: function() {
				return !this.changed;
			},
		   	onclick: function() {
				if (this.changed) {
					return RED.notify(RED._(nodeName+" undeployed changes"),"warning");
				}
				var label = this._def.label.call(this);
				if (label.length > 30) {
					label = label.substring(0,50)+"...";
				}
				label = label.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
				var node = this;
				
				function sendCommand(element,action) {
				   	$(element).dialog("close");
					$.get( "/"+nodeName+"/"+node.id+"/"+action )
					.done(function(json) {
					console.log(JSON.stringify(json));
					   	RED.notify(node._(nodeName+" signal success",{label:label}),{type:"success",id:"Load Injector"});
					   	$('<div></div>').appendTo('body').html(JSON.stringify(json))
					   	.dialog({
	  						modal: true,
	  						title: (node.name||nodeName)+" "+action,
	  						zIndex: 10000,
	  						autoOpen: true,
   							width: 'auto',
   							resizable: false,
							buttons: {
								close: function (event, ui) {
									$(this).remove();
								}
							}
						});
					}).fail(function( jqXHR, textStatus, error ) {
				   				if (jqXHR.status === 404) {
	 								RED.notify(node._(nodeName+" signal not deployed"),"error");
								} else if (jqXHR.status === 500) {
   									RED.notify(node._(nodeName+" signal inject failed with error "+textStatus||error||""),"error");
								} else if (jqXHR.status === 0) {
 									RED.notify(node._(nodeName+" signal no response"),"error");
				   				} else {
									RED.notify(node._(nodeName+" signal unexpected status:"+jqXHR.status+" message:"+textStatus+" "+error),"error");
								}
					});
				}
				$('<div></div>').appendTo('body').html('<div>Choose Action</div>')
  				.dialog({
	  				modal: true, title: (node.name||nodeName), zIndex: 10000, autoOpen: true,
   					width: 'auto', resizable: false,
					buttons: {
						"On": function () {
							sendCommand(this,"setOn");
						},
						"Off": function () {
							sendCommand(this,"setOff");
						},
					},
					close: function (event, ui) {
						$(this).remove();
					}
				});
			}
		}
 
	});
</script>