function saveSettings(e) {
	chrome.storage.sync.set({
		"apiKey": apiKey.value,
		"isEnabled": isEnabled.checked,
		"Auto": Auto.checked,
		"AutoCallback": AutoCallback.checked,
		"isAutoClickXpathSubmit": isAutoClickXpathSubmit.checked,
		"xpathSubmit": xpathSubmit.value
	});
}

function restoreSettings(e) {
	chrome.storage.sync.get('apiKey',function(res)
	{
		apiKey.value = res.apiKey || "";
		if(apiKey.value!="")
		{
			let url = `https://captcha69.com/check-thread/${apiKey.value}?json=1`;
			console.log("url balance: " + url);
			chrome.runtime.sendMessage({
	        method: 'GET',
	        action: 'xhttp',
	        url: url,
	        data: ''
	    }, function(responseText) {
			try{
				console.log("balance: " + responseText);
				if(responseText.includes("Points")){
					const jsonObject = JSON.parse(responseText);
					document.getElementById('pointCurrent').innerText = 'Point: '+jsonObject.Points;
				}
			}catch(err) {
				console.log(err);
			}
	    });
			
		}
	});

	chrome.storage.sync.get('isEnabled',function(res)
	{	
		isEnabled.checked = res.isEnabled;
	});

	chrome.storage.sync.get('Auto',function(res)
	{
		Auto.checked = res.Auto;
	});

	chrome.storage.sync.get('AutoCallback',function(res)
	{
		AutoCallback.checked = res.AutoCallback;
	});

	chrome.storage.sync.get('isAutoClickXpathSubmit',function(res)
	{
		isAutoClickXpathSubmit.checked = res.isAutoClickXpathSubmit;
	});

	chrome.storage.sync.get('xpathSubmit',function(res)
	{
		xpathSubmit.value = res.xpathSubmit || "";
	});

	apiKey.addEventListener('change',saveSettings);
	isEnabled.addEventListener('change',saveSettings);
	Auto.addEventListener('change',saveSettings);
	AutoCallback.addEventListener('change',saveSettings);
	isAutoClickXpathSubmit.addEventListener('change',saveSettings);
	xpathSubmit.addEventListener('change',saveSettings);
}

document.addEventListener('DOMContentLoaded', restoreSettings);


