var g_deviceName = sessionStorage.getItem("configData");
var isTimeOutFunction = false;
var isTimeChartCalled = false;
var isGraphThere = false;

$(document).ready(function () {
	loadCurrentSettings();
	function getStat(status) {
		if (status == 0 || status == null)
			stateImg = "../images/redLed.png";
		else if (status == 1)
			stateImg = "../images/greenLed.png";
		else
			stateImg = "../images/warningLed.png";
		return stateImg;
	}
	function loadCurrentSettings() {
		$.ajax({
			type: "Get",
			url: "../cgi/cgi_statusPageManager.py",
			contentType: 'application/json',
			dataType: 'json',
			headers: { 'Access-Control-Allow-Origin': '*' },
			data: { requestType: "read_currentSettingsLYRATL" },
			success: function (response) {
				data = response;
				//current settings						
				$("#demultiplexDataValue1").append(' ' + data.Demultiplexer_Output_1);
				$("#demultiplexDataValue2").append(' ' + data.Demultiplexer_Output_2);
				$("#demultiplexDataValue3").append(' ' + data.Demultiplexer_Output_3);
				$("#demultiplexDataValue4").append(' ' + data.Demultiplexer_Output_4);
				$("#featuresData1").append(' ' + (data["laser_data"][0] ?? 0));
				$("#featuresData2").append(' ' + (data["laser_data"][1] ?? 0));
				$("#featuresData3").append(' ' + (data["laser_data"][2] ?? 0));
				$("#featuresData4").append(' ' + (data["laser_data"][3] ?? 0));

				//states
				$.each(data, function (key, value) {
					var stateImg = "";
					var name = "";
					var img = "";
					// if(value==0 || value==null)
					// 		stateImg="../images/redLed.png";
					// else if(value==1)
					// 	stateImg="../images/greenLed.png";
					// else
					// 	stateImg="../images/warningLed.png";
					var array = [];
					switch (key) {
						case "ethernet":
							name = "Ethernet Connection";
							img = "../images/Ethernet-Connection.png";
							// array = 
							break;
						case "remote":
							name = "Remote Connection";
							img = "../images/Remote-Connection.png";
							break;
						// case "Demux Output 1":
						// 	name="Demux Output 1";
						// 	img ="../images/Demux-Output.png";
						// 	break;
						// case "Demux Output 2":
						// 	name="Demux Output 2";
						// 	img ="../images/Demux-Output.png";
						// 	break;
						// case "Demux Output 3":
						// 	name="Demux Output 3";
						// 	img ="../images/Demux-Output.png";
						// 	break;
						// case "Demux Output 4":
						// 	name="Demux Output 4";
						// 	img ="../images/Demux-Output.png";
						// 	break;
						case "Slave":
							name = "Laser";
							img = "../images/Slave-Section.png";
							// array = 
							break;
						case "VOA":
							name = "Variable Optical Attenuator";
							img = "../images/VOA-Section.png";
							break;
						case "Coupler":
							name = "Coupler";
							img = "../images/1_4-Coupler.png";
							break;
						case "Master":
							name = "Master Laser";
							img = "../images/Master-Section.png";
							break;
						case "tec":
							name = "TEC";
							img = "../images/TEC.png";
							// array = 
							break;
						case "Fan":
							name = "Fan";
							img = "../images/Fans.png";
							// array = 
							break;
						case "Master":
							name = "Master";
							img = "../images/TEC.png";
							break;
						case "RF Synthesizer":
							name = "RF Synthesizer";
							img = "../images/RF-Synth.png";
							break;
						case "Temperature":
							name = "Temperature";
							img = "../images/Temperature.png";
							// array = 
							break;
						case "PowerSupply":
							name = "Power Supply";
							img = "../images/Power-Supply.png";
							// array = 
							break;
						case "VOA":
							name = "VOA";
							img = "../images/VOA-Section.png";
							break;
						case "Date Modulation":
							name = "Date Modulation";
							img = "../images/Date-Modulation.png";
							break;
					}
					if (name != "") {
						$("#ethernetAppendDiv").append(`<div class="status-icon-space"><div style="display: flex;"><img src="${img}"><span class="led-space" /><label>${name}</label>
							</div>
								${value.map((item) => `<div>
									<div style="display: flex; margin-left : 50px;" >
										<img class="led-space" src="${getStat(item.status)}" /> 
										<span>${item.name}</span> 
									</div>
								</div>`).join('')}
							
							`);
						$("#ethernetAppendDivResponive").append('<div class="status-icon-space"><img src="' + img + '"><img class="led-space" src="' + stateImg + '" /><label>' + name + '</label></div>');
					}
				});

			},
			error: function (ex) {
				$("#test").html(ex.responseText);
			}
		});
	}
	let selectedMetric = "T";

	$('.clsChartType').change(function () {
		isTimeOutFunction = false;
		selectedMetric = $(this).val();
		console.log($(this).val(), "value");
		ShowChart();
	});

	function ShowChart() {
		var scale = $("input[name='chartSclae']:checked").val();
		InitializeChart(g_deviceName, selectedMetric, scale);
	}
	$('.clsChartScale').change(function () {
		isTimeOutFunction = false;
		ShowChart();
	});

});
var mymodal = $('#myModal');
$("#switch_output").change((e) => {
	const checkbox = document.getElementById('myCheckbox');
	$.ajax({
		type: "Get",
		url: "../cgi/cgi_statusPageManager.py",
		contentType: 'application/json',
		dataType: 'json',
		headers: { 'Access-Control-Allow-Origin': '*' },
		data: { requestType: "output" },
		success: (response) => {
			mymodal.find('.modal-title').text('Turn All Outputs OFF');
			mymodal.find('.modal-body').text(response.message);
			mymodal.modal('show');
			setTimeout(() => {
				mymodal.modal("hide")
			}, autoClose)
		}
	})
})
$("#switch_module").change((e) => {
	const checkbox = document.getElementById('myCheckbox');
	$.ajax({
		type: "Get",
		url: "../cgi/cgi_statusPageManager.py",
		contentType: 'application/json',
		dataType: 'json',
		headers: { 'Access-Control-Allow-Origin': '*' },
		data: { requestType: "module" },
		success: (response) => {
			mymodal.find('.modal-title').text('Turn Module OFF');
			mymodal.find('.modal-body').text(response.message);
			mymodal.modal('show');
			setTimeout(() => {
				mymodal.modal("hide")
			}, autoClose)
		}
	})
})