import {
    Application,
    Assets,
    Container,
    Sprite,
    Rectangle,
    Text,
} from "pixi.js";
import { TimedGame } from "./timedGame";
import { KeyBoardListener, makeDragable, getIntersection } from "../common";

const itemTypes = { TRASH: "TRASH", HARDWARE: "HARDWARE" };

export class CleanupHS extends TimedGame {
    constructor(width, height, ticker) {
        super(10000, ticker);

        this._elementCount = this._difficulty * 6;
        this._elementsPlaced = 0;
        this._items = [];
        this._scree_w = width;
        this._scree_h = height;

        this.timeout = this._elementCount * 1000; // 1s per element

        this.gameContainer.layout = {
            width: "100%",
            height: "100%",
            justifyContent: "space-between",
        };
        const trashcan = Sprite.from("trashcan");
        const shelf = Sprite.from("shelf");
        this.trashLandingZone = new Container();
        this.shelfLandingZone = new Container();
        this.trashLandingZone.addChild(trashcan);
        this.shelfLandingZone.addChild(shelf);

        this._gameDesc.text = "Range le HS !!";
        this._instruction.text = "drag & drop les objets dans le bon contenant";

        this.trashLandingZone.layout = { width: "20%", height: "100%" };
        this.shelfLandingZone.layout = {
            width: "20%",
            height: "100%",
        };
        trashcan.layout = { objectFit: "contain" };
        shelf.layout = { objectFit: "contain" };

        this._addGameObjectChild(this.trashLandingZone);
        this._addGameObjectChild(this.shelfLandingZone);
    }

    start() {
        super.start();
    }

    reset() {
        super.reset();
        this._elementsPlaced = 0;

        this._items = [];

        for (let i = 0; i < this._elementCount; i++) {
            const item = {};
            if (Math.random() > 0.5) {
                item.type = itemTypes.TRASH;
                item.sprite = Sprite.from("trash");
            } else {
                item.type = itemTypes.HARDWARE;
                item.sprite = Sprite.from("arduino");
            }

            this._items.push(item);

            // non dynamic sizes for now
            const w = this._scree_w * 0.04;
            item.sprite.width = w;
            item.sprite.height = w;
            makeDragable(item.sprite, (node) => {
                this._onItemDrop(item);
            });
            this._addGameObjectChild(item.sprite);
        }
    }

    _onItemDrop(item) {
        console.log("dropped", item.type);

        const intersection = new Rectangle();
        const overlapGreaterThan = (threshold, rect1, rect2) => {
            getIntersection(rect1, rect2, intersection);
            const intersectArea = intersection.width * intersection.height;
            const rect1Area = rect1.width * rect1.height;
            return intersectArea > threshold * rect1Area;
        };

        if (
            overlapGreaterThan(
                0.6,
                item.sprite.getBounds(),
                this.trashLandingZone.getBounds(),
            )
        ) {
            if (item.type != itemTypes.TRASH) {
                this._onError();
            }
            this._elementsPlaced++;
            this.gameContainer.removeChild(item.sprite);
        } else if (
            overlapGreaterThan(
                0.6,
                item.sprite.getBounds(),
                this.shelfLandingZone.getBounds(),
            )
        ) {
            if (item.type != itemTypes.HARDWARE) {
                this._onError();
            }
            this._elementsPlaced++;
            this.gameContainer.removeChild(item.sprite);
        }

        if (this._elementsPlaced >= this._elementCount) {
            this.end(true);
        }
    }

    _onError() {
        console.log("wrong placement");
        // apply penalty for putting item in the wrong place
        this.removeTime(5000);
    }

    _timerConsoleDisplay() {
        console.log("time left", this.getRemainingTime());
    }
}
