import * as mc from "@minecraft/server";
import * as vector from "../utils/vector";
import { toStructureRot } from "../structure/structureManager";

const rotToDir = new Map([
    [0, { x: 0, y: 0, z: -1 }],
    [360, { x: 0, y: 0, z: -1 }],
    [90, { x: 1, y: 0, z: 0 }],
    [-270, { x: 1, y: 0, z: 0 }],
    [180, { x: 0, y: 0, z: 1 }],
    [-180, { x: 0, y: 0, z: 1 }],
    [-90, { x: -1, y: 0, z: 0 }],
    [270, { x: -1, y: 0, z: 0 }]
]);

export function registerComponent(event) {
    event.blockComponentRegistry.registerCustomComponent("hf_mzs:gong", {
        onPlayerInteract(e) {
            const gong = e.block.dimension.getEntities({
                maxDistance: 2,
                families: ["hf_mzs_gong"],
                location: e.block.location
            })[0];
            if (gong !== undefined) {
                if (gong.getProperty("hf_mzs:ringing"))
                    return;
                gong.triggerEvent("ring");
                gong.runCommand("playsound hf_mzs:misc.gong @a[r=16] ~~~")
                mc.system.runTimeout(()=>{
                    if (gong.isValid())
                        gong.setProperty("hf_mzs:ringing", false);
                }, 1)
            }
        },
    });
}
export function onUseOn(event) {
    const { source, block, faceLocation } = event;
    const pos = vector.add(block.location, faceLocation);
    const rot = Math.round(source.getRotation().y / 90) * 90 + 180;
    //mc.world.sendMessage(`${rot}`);
    if (!source.dimension.getBlock(pos).isAir ||
        !source.dimension.getBlock(vector.add(pos, vector.up)).isAir ||
        !source.dimension.getBlock(vector.add(pos, vector.multiply(vector.up, 2))).isAir ||

        !source.dimension.getBlock(vector.add(pos, vector.cross(rotToDir.get(rot), vector.up))).isAir ||
        !source.dimension.getBlock(vector.add(vector.add(pos, vector.up), vector.cross(rotToDir.get(rot), vector.up))).isAir ||
        !source.dimension.getBlock(vector.add(vector.add(pos, vector.multiply(vector.up, 2)), vector.cross(rotToDir.get(rot), vector.up))).isAir ||
        
        !source.dimension.getBlock(vector.add(pos, vector.cross(rotToDir.get(rot), vector.down))).isAir ||
        !source.dimension.getBlock(vector.add(vector.add(pos, vector.up), vector.cross(rotToDir.get(rot), vector.down))).isAir ||
        !source.dimension.getBlock(vector.add(vector.add(pos, vector.multiply(vector.up, 2)), vector.cross(rotToDir.get(rot), vector.down))).isAir
    ) {
        event.cancel = true;
        return;
    }
    mc.system.run(()=>{
        let correctedPos;
        if (rot === 270 || rot === -90 || rot === -270 || rot === -180 || rot === 180)
            correctedPos = vector.add(pos, vector.cross(rotToDir.get(rot), vector.up))
        else
            correctedPos = vector.add(pos, vector.cross(rotToDir.get(rot), vector.down))
        mc.world.structureManager.place('hf_mzs:gong',source. dimension, 
            correctedPos, { rotation: toStructureRot.get(rot) }
        );
        event.source.runCommand(`/playsound random.anvil_land @a[r=16] ${vector.string(correctedPos)} 1 0.5`);
    })
}