import LabelView from "../../../../../view/common/LabelView.js";
import EmptyView from "../../../../../view/elements/EmptyView.js";
import { decompose_finger_print } from "../tile/decompose_finger_print.js";
import { tile_address_to_finger_print } from "../tile/tile_address_to_finger_print.js";

export class UsageDebugView extends EmptyView {
    #tileCount = new LabelView("")
    #tiles = new EmptyView();
    #mip_levels = 0;


    constructor(opt) {
        super(opt);

        this.addChild(this.#tileCount);
        this.addChild(this.#tiles);

        this.css({
            position: "absolute",
            left: "0",
            top: "0",
            background: "rgba(255,255,255,0.5)",
            fontFamily: "monospace",
            fontSize: "10px"
        });
    }

    set mip_levels(v) {
        this.#mip_levels = v;
    }

    /**
     *
     * @param {VirtualTextureUsage} usage
     */
    set usage(usage) {


        this.#tiles.removeAllChildren();

        const occupancy = usage.occupancy;
        const occupancyCount = usage.occupancy_count;

        for (let i = 0; i < occupancyCount; i++) {
            const occupancy_index = occupancy[i];
            const fingerPrint = tile_address_to_finger_print(occupancy_index);

            const count = usage.getCountByFingerprint(fingerPrint);

            if (count <= 0) {
                continue;
            }

            const { mip, x, y } = decompose_finger_print(fingerPrint);

            this.#tiles.addChild(new LabelView(`ID: ${0} Level: ${mip} X: ${x} Y: ${y} USAGE: ${count}`));
        }

        this.#tileCount.updateText(`Tile Count: ${occupancyCount}`);
    }


}