const btcPopup = new Popup({
	id: "btc-popup",
	title: `Support via Bitcoin`,
	content: `Make sure you read the <a href="https://blog.knugi.com/DONATE.html" target="_blank">Donation Guide</a> before making a donation!
	<img src="/images/btc_address.png">
	Bitcoin address:
	<a data-bs-toggle="tooltip" title="Copied!" class="copyAddress">bc1qm2cfq63xk69dg5apkfn7xttfe0d87d0wke33sv</a>`,
	showImmediately: false,
	underlineLinks: true,
});

const xmrPopup = new Popup({
	id: "xmr-popup",
	title: "Support via Monero",
	content: `Make sure you read the <a href="https://blog.knugi.com/DONATE.html" target="_blank">Donation Guide</a> before making a donation!
	<img src="/images/xmr_address.png">
	Monero address:
	<a data-bs-toggle="tooltip" title="Copied!" class="copyAddress">86SQDTgjRf3YZWbBTvVx1eD2GRdVn4S2zSdMH4aWGFxyVGkLK2aZXH2BnqPXpckm1zCuatu7uv3A5MTUjsEcGXTMVo2tku1</a>`,
	showImmediately: false,
	underlineLinks: true,
	loadCallback: () => {
		document.querySelectorAll("a.copyAddress").forEach(elem => {
			elem.addEventListener("click", event => {
				copyToClipboard(event)
			})
		})
	}
});

function copyToClipboard(event) {
	navigator.clipboard.writeText(event.target.innerText).then(function() {
		const tooltipTrigger = new bootstrap.Tooltip(event.target, {
			trigger: 'manual',
			container: event.target.parentElement.parentElement
		});
		tooltipTrigger.show();
		setTimeout(function () {
			tooltipTrigger.hide();
		}, 2000);
	}).catch(function(err) {
		console.error('Failed to copy text: ', err);
	});
}

function enableSubmit(){
	document.getElementById("submit-contact").disabled = false;
	tooltipTrigger1.disable();
}

let tooltipTrigger1;

$(document).ready(function() {
	tooltipTrigger1 = new bootstrap.Tooltip(document.getElementById("submit-contact"));
	document.querySelectorAll(".services-thumb").forEach(elem => {
		let repo = elem.querySelector("h3").innerText.replaceAll(" ", "-");
		fetch(`https://api.github.com/repos/knugihk/${repo}`)
		  .then(response => response.json())
		  .then(data => {
			const stars = data.stargazers_count;
			if (stars !== undefined){
				elem.querySelector("p.services-price-text").innerText = `★${stars}`;
			}
			
		  })
		  .catch(error => {
			console.error('Error fetching data:', error);
		  });
	})
	
	const lightbox = new PhotoSwipeLightbox({
		gallery: '#ai-gallery',
		children: 'a',
		pswpModule: () => import('/js/photoswipe.esm.js')
	});
	lightbox.init();

	$("nav").sticky({topSpacing:0});
	
	const form_status = new URL(window.location).searchParams.get("form");
	if (form_status === "submitted"){
		const myPopup = new Popup({
			id: "my-popup",
			title: "Form Submitted",
			content: `We may contact you soon.`,
			showImmediately: true
		});
	}else if (form_status === "rejected"){
		const myPopup = new Popup({
			id: "my-popup",
			title: "Form Rejected",
			content: `We are not accepting new form for now. Check back later.`,
			showImmediately: true
		});
	}

	document.getElementById("bitcoin").addEventListener("click", event => {
		btcPopup.show();
	})

	document.getElementById("monero").addEventListener("click", event => {
		xmrPopup.show();
	})
});