var _a;
import { Direction, system, world } from "@minecraft/server";
import { ModalFormData } from "@minecraft/server-ui";
import { getBlockState, getCardinalDirection, setBlockState } from "./my_API";
world.beforeEvents.itemUseOn.subscribe(event => {
    handleShowcaseBlockUse(event);
});
function handleShowcaseBlockUse(event) {
    if (event.block.typeId !== `nps_mot:showcase_block`) {
        return;
    }
    let itemstack = event.itemStack;
    if (itemstack.typeId !== `nps_mot:tablet_pen`) {
        return;
    }
    ShowcaseBlock.useOn(event);
}
class SelectionBoxes {
    constructor(...boxes) {
        this.boxes = boxes;
    }
    getSelected(faceLocation, options) {
        let location = { ...faceLocation };
        if (options?.invertX)
            location.x = 1 - location.x;
        if (options?.invertY)
            location.y = 1 - location.y;
        if (options?.invertZ)
            location.z = 1 - location.z;
        for (let i = 0; i < this.boxes.length; i++) {
            const box = this.boxes[i];
            const { origin, size } = box;
            const absoluteOrigin = {
                x: origin[0] + 8,
                y: origin[1],
                z: origin[2] + 8,
            };
            const inXRange = this.isInRange(location.x, absoluteOrigin.x / 16, (absoluteOrigin.x + size[0]) / 16);
            const inYRange = this.isInRange(location.y, absoluteOrigin.y / 16, (absoluteOrigin.y + size[1]) / 16);
            const inZRange = this.isInRange(location.z, absoluteOrigin.z / 16, (absoluteOrigin.z + size[2]) / 16);
            if (inXRange && inZRange)
                return i;
        }
        return undefined;
    }
    isInRange(value, min, max) {
        return value >= min && value <= max;
    }
}
class ZoneAction {
    constructor(zoneId, selectionBoxes) {
        this.zoneId = zoneId;
        this.selectionBoxes = selectionBoxes;
    }
    rotateZoneAction(angle) {
        return new ZoneAction(this.zoneId, rotateSelectionBoxes(this.selectionBoxes, angle));
    }
}
function rotateSelectionBoxes(selectionBoxes, angle) {
    const newBoxes = selectionBoxes.boxes.map(box => {
        const newOrigin = rotateCubeY(box, angle).origin;
        const newSize = rotateCubeY(box, angle).size;
        return { origin: newOrigin, size: newSize };
    });
    return new SelectionBoxes(...newBoxes);
}
function rotateCubeY(cube, angle) {
    const radians = angle * Math.PI / 180; // Convertir l'angle en radians
    const cosTheta = Math.cos(radians);
    const sinTheta = Math.sin(radians);
    // Appliquer la rotation autour de l'axe Y
    const newOriginX = cosTheta * cube.origin[0] + sinTheta * cube.origin[2];
    const newOriginZ = -sinTheta * cube.origin[0] + cosTheta * cube.origin[2];
    const newSizeX = cosTheta * cube.size[0] + sinTheta * cube.size[2];
    const newSizeZ = -sinTheta * cube.size[0] + cosTheta * cube.size[2];
    return {
        origin: [newOriginX, cube.origin[1], newOriginZ],
        size: [newSizeX, cube.size[1], newSizeZ]
    };
}
class ShowcaseBlock {
    static getScreenNumber(block) {
        const bit0 = getBlockState(block, `nps_mot:bit0`);
        const bit1 = getBlockState(block, `nps_mot:bit1`);
        const screen = 16 * bit1 + bit0;
        return screen;
    }
    static setScreenNumber(block, newScreen) {
        system.run(() => {
            const newBit0 = newScreen % 16;
            const newBit1 = Math.floor(newScreen / 16);
            setBlockState(block, `nps_mot:bit0`, newBit0);
            setBlockState(block, `nps_mot:bit1`, newBit1);
        });
    }
    static useOn(event) {
        const cardinalDirection = getCardinalDirection(event.block).toString().toLocaleLowerCase();
        const buttons = this.faceButtonMapping[cardinalDirection];
        const leftButtonZoneAction = _a.zonesOfAction.find(zone => zone.zoneId === buttons.left);
        const rightButtonZoneAction = _a.zonesOfAction.find(zone => zone.zoneId === buttons.right);
        const touchedZone = _a.getTouchedZoneAction(event, [leftButtonZoneAction, rightButtonZoneAction]);
        switch (touchedZone) {
            case (buttons.left): {
                this.leftButton(event.block);
                system.run(() => {
                    event.source.playSound('nps_mot:button_pressed', { location: event.block.location });
                });
                break;
            }
            case (buttons.right): {
                this.rightButton(event.block);
                system.run(() => {
                    event.source.playSound('nps_mot:button_pressed', { location: event.block.location });
                });
                break;
            }
            default: {
                ShowcaseBlockForm(event);
                break;
            }
        }
    }
    static getTouchedZoneAction(event, zonesOfAction) {
        const faceLocation = event.faceLocation;
        const face = event.blockFace;
        const invertX = face === Direction.West.toLocaleLowerCase();
        const invertZ = face === Direction.South.toLocaleLowerCase();
        for (const zoneAction of zonesOfAction) {
            const selectedBox = zoneAction.selectionBoxes.getSelected(faceLocation, { invertX: invertX, invertZ: invertZ });
            if (selectedBox !== undefined) {
                return zoneAction.zoneId;
            }
        }
        return null;
    }
    static rightButton(block) {
        const screen = _a.getScreenNumber(block);
        let newScreen = screen + 1;
        if (newScreen >= _a.screenNumber) {
            newScreen = 0;
        }
        _a.setScreenNumber(block, newScreen);
    }
    static leftButton(block) {
        const screen = _a.getScreenNumber(block);
        let newScreen = screen - 1;
        if (newScreen < 0) {
            newScreen = _a.screenNumber - 1;
        }
        _a.setScreenNumber(block, newScreen);
    }
}
_a = ShowcaseBlock;
ShowcaseBlock.screenNumber = 30; //0..29
ShowcaseBlock.faceButtonMapping = {
    north: { left: "northLeftButton", right: "northRightButton" },
    south: { left: "southLeftButton", right: "southRightButton" },
    east: { left: "eastLeftButton", right: "eastRightButton" },
    west: { left: "westLeftButton", right: "westRightButton" }
};
ShowcaseBlock.zonesOfAction = [
    new ZoneAction(_a.faceButtonMapping["north"].left, new SelectionBoxes({
        origin: [0.5275, 14.25456, -7.8026],
        size: [2.8, 4.3, 3.3]
    })),
    new ZoneAction(_a.faceButtonMapping["north"].right, new SelectionBoxes({
        origin: [-3.3225, 14.25456, -7.8026],
        size: [2.8, 4.3, 3.3]
    })),
    new ZoneAction(_a.faceButtonMapping["south"].left, new SelectionBoxes({
        origin: [-3.3225, 14.25456, 4.6974],
        size: [2.8, 4.3, 3.3]
    })),
    new ZoneAction(_a.faceButtonMapping["south"].right, new SelectionBoxes({
        origin: [0.5275, 14.25456, 4.6974],
        size: [2.8, 4.3, 3.3]
    })),
    new ZoneAction(_a.faceButtonMapping["west"].left, new SelectionBoxes({
        origin: [-7.8225, 14.25456, -3.3026],
        size: [3.3, 4.3, 2.8]
    })),
    new ZoneAction(_a.faceButtonMapping["west"].right, new SelectionBoxes({
        origin: [-7.8725, 14.25456, 0.4974],
        size: [3.3, 4.3, 2.8]
    })),
    new ZoneAction(_a.faceButtonMapping["east"].left, new SelectionBoxes({
        origin: [4.6275, 14.25456, 0.4974],
        size: [3.3, 4.3, 2.8]
    })),
    new ZoneAction(_a.faceButtonMapping["east"].right, new SelectionBoxes({
        origin: [4.6775, 14.25456, -3.3026],
        size: [3.3, 4.3, 2.8]
    }))
];
function ShowcaseBlockForm(event) {
    system.run(() => {
        const form = new ModalFormData();
        form.title({ translate: 'tile.nps_mot:showcase_block.name' });
        const screen = ShowcaseBlock.getScreenNumber(event.block);
        form.slider('Page', 0, ShowcaseBlock.screenNumber - 1, 1, screen);
        form.show(event.source).then(response => {
            if (response.canceled) {
                return;
            }
            const newScreen = response.formValues[0];
            ShowcaseBlock.setScreenNumber(event.block, newScreen);
        });
    });
}
