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

system.beforeEvents.startup.subscribe(eventData => {
    eventData.blockComponentRegistry.registerCustomComponent("rustic:openDoubleCabinet", {
        onPlayerInteract: e => {
            const { player, block, dimension, face, faceLocation} = e;
            const blockLocation = block.center()
            const blockFaceLocation = block.permutation.getState("minecraft:cardinal_direction");
            const cabinetPart = block.permutation.getState("r:double");
            let animationEntity;
            switch(blockFaceLocation){
                case 'north':
                    if ( face === 'North'){
                        if (cabinetPart === "bottom") {
                            block.above().setPermutation(block.above().permutation.withState("r:interact",true))
                            animationEntity = dimension.spawnEntity("rustic:double_cabinet_na",{ x: blockLocation.x, y: blockLocation.y - 0.509, z: blockLocation.z })
                        }
                        else if (cabinetPart === "upper") {
                            block.below().setPermutation(block.below().permutation.withState("r:interact",true))
                            animationEntity = dimension.spawnEntity("rustic:double_cabinet_na",{ x: blockLocation.x, y: blockLocation.y - 1.509, z: blockLocation.z })
                        }
                        block.setPermutation(block.permutation.withState("r:interact",true))
                        dimension.runCommand(`playsound random.chestopen @a ${blockLocation.x} ${blockLocation.y} ${blockLocation.z}`)
                        animationEntity.triggerEvent("minecraft:active_animation_event")
                    }
                    break;
                case 'south':
                    if ( face === 'South'){
                        if (cabinetPart === "bottom") {
                            block.above().setPermutation(block.above().permutation.withState("r:interact",true))
                            animationEntity = dimension.spawnEntity("rustic:double_cabinet_sa",{ x: blockLocation.x, y: blockLocation.y - 0.509, z: blockLocation.z })
                        }
                        else if (cabinetPart === "upper") {
                            block.below().setPermutation(block.below().permutation.withState("r:interact",true))
                            animationEntity = dimension.spawnEntity("rustic:double_cabinet_sa",{ x: blockLocation.x, y: blockLocation.y - 1.509, z: blockLocation.z })
                        }
                        block.setPermutation(block.permutation.withState("r:interact",true))
                        dimension.runCommand(`playsound random.chestopen @a ${blockLocation.x} ${blockLocation.y} ${blockLocation.z}`)
                        animationEntity.triggerEvent("minecraft:active_animation_event")
                    }
                    break;
                case 'east':
                    if ( face === 'East'){
                        if (cabinetPart === "bottom") {
                            block.above().setPermutation(block.above().permutation.withState("r:interact",true))
                            animationEntity =dimension.spawnEntity("rustic:double_cabinet_wa",{ x: blockLocation.x, y: blockLocation.y - 0.509, z: blockLocation.z })
                        }
                        else if (cabinetPart === "upper") {
                            block.below().setPermutation(block.below().permutation.withState("r:interact",true))
                            animationEntity =dimension.spawnEntity("rustic:double_cabinet_wa",{ x: blockLocation.x, y: blockLocation.y - 1.509, z: blockLocation.z })
                        }
                        block.setPermutation(block.permutation.withState("r:interact",true))
                        dimension.runCommand(`playsound random.chestopen @a ${blockLocation.x} ${blockLocation.y} ${blockLocation.z}`)
                        animationEntity.triggerEvent("minecraft:active_animation_event")
                    }
                    break;
                case 'west':
                    if ( face === 'West'){
                        if (cabinetPart === "bottom") {
                            block.above().setPermutation(block.above().permutation.withState("r:interact",true))
                            animationEntity =dimension.spawnEntity("rustic:double_cabinet_ea",{ x: blockLocation.x, y: blockLocation.y - 0.509, z: blockLocation.z })
                        }
                        else if (cabinetPart === "upper") {
                            block.below().setPermutation(block.below().permutation.withState("r:interact",true))
                            animationEntity =dimension.spawnEntity("rustic:double_cabinet_ea",{ x: blockLocation.x, y: blockLocation.y - 1.509, z: blockLocation.z })
                        }
                        block.setPermutation(block.permutation.withState("r:interact",true))
                        dimension.runCommand(`playsound random.chestopen @a ${blockLocation.x} ${blockLocation.y} ${blockLocation.z}`)
                        animationEntity.triggerEvent("minecraft:active_animation_event")
                    }
                    break;
            }
            
        },
        onPlayerBreak: e => {
            const { player, block, dimension } = e;
            const blockLocation = block.center()
            const entities = dimension.getEntities({location: blockLocation, maxDistance: 1})
            
            for (const entity of entities){
                if(entity.typeId === "rustic:double_cabinet_sa"){
                    entity.triggerEvent("minecraft:despawn_event")
                }
                else if(entity.typeId === "rustic:double_cabinet_na"){
                    entity.triggerEvent("minecraft:despawn_event")
                }
                else if(entity.typeId === "rustic:double_cabinet_ea"){
                    entity.triggerEvent("minecraft:despawn_event")
                }
                else if(entity.typeId === "rustic:double_cabinet_wa"){
                    entity.triggerEvent("minecraft:despawn_event")
                }
            }
            const blockBelow = block.below();
            const blockAbove = block.above();

            const cabinetPartAbove = blockAbove.permutation.getState("r:double");
            const cabinetPartBelow = blockBelow.permutation.getState("r:double");

            if( cabinetPartAbove === "upper" ) {
                blockAbove.setPermutation(blockAbove.permutation.withState("r:double","none"))
            }
            if( cabinetPartBelow === "bottom" ) {
                blockBelow.setPermutation(blockBelow.permutation.withState("r:double","none"))
            }
        },
    });
});

system.beforeEvents.startup.subscribe(eventData => {
    eventData.blockComponentRegistry.registerCustomComponent("rustic:closeDoubleCabinet", {
        onTick: e => {
            const { block, dimension } = e;
            const blockLocation = block.center()
            block.setPermutation(block.permutation.withState("r:interact",false))
            dimension.runCommand(`playsound random.chestclosed @a ${blockLocation.x} ${blockLocation.y} ${blockLocation.z}`)
        },
        onPlayerBreak: e => {
            const { player, block, dimension } = e;
            const blockLocation = block.center()
            const entities = dimension.getEntities({location: blockLocation, maxDistance: 1})
            for (const entity of entities){
                if(entity.typeId === "rustic:double_cabinet_sa"){
                    entity.triggerEvent("minecraft:despawn_event")
                }
                else if(entity.typeId === "rustic:double_cabinet_na"){
                    entity.triggerEvent("minecraft:despawn_event")
                }
                else if(entity.typeId === "rustic:double_cabinet_ea"){
                    entity.triggerEvent("minecraft:despawn_event")
                }
                else if(entity.typeId === "rustic:double_cabinet_wa"){
                    entity.triggerEvent("minecraft:despawn_event")
                }
            }
        },
        
    });
});

//ticking 6,6