import "@pixi/layout";
import { Application, Assets, Container, Sprite } from "pixi.js";
import { Fridge, Opinator, SmartMonday, CleanupHS } from "./games";
import { backgroundLayout, KeyBoardListener } from "./common";
import { GameManager } from "./gameManager";

const app = new Application();
let root = new Container();

async function setup() {
    await app.init({ background: "#1099bb", resizeTo: window });

    root = app.stage;
    const setRootLayout = () => {
        root.layout = {
            width: app.screen.width,
            height: app.screen.height,
            maxWidth: app.screen.width,
            maxHeight: app.screen.height,
        };
    };
    setRootLayout();
    app.renderer.on("resize", () => {
        setRootLayout();
    });
    document.body.appendChild(app.canvas);
}

async function preload() {
    const assets = [
        {
            alias: "background",
            src: "assets/img/background1.jpg",
        },
        {
            alias: "opinator",
            src: "assets/img/opinator.avif",
        },
        {
            alias: "frigo",
            src: "assets/img/frigo.jpg",
        },
        {
            alias: "clubmate",
            src: "assets/img/clubmate.png",
        },
        {
            alias: "clubmate_grey",
            src: "assets/img/clubmate_grey.png",
        },
        {
            alias: "mate_caisse",
            src: "assets/img/caisse.jpg",
        },
        {
            alias: "trash",
            src: "assets/img/empty_monster.jpg",
        },
        {
            alias: "trashcan",
            src: "assets/img/trashcan.png",
        },
        {
            alias: "arduino",
            src: "assets/img/arduino.jpg",
        },
        {
            alias: "shelf",
            src: "assets/img/shelf.avif",
        },
    ];
    await Assets.load(assets);

    KeyBoardListener.init();
}

function switchToGame(gameContainer, newGame) {
    //empty previous container
    //create new game and add
}

(async () => {
    await setup();
    await preload();

    const background = Sprite.from("background");
    background.layout = backgroundLayout;

    root.addChild(background);

    const gameContainter = new Container();
    gameContainter.layout = {
        position: "absolute",
        width: "100%",
        height: "100%",
        maxWidth: "100%",
        maxHeight: "100%",
        margin: 0,
    };
    root.addChild(gameContainter);

    const manager = new GameManager(app, gameContainter);
    manager.start();

    const opinator = new Opinator(app.ticker);
    const fridge = new Fridge(app.ticker);
    const smartMonday = new SmartMonday(app.ticker);
    const cleanupHS = new CleanupHS(root.width, root.height, app.ticker);
    let currentGame = cleanupHS;
    //gameContainter.addChild(currentGame.gameContainer);

    //currentGame.start();
})();
