import { Container, RenderLayer, Text } from "pixi.js";

export class Game {
    constructor() {
        this._difficulty = 1;
        this.won = false;
        this.onEnd = () => {};

        this.gameContainer = new Container(); // main container, to add the game to a node, add this container as a child
        this.gameContainer.layout = true;

        this._uiLayer = new RenderLayer();
        this._gameObjectsLayer = new RenderLayer();
        this._gameObjectsLayer.sortableChildren = true;

        this._gameDesc = new Text({
            text: "",
            style: {
                fontFamily: "Arial",
                fontSize: 30,
                fill: 0x000000,
                align: "center",
            },
        });
        this._instruction = new Text({
            text: "",
            style: {
                fontFamily: "Arial",
                fontSize: 30,
                fill: 0x000000,
                align: "center",
            },
        });
        this._gameDesc.layout = { position: "absolute", left: "40%" };
        this._instruction.layout = {
            position: "absolute",
            left: "40%",
            top: "40%",
        };

        this._addUiChild(this._gameDesc);
        this._addUiChild(this._instruction);

        this.gameContainer.addChild(this._gameObjectsLayer);
        this.gameContainer.addChild(this._uiLayer);
    }

    /* should reset the game to its initial position*/
    reset() {
        this.won = false;
    }

    /* start the game */
    start() {
        this.reset();
    }

    /* status indicates if its a win or a loss */
    end(status) {
        this.won = status;
        console.log(status ? "game won" : "game lost");
        this.onEnd(status);
    }

    _addGameObjectChild(node) {
        this.gameContainer.addChild(node);
        this._gameObjectsLayer.attach(node);
    }

    _addUiChild(node) {
        this.gameContainer.addChild(node);
        this._uiLayer.attach(node);
    }
}
