let demoParameters = [
	{
		moduleType: "readBand",
		url: "../cgi/cgi_BandSelect.py",
		data: {
			requestType: "read_band_select",
			id: 1
		}
	}
]
let id ;
let modeData = {}

function loadDemoData() {
	demoParameters.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 "readBand":
						modeData = response
						$("#tx_band").val(response.tx_band);
						$("#rx_band").val(response.rx_band);
						if(response.enable_comb == 1){
							$("#enableComb").prop("checked", true);
						}
						else{
							$("#enableComb").prop("checked", false);
						}
						id = response.id;
						break;
				}
				$("#graph-water-mark").addClass("d-none");
				$("#demoMode").removeClass("d-none");
			},
			error: function (ex) {
				$("#graph-water-mark").addClass("d-none");
				$("#demoMode").removeClass("d-none");
			}
		});
	});
}

$("input[name='enableComb']").change(() => {
	let value = $("#enableComb").prop("checked") == true ? 1 : 0;
	saveBand(value,"set_enable_comb");
})

function saveBand(value,requestType) {
	$.ajax({
		type: "Get",
		url: "../cgi/cgi_BandSelect.py",
		contentType: 'application/json',
		dataType: 'json',
		headers: {
			'Access-Control-Allow-Origin': '*'
		},
		data: {
			"requestType" : requestType,
            "id": id,
            "value": value
		},
		success: function (response) {
			if(response.status == "fail"){
				swal("Error", response.description,"error")
			}
			else{
				if (requestType == "set_enable_comb" && value == 1){
					getWarmupTime()
					swal({
						title: "Success",
						html: true,
						text: response.description + "<br>Warmup Timer Started",
						type: "success",
					});
				}
				else if (requestType == "set_enable_comb" && value == 0){
					getWarmupTime()
					swal({
						title: "Warning",
						html: true,
						text: response.description + "<br>Disabled Comb",
						type: "warning",
					});
				}
				else{
					swal("Success", response.description,"success")
				}
			}
			$("#graph-water-mark").addClass("d-none");
			$("#demoMode").removeClass("d-none");
			loadDemoData();
		},
		error: function (ex) {
			$("#graph-water-mark").addClass("d-none");
			$("#demoMode").removeClass("d-none");
		}
	});
}
$(document).ready(function () {
	loadDemoData();

	$(".save-btn").click(function () {
		switch ($(this).val()) {
			case "BandSave":
				saveBand($("#tx_band").val(), "save_tx_band");
				saveBand($("#rx_band").val(), "save_rx_band");


		}
	});

	//cancel
	$("#demo-cancel-btn").click(function () {
		let i = 0;
		if($("#tx_band").val() != modeData.tx_band){
			$("#tx_band").val(modeData.tx_band);
			i = i+1
		}
		if($("#rx_band").val() != modeData.rx_band){
			$("#rx_band").val(modeData.rx_band);
			i = i+1
		}
		// let enable_comb =$("input[name='enableComb']:checked").val() == "yes" ? 1 : 0;
		// if(enable_comb != modeData.enable_comb){
		// 	$("input[name='enableComb']").each(function () {
		// 		console.log($(this).attr('value'))		
		// 		if($(this).attr('value') == modeData?.enable_comb) {
		// 			$(this).prop('checked',true);
		// 		}
		// 	});
		// 	i = i+1
		// }

		if (i > 0 ){
			showModal("Alert!", "Changes Reversed");
		}
		else{
			showModal("Alert!", "No Changes Detected")
		}
	});


	$("#restore-demo-btn").click(() => {
		let parameter = { url: "../cgi/cgi_RestoreDefaultSettings.py", data: { requestType: "restore_demo" } };
		$.ajax({
			url: parameter.url,
			type: "get",
			data: parameter.data,
			contentType: 'application/json',
			dataType: 'json',
			headers: {
				'Access-Control-Allow-Origin': '*'
			},
			success: function (response) {
				showModal('Success', response.response);
			}
		})
	})

	$(".accordion-head").click(function (e) {
		e.preventDefault();
		var $this = $(this);
		if ($this.hasClass('icon') == true) {
			$("html, body").animate({
				scrollTop: ($this.offset().top)
			}, 1000);
			// $('html, body').animate({
			// 	scrollTop: $("#demo-demux-tab").offset().top
			// }, 1500);
		}
	})
})