/**
 * Nesvra Blog Reading Progress & Social Sharing Handlers
 */
document.addEventListener('DOMContentLoaded', function () {
    if (typeof AOS !== 'undefined') {
        AOS.init({ once: true, duration: 600, offset: 60 });
    }

    // Scroll reading progress meter
    window.addEventListener('scroll', function () {
        const totalHeight = document.body.scrollHeight - window.innerHeight;
        if (totalHeight <= 0) return;
        const progress = Math.min(100, Math.max(0, Math.round((window.pageYOffset / totalHeight) * 100)));

        const circle = document.getElementById('progress-circle');
        if (circle) {
            const radius = 24;
            const circumference = 2 * Math.PI * radius;
            circle.style.strokeDashoffset = circumference - (progress / 100) * circumference;
        }

        const text = document.getElementById('progress-text');
        if (text) {
            text.innerText = progress + '%';
        }
    });
});

/**
 * Share to Twitter / X
 */
function shareOnTwitter() {
    const url = encodeURIComponent(window.location.href);
    const title = encodeURIComponent(document.title || 'Check out this article on Nesvra');
    window.open(`https://twitter.com/intent/tweet?url=${url}&text=${title}`, '_blank', 'width=600,height=450,resizable=yes');
}

/**
 * Share to LinkedIn
 */
function shareOnLinkedIn() {
    const url = encodeURIComponent(window.location.href);
    window.open(`https://www.linkedin.com/sharing/share-offsite/?url=${url}`, '_blank', 'width=600,height=550,resizable=yes');
}

/**
 * Copy article link with toast notification
 */
function copyArticleLink() {
    const url = window.location.href;
    if (navigator.clipboard && window.isSecureContext) {
        navigator.clipboard.writeText(url).then(showCopyToast).catch(() => fallbackCopy(url));
    } else {
        fallbackCopy(url);
    }
}

function fallbackCopy(text) {
    const input = document.createElement('input');
    input.value = text;
    input.style.position = 'fixed';
    input.style.opacity = '0';
    document.body.appendChild(input);
    input.select();
    try {
        document.execCommand('copy');
        showCopyToast();
    } catch (err) {
        prompt('Copy this link:', text);
    }
    document.body.removeChild(input);
}

function showCopyToast() {
    let toast = document.getElementById('nesvra-toast');
    if (!toast) {
        toast = document.createElement('div');
        toast.id = 'nesvra-toast';
        toast.className = 'fixed bottom-8 left-1/2 -translate-x-1/2 z-[200] bg-dark text-white px-6 py-3 rounded-full text-xs font-mono tracking-wider shadow-2xl flex items-center gap-3 border border-white/20 transition-all duration-300 transform opacity-0 translate-y-4';
        toast.innerHTML = '<span class="w-2 h-2 rounded-full bg-brandOrange inline-block animate-pulse"></span> Link copied to clipboard!';
        document.body.appendChild(toast);
    }

    setTimeout(() => {
        toast.classList.remove('opacity-0', 'translate-y-4');
        toast.classList.add('opacity-100', 'translate-y-0');
    }, 10);

    setTimeout(() => {
        toast.classList.remove('opacity-100', 'translate-y-0');
        toast.classList.add('opacity-0', 'translate-y-4');
    }, 3000);
}