import { GefestEngine } from "./modules/Gefest/engine";
import { Navbar } from "./pages/navbar";
import { DarkStyle, LightStyle, defaultStyle } from "./styles";
import { ControllerAPI } from "./api/RESTful";
import { NewsFeed } from "./pages/feed/news";

// I'll add a dark mode toggle later, but for now let's just use the light style by default
let currentStyle = defaultStyle;

{ // I hate trash in global scope, so I wrap it in a block
    const appElement = document.getElementById("app");
    if (appElement) {
        appElement.innerHTML = "<p>Loading...</p>";
    }
}

const api : ControllerAPI = new ControllerAPI();

async function authInit() {
    console.log("User is authenticated");
    const newsFeed = await api.getNews(true) as NewsFeed;
    const appElement = document.getElementById("app");
    if (appElement)
        appElement.innerHTML = newsFeed.build();
}

async function logInInit () {
    console.log("User is not authenticated");
    // Load the auth page
}

async function main() {
    // init navbar
    const navbar = new Navbar();
    navbar.style = currentStyle;
    document.body.innerHTML = "<div id='app'></div>" + navbar.build();

    if (await api.isAuthenticated()) {
        await authInit();
    } else {
        await logInInit();
    }
}

GefestEngine.main(main).catch(error => {
    console.error("Error during initialization:", error);
});