let currentPercent = 0;
let hashInterval;
const progressBarLength = 25; // Length of the progress bar

// Function to generate a random hash
function generateRandomHash(length = 4) {
    const chars = "01234567890";
    let hash = "";
    for (let i = 0; i < length; i++) {
        hash += chars[Math.floor(Math.random() * chars.length)];
    }
    return hash;
}

// Function to generate a random symbol
function generateRandomSymbol() {
    const symbols = "!@#$%^&*()_+-=[]{}|;:,.<>?";
    return symbols[Math.floor(Math.random() * symbols.length)];
}

// Function to update the loading hash with a random symbol effect on "Hacking ..."
function updateLoadingHash() {
    const loadingHash = document.getElementById("loading-hash");
    const originalText = "Hacking ..."; // The word to modify
    let textArray = originalText.split("");

    hashInterval = setInterval(() => {
        // Choose a random index to replace in "Hacking ..."
        const randomIndex = Math.floor(Math.random() * textArray.length);
        const originalChar = textArray[randomIndex]; // Save the original character

        // Replace the character with a random symbol
        textArray[randomIndex] = generateRandomSymbol();
        const modifiedText = textArray.join("");

        // Update the loading hash with the modified "Hacking ..." and random hashes
        loadingHash.textContent = `${modifiedText} X:${generateRandomHash()} , Y:${generateRandomHash()}`;

        // Restore the original character after a short delay
        setTimeout(() => {
            textArray[randomIndex] = originalChar;
            loadingHash.textContent = `${textArray.join("")} X:${generateRandomHash()} , Y:${generateRandomHash()}`;
        }, 50); // Restore after 50ms
    }, 100); // Update every 100ms
}

// Function to stop the loading hash
function stopLoadingHash() {
    clearInterval(hashInterval);
    const loadingHash = document.getElementById("loading-hash");
    loadingHash.textContent = "Hacking complete!";
}

// Reusable function to create an animated progress bar
function createProgressBar(containerId, targetPercent, duration) {
    const progressBar = document.getElementById(containerId);
    const interval = 50; // Update every 50ms
    const steps = duration / interval;
    const increment = (targetPercent - currentPercent) / steps;

    const animation = setInterval(() => {
        if (currentPercent >= targetPercent) {
            clearInterval(animation);
            if (currentPercent >= 100) {
                progressBar.innerHTML = "☠️☠️☠️☠️☠️☠️☠️☠️"; // Skull bar
                progressBar.classList.add("skull-flash"); // Add flashing effect
                stopLoadingHash(); // Stop the hash loading
                const audio = new Audio("./progressbar/finish-him-off.mp3"); // Replace with your sound file
    audio.play();
            }
            return;
        }

        currentPercent += increment;
        if (currentPercent > targetPercent) {
            currentPercent = targetPercent;
        }

        progressBar.textContent = ""; // Clear the progress bar
        for (let n = 0; n < progressBarLength; n++) {
            if (currentPercent < (n + 1) * (100 / progressBarLength)) {
                progressBar.textContent += "░"; // Empty block
            } else {
                progressBar.textContent += "▓"; // Filled block
            }
        }
    }, interval);
}