let calibrationParameters = [
	{
		moduleType: "calibrationData",
		url: "../cgi/cgi_Calibration.py",
		data: {
			requestType: "read_calibration",
		}
	},
]

$(document).ready(function(){
	function loadCalibrationData() {
		calibrationParameters.forEach(function (parameter) {
			$.ajax({
				type: "Get",
				url: parameter.url,
				async: false,
				contentType: 'application/json',
				dataType: 'json',
				headers: {
					'Access-Control-Allow-Origin': '*'
				},
				data: parameter.data,
				success: function (response) {
					switch (parameter.moduleType) {
						case "calibrationData":
							// calibrationData = response[0];
							// console.log(response);
							break;
					}
				},
				error: function (ex) {
					$("#test").html(ex.responseText)
				}
			});
		});
		if ($("#demo_dr_demultiplexer_0").val() == -1 || $("#demo_dr_demultiplexer_1").val() == -1 || $("#demo_dr_demultiplexer_2").val() == -1 || $("#demo_dr_demultiplexer_3").val() == -1) {
			$("#updateView").prop("disabled", true);
		}
	}

	loadCalibrationData();

	if ($("#cal-wavelength-slider").next().length == 0) {
		loadSlider(calibrationWaveSliderObj, "#cal-wavelength-slider", "#cal-wavelength-setvalue", 0, 100, 25, caliSliders, true, 1, true, 0, false);
		loadSlider(calibrationFreqSliderObj, "#cal-frequency-slider", "#cal-frequency-setvalue", 0, 100, 25, caliSliders, true, 1, true, 0, false);
		$("#cal-wavelength-setvalue").prop("disabled", true);
		$("#cal-frequency-setvalue").prop("disabled", true);
	}

    $('#start-btn').click(function () {
		$('.loaderDiv').css('display', 'block');
		$.ajax({
			type: "Get",
			url: "../cgi/cgi_Calibration.py",
			contentType: 'application/json',
			dataType: 'json',
			headers: {
				'Access-Control-Allow-Origin': '*'
			},
			data: {
				requestType: "start_calibration"
			},
			success: function (response) {
				$('.loaderDiv').css('display', 'none');
				if(response.status == "fail"){
					showModal('Error', response.description);
				}
				else{
					showModal('Success', response.description);
				}
			},
			error: function (ex) {
				$("#test").html(ex.responseText);
				
			}
		
		});
	});

    $("#cal-wavelength-setvalue").change(function () {
		var value = parseFloat(parseFloat($(this).val()).toFixed(2));
		calibrationWaveSliderObj.slider.setValues(value, 100)
	});

	$("#cal-frequency-setvalue").change(function () {
		var value = parseFloat(parseFloat($(this).val()).toFixed(2));
		calibrationFreqSliderObj.slider.setValues(value, 100)
	});


	$("#operationDropdown").change(function () {
		if ($(this).val() == "OPS") {
			$("#demultiplexer1Value").text('Scope detected');
		} else {
			$("#demultiplexer1Value").text('OSA detected');
		}

		if (parseInt($(this).val()) == -1) {
			$("#cal-wavelength-setvalue").prop("disabled", true);
			$("#cal-frequency-setvalue").prop("disabled", true);
			calibrationWaveSliderObj.slider.conf.isIDrive = true;
			calibrationFreqSliderObj.slider.conf.isIDrive = true;
			
		} else {
			$("#cal-wavelength-setvalue").prop("disabled", false);
			$("#cal-frequency-setvalue").prop("disabled", false);
			calibrationWaveSliderObj.slider.conf.isIDrive = false;
			calibrationFreqSliderObj.slider.conf.isIDrive = false;
		}
	});

    $("#cal-wavelength-setvalue-debug").change(function () {
		var value = parseFloat(parseFloat($(this).val()).toFixed(2));
		calibrationWaveSliderObj.slider.setValues(value, 100)
	});
    
})