/**
 *
 * @licstart  The following is the entire license notice for the 
 *  JavaScript code in this page.
 *
 * Copyright (C) 2024 Gabriel Weingardt
 *
 *
 * The JavaScript code in this page is free software: you can
 * redistribute it and/or modify it under the terms of the GNU
 * General Public License (GNU GPL) as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option)
 * any later version.  The code is distributed WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU GPL for more details.
 *
 * As additional permission under GNU GPL version 3 section 7, you
 * may distribute non-source (e.g., minimized or compacted) forms of
 * that code without the copy of the GNU GPL normally required by
 * section 4, provided you include this license notice and a URL
 * through which recipients can access the Corresponding Source.
 *
 * @licend  The above is the entire license notice
 * for the JavaScript code in this page.
 *
 */

// Link or Path, Display Name, Target
navContent = [  
    ["P", "Home", ""],
    ["P", "Contact", "contact/"],
    ["P", "Projects", "projects/"],
    ["P", "Posts", "posts/"],
    ["SEP"],
    ["L", "Git", "https://git.weingardt.dev/explore/repos"],
    ["SEP"],
    ["P", "Website Info", "info/"]
];

// Alt, Href, Src
funButtons = [
    ["CC BY-SA 4.0", "https://creativecommons.org/licenses/by-sa/4.0/", "global/cc-by-sa.png"],
    ["Right to repair", "", "global/right2repair.gif"],
    ["Made with VSCodium", "https://vscodium.com/", "global/vscodium.png"],
    ["Made on Gnu/Linux", "https://www.gnu.org/home.en.html", "global/gnu-linux.gif"],
    ["Windows 10, NO", "", "global/win10no.gif"],
    //["No NFT's", "", "global/nftbutton.gif"],
    ["Anything but Chrome", "https://librewolf.net/", "global/nochrome.png"],
    ["UBlock Origin Now!", "https://ublockorigin.com/", "global/ublock.png"],
    //["Get a Website - Landchad", "https://landchad.net/", "global/landchad.gif"]
];

var styles = ["Tomorrow", "Forgotten", "Yotsuba"];


function updateStyle(type){
    var doc = document.documentElement;

    if (type == undefined){ type = "Tomorrow"; }

    if (type == "Yotsuba"){
        doc.style.setProperty("--main-bg-color",        "#fed6af");
        doc.style.setProperty("--main-bg-end-color",    "#ffffee");
        doc.style.setProperty("--main-color",           "#ffffff");
        doc.style.setProperty("--main-fg-color",        "#800000");
        doc.style.setProperty("--main-title-fg-color",  "#800000");

        doc.style.setProperty("--font-family", "Arial, Helvetica, sans-serif");
        doc.style.setProperty("--font-size", "13px");

        document.querySelector("body").style.backgroundImage = "url(" + dirDepth + "global/fade.png)";
    } else if (type == "Tomorrow"){
        doc.style.setProperty("--main-bg-color",        "#1d1f21");
        doc.style.setProperty("--main-bg-end-color",    "#1d1f21");
        doc.style.setProperty("--main-color",           "#282a2e");
        doc.style.setProperty("--main-fg-color",        "#ffffff");
        doc.style.setProperty("--main-title-fg-color",  "#ffffff");

        doc.style.setProperty("--font-family", "Arial, Helvetica, sans-serif");
        doc.style.setProperty("--font-size", "13px");

        document.querySelector("body").style.backgroundImage = "none";
    } else if (type == "Forgotten"){
        doc.style.setProperty("--main-bg-color",        "#282828");
        doc.style.setProperty("--main-bg-end-color",    "#458588");
        doc.style.setProperty("--main-color",           "#a89984");
        doc.style.setProperty("--main-fg-color",        "#282828");
        doc.style.setProperty("--main-title-fg-color",  "#7c7c7c");

        doc.style.setProperty("--font-family", "FreePixel");
        doc.style.setProperty("--font-size", "15px");

        document.querySelector("body").style.backgroundImage = "none";
    }
}


let dirDepth = document.getElementById("toplevel").getAttribute("data-");
document.getElementById("toplevel").textContent = "";

const WEBSIDENAME = document.URL;

const LICENCE_LINK = "https://creativecommons.org/licenses/by-sa/4.0/";
const LICENCE_NAME = "CC BY-SA 4.0";
const CODE_LICENCE_LINK = "https://www.gnu.org/licenses/gpl-3.0.en.html"
const CODE_LICENCE_NAME = "GPL v.3"


var footing = document.getElementById("footnote"); 


/* Footer Section */

/* Image Buttons */

var title = document.createElement("h2");
title.setAttribute("class", "titlebar");
title.textContent = "Page Info";

footing.appendChild(title);

if (document.getElementById("please-no-buttons") == null){
    var button_container = document.createElement("div");
    button_container.setAttribute("class", "button-container");

    for (let i = 0; i < funButtons.length; i++) {
        link = document.createElement("a");
        if (funButtons[Number(i)][1] != ""){
            link.setAttribute("href", funButtons[Number(i)][1]);
        }
        link.setAttribute("target", "_blank");    

        img = document.createElement("img");
        img.setAttribute("src", dirDepth + funButtons[Number(i)][2]);
        img.setAttribute("alt", funButtons[Number(i)][0]);
        img.setAttribute("class", "license-img");
        link.appendChild(img);

        button_container.appendChild(link);
    }
    footing.appendChild(button_container);
}

// Last updated thingie

var docDate = ((("" + document.lastModified).replaceAll(",", " at ")).replaceAll("/", ".")).split(".");
docSave = docDate[0];
docDate[0] = docDate[1];
docDate[1] = docSave;
docDate = "Page last updated: " + ((docDate.toString().replaceAll(",", ".")).replaceAll(" ", " at "));

var last_updated = document.createElement("p");
last_updated.setAttribute("class", "text-center");
last_updated.textContent = docDate;

footing.appendChild(last_updated);

// Footer

var footer = document.getElementById("footer");

var span = document.createElement("p");
span.setAttribute("class", "nobr");
span.setAttribute("class", "text-center");
span.textContent = "Website licensed under\u00A0";

var span_license = document.createElement("a");
span_license.setAttribute("href", LICENCE_LINK);
span_license.setAttribute("class", "nobr");
span.setAttribute("class", "text-center");
span_license.textContent = LICENCE_NAME;
span.appendChild(span_license);
/*
var span_gpl_pre = document.createElement("p");
span_gpl_pre.setAttribute("class", "nobr");
span_gpl_pre.textContent = "\u00A0| Code licensed under\u00A0";
span.appendChild(span_gpl_pre);

var span_gpl = document.createElement("a");
span_gpl.setAttribute("href", CODE_LICENCE_LINK);
span_gpl.setAttribute("class", "nobr");
span_gpl.textContent = CODE_LICENCE_NAME;
span.appendChild(span_gpl);
*/
footing.appendChild(span);

// Webside name

var webside_name = document.createElement("a");
webside_name.setAttribute("class", "text-center");
webside_name.setAttribute("href", WEBSIDENAME);
webside_name.textContent = WEBSIDENAME;

footing.appendChild(webside_name);

var button_span = document.createElement("span");
button_span.setAttribute("class", "prefSpan")
button_span.textContent = "Style: "

var button_select = document.createElement("select");
button_select.id = "styleselector";

for (let i = 0; i < styles.length; i++) {
    var button_item = document.createElement("option");
    button_item.textContent = styles[Number(i)];
    button_item.value = styles[Number(i)];
    if (styles[Number(i)] === localStorage.getItem("style")) { button_item.selected = "selected"; }

    button_select.appendChild(button_item);
}

button_span.appendChild(button_select);
footing.appendChild(button_span);

if(dirDepth === ""){
    document.getElementById("agestamp").textContent = new Date(new Date().getTime() - new Date("March, 30, 2006").getTime()).getFullYear() - 1970;
}



/* Navbar Section */

if (document.getElementById("navbar") != null){

    var selectedNav = document.getElementById("selected-nav").getAttribute("data-");

    var nav = document.getElementById("navbar");

    for (let i = 0; i < navContent.length; i++) {
        if (navContent[Number(i)][0] === "SEP"){
            tmpAdd = document.createElement("hr");
            tmpAdd.classList.add("navSep");
            document.getElementById("navbar").appendChild(tmpAdd);

        } else {
            href = navContent[Number(i)][2];
            sel = navContent[Number(i)][1].toLowerCase().includes(selectedNav.toLowerCase());

            if (navContent[Number(i)][0] === "P"){
                href = dirDepth + href;
            }

            tmpAdd = document.createElement("a");
            tmpAdd.textContent = navContent[Number(i)][1];
            tmpAdd.setAttribute("href", href);
            tmpAdd.classList.add("nav");

            if (sel) {
                tmpAdd.classList.add("selected-nav");
            }


            document.getElementById("navbar").appendChild(tmpAdd);
        }
    } 
}

var stylebutton = document.getElementById("styleselector");
stylebutton.addEventListener("change", (event) => {
    updateStyle(event.target.value);
    localStorage.setItem("style", event.target.value);
});

updateStyle(localStorage.getItem("style"));