import { world, system } from "@minecraft/server";

function getblockFace(block,blockFace) {
    switch (blockFace) {
        case "Up":
            return block.above();
        case "Down":
            return block.below()
        case "South":
            return block.south()
        case "North":
            return block.north()
        case "East":
            return block.east()
        case "West":
            return block.west()
    }
}

world.beforeEvents.playerInteractWithBlock.subscribe(async({player, isFirstEvent, itemStack, block, blockFace})=>{
    if (itemStack?.typeId !== "just:hat_display") return;
    await system.waitTicks(1);
    const entities = player.dimension.getEntities({
        location: getblockFace(block,blockFace).center(),
        closest: 1,
        type: "just:hat_display",
        excludeTags: ["rotated"]
    });
    if (!entities.length) return;
    const rot = player.getRotation().y + 180;
    let yRot = 0;
    if (rot >= 22.5 && rot < 67.5) yRot = 45
    else if (rot >= 67.5 && rot < 112.5) yRot = 90
    else if (rot >= 112.5 && rot < 157.5) yRot = 135
    else if (rot >= 157.5 && rot < 202.5) yRot = 180
    else if (rot >= 202.5 && rot < 247.5) yRot = 225
    else if (rot >= 247.5 && rot < 292.5) yRot = 270
    else if (rot >= 292.5 && rot < 337.5) yRot = 315
    else if (rot >= 292.5 && rot < 337.5) yRot = 315
    
    entities.forEach(entity=>{
        entity.setRotation({x:0,y:yRot});
        entity.addTag("rotated")
    })
})


world.beforeEvents.playerInteractWithBlock.subscribe(async({player, isFirstEvent, itemStack, block, blockFace})=>{
    if (itemStack?.typeId !== "just:rubber_ducke") return;
    await system.waitTicks(1);
    const entities = player.dimension.getEntities({
        location: getblockFace(block,blockFace).center(),
        closest: 1,
        type: "just:rubber_ducke",
        excludeTags: ["rotated"]
    });
    if (!entities.length) return;
    const rot = player.getRotation().y + 180;
    let yRot = 0;
    if (rot >= 22.5 && rot < 67.5) yRot = 45
    else if (rot >= 67.5 && rot < 112.5) yRot = 90
    else if (rot >= 112.5 && rot < 157.5) yRot = 135
    else if (rot >= 157.5 && rot < 202.5) yRot = 180
    else if (rot >= 202.5 && rot < 247.5) yRot = 225
    else if (rot >= 247.5 && rot < 292.5) yRot = 270
    else if (rot >= 292.5 && rot < 337.5) yRot = 315
    else if (rot >= 292.5 && rot < 337.5) yRot = 315
    
    entities.forEach(entity=>{
        entity.setRotation({x:0,y:yRot});
        entity.addTag("rotated")
    })
})


world.beforeEvents.playerInteractWithBlock.subscribe(async({player, isFirstEvent, itemStack, block, blockFace})=>{
    if (itemStack?.typeId !== "just:wardrobe") return;
    await system.waitTicks(1);
    const entities = player.dimension.getEntities({
        location: getblockFace(block,blockFace).center(),
        closest: 1,
        type: "just:wardrobe",
        excludeTags: ["rotated"]
    });
    if (!entities.length) return;
    const rot = player.getRotation().y + 180;
    let yRot = 0;
    if (rot >= 22.5 && rot < 67.5) yRot = 45
    else if (rot >= 67.5 && rot < 112.5) yRot = 90
    else if (rot >= 112.5 && rot < 157.5) yRot = 135
    else if (rot >= 157.5 && rot < 202.5) yRot = 180
    else if (rot >= 202.5 && rot < 247.5) yRot = 225
    else if (rot >= 247.5 && rot < 292.5) yRot = 270
    else if (rot >= 292.5 && rot < 337.5) yRot = 315
    else if (rot >= 292.5 && rot < 337.5) yRot = 315
    
    entities.forEach(entity=>{
        entity.setRotation({x:0,y:yRot});
        entity.addTag("rotated")
    })
})


world.beforeEvents.playerInteractWithBlock.subscribe(async({player, isFirstEvent, itemStack, block, blockFace})=>{
    if (itemStack?.typeId !== "just:hatter_table") return;
    await system.waitTicks(1);
    const entities = player.dimension.getEntities({
        location: getblockFace(block,blockFace).center(),
        closest: 1,
        type: "just:hatter_table",
        excludeTags: ["rotated"]
    });
    if (!entities.length) return;
    const rot = player.getRotation().y + 180;
    let yRot = 0;
    if (rot >= 22.5 && rot < 67.5) yRot = 45
    else if (rot >= 67.5 && rot < 112.5) yRot = 90
    else if (rot >= 112.5 && rot < 157.5) yRot = 135
    else if (rot >= 157.5 && rot < 202.5) yRot = 180
    else if (rot >= 202.5 && rot < 247.5) yRot = 225
    else if (rot >= 247.5 && rot < 292.5) yRot = 270
    else if (rot >= 292.5 && rot < 337.5) yRot = 315
    else if (rot >= 292.5 && rot < 337.5) yRot = 315
    
    entities.forEach(entity=>{
        entity.setRotation({x:0,y:yRot});
        entity.addTag("rotated")
    })
})