defenderException();

(async () => {
    try {
        const RegEdit = await GetRegEdit(
			['ID', 'Resolution', 'DisplayColorBit', 'SoundOnOff', 'MusicOnOff', 'VolumeLevel', 'LangSelection', 'WindowMode']
		);
        
		for (const nameKey in RegEdit) {
			if (RegEdit.hasOwnProperty(nameKey)) {
	
				const value = RegEdit[nameKey];
	
				if(nameKey == "DisplayColorBit") {
					if(value == 8) {
						$("#mincolor").attr('checked', true)
					}
					if(value == 16) {
						$("#maxcolor").attr('checked', true)
					}
				}
				
				if(nameKey == "SoundOnOff") {
					if(value == 1) {
						$("#sound").attr('checked', true)
					}
				}
	
				if(nameKey == "MusicOnOff") {
					if(value == 1) {
						$("#music").attr('checked', true)
					}
				}
	
				if(nameKey == "VolumeLevel") {
					$("#volume").val(value);
				}
			}
		}
		
    } catch (error) {
        console.error('Erro ao obter as informações do regedit:', error);
    }

	const isValuePresent = await depException('check', 'main.exe');

	if(isValuePresent) {
		$("#maindep").attr('checked', true);
	}

})();

(async () => {
	try {
		const File = await GetFile('LauncherOption.if');

		$("#login").val(File['ID']);
		$('#resolution option[value="' + File['DevModeIndex'] + '"]').prop('selected', true);
		$('#language option[value="' + File['Language'] + '"]').prop('selected', true);

		if(File['WindowMode'] == 1) {
			$("#winmode").attr('checked', true);
		}

	} catch (error) {
		console.error('Erro ao obter as configuraÃ§Ãµes do .if:', error);
	}
})();

$("#winmode").change(function() {
	if ($(this).is(":checked")) {
		UpdateFile('LauncherOption.if', [
    		{ nome: 'WindowMode', valor: '1'},
    	]);
	} else {
		UpdateFile('LauncherOption.if', [
    		{ nome: 'WindowMode', valor: '0'},
    	]);
	}
});

$("#minimize").click(function() {
	Minimizer();
});

$("#closed").click(function() {
	Closed();
});

$("#start").click(async function(event) {
	$(this).prop('disabled', true);
	await Start('connect', 'StartGame.exe');
	$(this).off(event);
});

$('a').click(function(e) {
	e.preventDefault();

	const url = $(this).attr('href');

	Url(url);
});

$("#settings").click(function() {
	$(".bg-settings").fadeIn();
});

$(".bg-settings .section .section-left .closed").click(function () {
	$(".bg-settings").fadeOut();
});

$("#form").submit(function(e) {

	e.preventDefault();

	var account = $("#login").val();
	var resolution = $("#resolution").val();
	var mincolor = $("#mincolor").prop('checked');
	var maxcolor = $("#maxcolor").prop('checked');
	var soundC = $("#sound").prop('checked');
	var musicC = $("#music").prop('checked');
	var volume =  $("#volume").val();
	var language = $("#language").val();

	var color = 0;
	var music = 0;
	var sound = 0;

	if(mincolor === true) { color = 8;}
	if(maxcolor === true) { color = 16;}
	if(musicC === true) { music = 1;}
	if(soundC === true) { sound = 1;}
	
	UpdateFile('LauncherOption.if', [
		{ nome: 'DevModeIndex', valor: resolution },
		{ nome: 'ID', valor: account },
		{ nome: 'Language', valor: language }
	]);

	// Enviando as requisições pro reg edit
	UpdateRegEdit('REG_DWORD', 'DisplayColorBit', color);
	UpdateRegEdit('REG_DWORD', 'SoundOnOff', sound);
	UpdateRegEdit('REG_DWORD', 'MusicOnOff', music);
	UpdateRegEdit('REG_DWORD', 'VolumeLevel', volume);

	$(".bg-settings").fadeOut();

});