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

$(document).ready(function(){
	loadCurrentSettings();
    function loadCurrentSettings(){
		$.ajax({
				type: "Get",
				url: "../cgi/cgi_statusPageManager.py",
				contentType: 'application/json',
				dataType: 'json',
				headers: { 'Access-Control-Allow-Origin': '*' },
				data:{requestType:"read_currentSettingsAirbus"},
				success: function (response) {
					data=response;
					//current settings						
						$("#demultiplexDataValue1").append(' '+ (data?.Demultiplexer_Output_1 ?? 0));
						$("#demultiplexDataValue2").append(' '+ (data?.Demultiplexer_Output_2 ?? 0));
						$("#demultiplexDataValue3").append(' '+ (data?.Demultiplexer_Output_3 ?? 0));
						$("#demultiplexDataValue4").append(' '+ (data?.Demultiplexer_Output_4 ?? 0));
						$("#featuresData1").append(' '+(data.wavelength ?? 1400));
						$("#featuresData2").append(' '+(data.FSR ?? 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";

						switch(key){
							case "ethernet":
								name="Ethernet Connection";
								img ="../images/Ethernet-Connection.png";
								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="Slave Laser 1";
								img ="../images/Slave-Section.png";
								break;
							case "Slave2":
								name="Slave Laser 2";
								img ="../images/Slave-Section.png";
								break;
							case "VOA":
								name="Variable Optical Attenuator 1";
								img ="../images/VOA-Section.png";
								break;
							case "VOA2":
								name="Variable Optical Attenuator 2";
								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 "tec1":
								name="TEC 1";
								img ="../images/TEC.png";
								break;
							case "tec2":
								name="TEC 2";
								img ="../images/TEC.png";
								break;
							case "tec3":
								name="TEC 3";
								img ="../images/TEC.png";
								break;
							case "Fan":
								name="Fan";
								img ="../images/Fans.png";
								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";
								break;
							case "PowerSupply":
								name="Power Supply";
								img ="../images/Power-Supply.png";
								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"><img src="'+img+'"><img class="led-space" src="'+stateImg+'" /><label>'+name+'</label></div>');
							$("#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 = "I";

    $('.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)=>{
			// swal("", response.message, "success");
			mymodal.find('.modal-title').text('Turn Module OFF');
            mymodal.find('.modal-body').text(response.message);
			mymodal.modal('show');
            setTimeout(()=> {
                mymodal.modal("hide")
            },autoClose)
		}
	})
})