document.addEventListener("DOMContentLoaded", function() {
	socket = io();
	const url = new URL (document.URL)
	var path = url.pathname
	var adds = false
	if (path == '/parameters') {
		adds = true
		console.log(adds)
	}
	socket.on('return', function(msg) {
		switch(Object.keys(msg)[0]) {
			case "curvecalc":
				appendAlert(msg.curvecalc, 'success');
				$("#hcurve").text(msg.curvecalc);
				$("#button-spinner").addClass("d-none");
				break;
			case "tempchange":
				appendAlert(msg.tempchange, msg.type);
				break;
		}
		console.log(msg)
	});
if (path != '/settings') {
    socket.on('data_update', function(msg) {
		switch(Object.keys(msg)[0]) {
		case "restarted":
				if (msg.restarted == 1) {
					restartModal.hide()
					errorRestart.cancel()
					appendAlert("servicestart", "success")
					socket.emit("client", { 'restarted' : 0 })
				}
				break;
	    	case "twitwo":
				$("#twi").text(msg.twitwo[0] + (adds ? " °C" : ""))
	    		$("#two").text(msg.twitwo[1] + (adds ? " °C" : ""))
				$("#deltat").text((msg.twitwo[1]-msg.twitwo[0]).toFixed(1) + (adds ? " °C" : ""))
				break;
			case "thitho":
				$("#thi").text(msg.thitho[0] + (adds ? " °C" : ""))
        		$("#tho").text(msg.thitho[1] + (adds ? " °C" : ""))
				break;
			case "pdps":
				$("#pdset").text(msg.pdps[0] + (adds ? " bar" : ""))
				$("#pdact").text(msg.pdps[1] + (adds ? " bar" : ""))
				$("#psset").text(msg.pdps[2] + (adds ? " bar" : ""))
				$("#psact").text(msg.pdps[3] + (adds ? " bar" : ""))
				break;
			case "tsatpd":
				$("#tsatpsset").text(msg.tsatpd[0] + (adds ? " °C" : ""))
				$("#tsatpsact").text(msg.tsatpd[1] + (adds ? " °C" : ""))
				$("#tsatpdset").text(msg.tsatpd[0] + (adds ? " °C" : ""))
				$("#tsatpdact").text(msg.tsatpd[1] + (adds ? " °C" : ""))
				break;
			case "eevlevel":
				$("#eevlevel").text(msg.eevlevel+" °")
				break;
	    	case "compinfo":
				$("#fact").text(msg.compinfo[0] + (adds ? " Hz" : ""))
				$("#fset").text(msg.compinfo[1] + (adds ? " Hz" : ""))
				$("#ccurr").text(msg.compinfo[2] + (adds ? " A" : ""))
				$("#cvolt").text(msg.compinfo[3] + (adds ? " V" : ""))
				$("#ctemp").text(msg.compinfo[4] + (adds ? "°C" : ""))
				compinfo = msg.compinfo
				break;
			case "fans":
				$("#fan1").text(msg.fans[0] + (adds ? "RPM" : ""))
				$("#fan2").text(msg.fans[1] + (adds ? "RPM" : ""))
				break;
			case "tao":
				$("#tao").text(msg.tao + (adds ? "°C" : ""))
				break;
			case "tdts":
				$("#td").text(msg.tdts[0] + (adds ? "°C" : ""))
				$("#ts").text(msg.tdts[1] + (adds ? "°C" : ""))
				break;
	    	case "pump":
				$("#pump").text(msg.pump.toLowerCase())
				$("#pump").attr("data-i18n", msg.pump.toLowerCase());
				break;
	    	case "threeway":
				$("#threeway").text(msg.threeway.toLowerCase())
				$("#threeway").attr("data-i18n", msg.threeway.toLowerCase());
				break;
	    	case "archerror":
				$("#archerror").text(msg.archerror)
				break;
	    	case "dtquiet":
				$("#deltatempquiet").text(parseFloat(msg.dtquiet).toFixed(1));
				break;
		    case "dtturbo":
				$("#deltatempturbo").text(parseFloat(msg.dtturbo).toFixed(1));
			break;
		    case "dtflimit":
				$("#deltatempflimit").text(parseFloat(msg.dtflimit).toFixed(1));
				break;
	    	case "aoodt":
        		$("#antionoffdeltatime").text(parseFloat(msg.aoodt).toFixed(1));
				break;
	    	case "delta":
				$("#delta").text(msg.delta);
				break;
		    case "flimiton":
				$("#flimiton").text(msg.flimiton)
				$("#flimitchange").val(msg.flimiton)
				switch (msg.flimiton) {
					case "0":
						$("#flrelay").text("off");
						$("#flrelay").attr("data-i18n", "off");
						break;
					case "1":
						$("#flrelay").text("on");
						$("#flrelay").attr("data-i18n", "on");
					break;
				}
				break;
		    case "tank":
				$("#ttemp").text(parseFloat(msg.tank).toFixed(1) + (adds ? " °C" : ""));
				break;
		    case "hcurve":
				$("#hcurve").text(parseFloat(msg.hcurve).toFixed(1) + (adds ? " °C" : ""));
				break;
		    case "dhw":
				$("#dhwsetpoint").text(msg.dhw + (adds ? " °C" : ""));
				if (!adds) {
                	dhwsetpoint = msg.dhw
                	$("#dhwtempval").val(msg.dhw)
				}
                break;
		    case "setpoint":
				$("#setpoint").text(parseFloat(msg.setpoint).toFixed(1) + (adds ? " °C" : ""));
				if (!adds) {
					$("#intempval").val(msg.setpoint)
					temp = msg.setpoint
				}
				break;
	    	case "intemp":
				$("#intemp").text(parseFloat(msg.intemp).toFixed(1) + (adds ? " °C" : ""));
				break;
		    case "outtemp":
				$("#outtemp").text(parseFloat(msg.outtemp).toFixed(1));
				break;
	    	case "humid":
				$("#humid").text(parseFloat(msg.humid).toFixed(1))
				break;
		    case "ltemp":
				$("#ltemp").text(msg.ltemp)
				break;
	    	case "antionoff":
				if(msg.antionoff == "1") {
				    $("#presetdiv").hide();
				    $("#antionoffdiv").show();
				}
				break;
	    	case "mode":
				$("#presetchange").val(msg.mode)
				switch (msg.mode) {
				    case "quiet":
						$("#mode").text(msg.mode)
						$("#quiet").html("<b><u>"+msg.mode.toUpperCase()+"</u></b>");
						$("#quiet-label").html("<b><u>"+msg.mode.toUpperCase()+"</u></b>");
						//$("#eco").text("Eco");
						//$("#eco-label").text("Eco");
						//$("#turbo").text("Turbo");
						//$("#turbo-label").text("Eco");
						break;
		    		case "eco":
						$("#mode").text(msg.mode)
						//$("#quiet").text("Quiet");
						//$("#quiet-label").text("Quiet");
                        $("#eco").html("<b><u>"+msg.mode.toUpperCase()+"</u></b>");
						$("#eco-label").html("<b><u>"+msg.mode.toUpperCase()+"</u></b>");
                        //$("#turbo").text("Turbo");
						//$("#turbo-label").text("Turbo");
						break;
				    case "turbo":
						$("#mode").text(msg.mode)
						//$("#quiet").text("Quiet");
						//$("#quiet-label").text("Quiet");
                		//$("#eco").text("Eco");
						//$("#eco-label").text("Eco");
                        $("#turbo").html("<b><u>"+msg.mode.toUpperCase()+"</u></b>");
   	                	$("#turbo-label").html("<b><u>"+msg.mode.toUpperCase()+"</u></b>")
						break;
		    		default:
						$("#quiet").text("Quiet");
						$("#quiet-label").text("Quiet");
						$("#eco").text("Eco");
						$("#eco-label").text("Eco");
						$("#turbo").text("Turbo");
						$("#turbo-label").text("Turbo");
				}
				break;
		    case "presetquiet":
				$("#presetquiet").text(msg.presetquiet);
				break;
	    	case "presetturbo":
				$("#presetturbo").text(msg.presetturbo);
				break;
	    	case "presetch":
				if(msg.presetch == "manual") {
				    $("#presetchange").show()
				    $("#preset").text(" Auto")
				} else if ( msg.presetch == "auto") {
				    $("#presetauto").show();
				    $("#preset").text("Manual")
				}
				break;
		    case "fflimit":
				if(msg.fflimit == "manual") {
		    		$("#flimit").text("Manual")
				    $("#flimitchange").show()
				} else if (msg.fflimit == "auto"){
				    $("#flimitauto").show()
				    $("#flimit").text("Auto")
				}
				break;
		    case "heatingcurve":
				if(msg.heatingcurve == "directly" ) {
		    		minch=25
				    maxch=55
				} else {
				    minch=12
				    maxch=30
				}
				break;
	  	}
	
		switch(msg.threeway) {
			case "CH":
            	if ( compinfo[0] > 0 ) {
               		updateFireStatus("ch","firestatus.heating");
                    updateFireStatus("dhw","firestatus.idle");
                } else {
                    updateFireStatus("ch","firestatus.idle");
                    updateFireStatus("dhw","firestatus.idle");
                }
        		break;
            case "DHW":
            	if ( compinfo[0] > 0 ) {
                	updateFireStatus("dhw","firestatus.heating");
                    updateFireStatus("ch","firestatus.idle");
                } else {
                    updateFireStatus("dhw","firestatus.idle");
                    updateFireStatus("ch","firestatus.idle");
                }
        	break;
		}

		switch(msg.pch) {
        	case "on":
				if (!adds) {
					if ($("#firestatus")[0].classList.contains("bi-snow") == true ) {
						toggleCSSclasses($("#firestatus")[0], "bi-fire", "bi-snow")
						$("#powerheat").val("pch");
						$("#water-decrement-button").removeAttr('disabled');
						$("#water-increment-button").removeAttr('disabled');
					} else if ($("#firestatus")[0].classList.contains("bi-fire") == false) {
						$("#firestatus")[0].classList.add("bi-fire");
						$("#powerheat").val("pch");
						$("#water-decrement-button").removeAttr('disabled');
						$("#water-increment-button").removeAttr('disabled');
					}
				}
                break;
            case "off":
				console.log(adds)
				if (!adds) {
					console.log("no adds")
					if ($("#firestatus")[0].classList.contains("bi-fire") == true ) {
						$("#firestatus")[0].classList.remove("bi-fire");
					}
					$("#powerheat").val("off");
					$("#water-decrement-button").attr('disabled','disabled');
					$("#water-increment-button").attr('disabled','disabled');
				}
                break;
            default:
        }
        switch(msg.pcool) {
        	case "on":
				if (!adds) {
					if ($("#firestatus")[0].classList.contains("bi-fire") == true ) {
						toggleCSSclasses($("#firestatus")[0], "bi-fire", "bi-snow")
						$("#powerheat").val("pcool");
						$("#water-decrement-button").removeAttr('disabled');
						$("#water-increment-button").removeAttr('disabled');
					} else if ($("#firestatus")[0].classList.contains("bi-fire") == false) {
						$("#firestatus")[0].classList.add("bi-snow");
						$("#powerheat").val("pcool");
						$("#water-decrement-button").removeAttr('disabled');
						$("#water-increment-button").removeAttr('disabled');
					}
				}
            	break;
            case "off":
				if (!adds) {
					if ($("#firestatus")[0].classList.contains("bi-snow") == true ) {
						$("#firestatus")[0].classList.remove("bi-snow");
						$("#powerheat").val("off");
						$("#water-decrement-button").attr('disabled','disabled');
						$("#water-increment-button").attr('disabled','disabled');
					}
				}
                break;
            default:
        }
        switch(msg.pdhw) {
        	case "on":
				if (!adds) {
					if ($("#dhwstatus")[0].classList.contains("bi-droplet-fill") == false ) {
						toggleCSSclasses($("#dhwstatus")[0], "bi-droplet-fill", "bi-power")
						$("#powerdhw").val("on");
						$("#dhw-decrement-button").removeAttr('disabled');
						$("#dhw-increment-button").removeAttr('disabled');
					}
				}
                break;
            case "off":
				if (!adds) {
					if ($("#dhwstatus")[0].classList.contains("bi-droplet-fill") == true ) {
						toggleCSSclasses($("#dhwstatus")[0], "bi-droplet-fill", "bi-power")
						$("#powerdhw").val("off");
						$("#dhw-decrement-button").attr('disabled','disabled');
						$("#dhw-increment-button").attr('disabled','disabled');
					} else {
						$("#powerdhw").val("off");
						$("#dhw-decrement-button").attr('disabled','disabled');
						$("#dhw-increment-button").attr('disabled','disabled');
					}
				}
                break;
            default:
        }
	  console.log(msg)
    });
} else {
	console.log("settings");
	console.log(socket);
setTimeout(() => {
  console.log(socket);
}, 3000);
}
});

