const values = [
    "Web developer", 
    "Pc games enjoyer", 
    "Server backend survivor", 
    "python programmer", 
    "Javascript disliker",
    "I use Arch btw",
    "Owo, what's this?",
    "Ultimate procrastinator",
    "Ultrakill gaming",
    "00111010 00110011",
    "fistful of dollar",
    "Thy end is now!!!",
    "Possibly a furry",
    "Prepare thyself!!!",
    "Spegatti code master",
    "Ethernet cable untangler",
    "Caffeine addict",
    "I'm not a robot ☑",
    "Loud Music enjoyer",
    "part time femboy :<",
];
let typing_direction = 1;
let text = "";
let speed = 100; 
let selectedValue = 0;
let currentValueIndex = 0;
let pause = false;

function randomValue() {
    selectedValue = Math.floor(Math.random() * values.length);
    currentValueIndex = 0;
}

function type() {
    if (typing_direction == 1) {
        if (currentValueIndex < values[selectedValue].length) {
            text += values[selectedValue][currentValueIndex];
            currentValueIndex++;
        } else {
            typing_direction = -1;
            pause = true;
        }
    } else {
        if (currentValueIndex > 0) {
            text = text.slice(0, -1);
            currentValueIndex--;
        } else {
            typing_direction = 1;
            randomValue();
        }
    }
}

function typing() {
    type();
    document.getElementById("typing").innerHTML = "$ " + text;
    if (typing_direction == 1) {
        speed = 80 + Math.random() * 100;
    } else {
        speed = 60 + (Math.random() * 100) / 2;
    }
    if (!pause) {
        setTimeout(typing, speed);
    } else {
        setTimeout(typing, 500);
        pause = false;
    }
}

typing();

// HIDDEN STUFF (shh don't tell anyone >:3)

let last5Chars = "";

document.addEventListener('keydown', function(event) {
    last5Chars += event.key;
    if (last5Chars == "furry") {
        console.log("owo, whats this?");
        document.getElementById('furry').style.display = 'block';
    }
    if (last5Chars == "irken") {
        console.log("doom doom doom!");
        document.querySelector(":root").style.setProperty('--font-family', 'Irken');
        document.querySelector(":root").style.setProperty('--title-font', '1.5em');
    }
    while (last5Chars.length >= 5) {
        last5Chars = last5Chars.slice(1);
    }
});

// Spotify API

function getSpotify() {
    fetch('https://api.alfieking.dev/spotify/nowplaying/xz02oolstlvwxqu1pfcua9exz').then(response => {
        return response.json();
    }).then(data => {
        if (data.item == null) {
            document.getElementById('spotify').style.backgroundImage = "none";
            document.getElementById('spotify-title').innerHTML = "Spotify is not playing anything";
            document.getElementById('spotify-artist').innerHTML = ":(";
            document.getElementById('spotify-link').href = "https://open.spotify.com/";
            return;
        }
        document.getElementById('spotify').style.backgroundImage = "url(" + data.item.album.images[0].url + ")";
        document.getElementById('spotify-title').innerHTML = data.item.name;
        document.getElementById('spotify-artist').innerHTML = data.item.artists[0].name;
        document.getElementById('spotify-link').href = data.item.external_urls.spotify;
    });
}

if (document.getElementById('spotify')) {
    getSpotify();
    setInterval(getSpotify, 15000);
}

// load buttons

function loadButtons() {
    fetch('/static/content/other/buttons.txt').then(response => {
        return response.text();
    }).then(data => {
        container = document.getElementById('button-collection');
        for (let line of data.split('\n')) {
            if (line == "") {
                continue;
            }
            let img = document.createElement('img');
            img.src = line;
            container.appendChild(img);
        }
    });
}

if (document.getElementById('button-collection')) {
    loadButtons();
}