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

system.beforeEvents.startup.subscribe(eventData => {
    eventData.blockComponentRegistry.registerCustomComponent("rustic:openCabinet", {
        onPlayerInteract: e => {
            const { player, block, dimension, face, faceLocation} = e;
            const blockLocation = block.center()
            const blockFaceLocation = block.permutation.getState("minecraft:cardinal_direction");
            let animationEntity;
            switch(blockFaceLocation){
                case 'north':
                    if ( face === 'North'){
                        block.setPermutation(block.permutation.withState("r:interact",true))
                        dimension.runCommand(`playsound random.chestopen @a ${blockLocation.x} ${blockLocation.y} ${blockLocation.z}`)
                        animationEntity = dimension.spawnEntity("rustic:cabinet_na",{ x: blockLocation.x, y: blockLocation.y - 0.509, z: blockLocation.z })
                        animationEntity.triggerEvent("minecraft:active_animation_event")
                    }
                    break;
                case 'south':
                    if ( face === 'South'){
                        block.setPermutation(block.permutation.withState("r:interact",true))
                        dimension.runCommand(`playsound random.chestopen @a ${blockLocation.x} ${blockLocation.y} ${blockLocation.z}`)
                        animationEntity = dimension.spawnEntity("rustic:cabinet_sa",{ x: blockLocation.x, y: blockLocation.y - 0.509, z: blockLocation.z })
                        animationEntity.triggerEvent("minecraft:active_animation_event")
                    }
                    break;
                case 'east':
                    if ( face === 'East'){
                        block.setPermutation(block.permutation.withState("r:interact",true))
                        dimension.runCommand(`playsound random.chestopen @a ${blockLocation.x} ${blockLocation.y} ${blockLocation.z}`)
                        animationEntity =dimension.spawnEntity("rustic:cabinet_wa",{ x: blockLocation.x, y: blockLocation.y - 0.509, z: blockLocation.z })
                        animationEntity.triggerEvent("minecraft:active_animation_event")
                    }
                    break;
                case 'west':
                    if ( face === 'West'){
                        block.setPermutation(block.permutation.withState("r:interact",true))
                        dimension.runCommand(`playsound random.chestopen @a ${blockLocation.x} ${blockLocation.y} ${blockLocation.z}`)
                        animationEntity =dimension.spawnEntity("rustic:cabinet_ea",{ x: blockLocation.x, y: blockLocation.y - 0.509, z: blockLocation.z })
                        animationEntity.triggerEvent("minecraft:active_animation_event")
                    }
                    break;
            }
            
        },
        beforeOnPlayerPlace: e => {
            const { player, block, face, permutationToPlace, dimension } = e;
            const blockLocation = block.center()
            system.run(() => {dimension.runCommand(`summon rustic:cabinet_chest ${blockLocation.x} ${blockLocation.y - 0.4} ${blockLocation.z} ~ ~ rustic:cabinet_chest cabinet_chest`)})
            
        },
        onPlace: e => {
            const { block, dimension, previousBlock } = e;
            const blockBelow = block.below();
            const blockAbove = block.above();
            const blockAboveFaceLocation = blockAbove.permutation.getState("minecraft:cardinal_direction");
            const blockBelowFaceLocation = blockBelow.permutation.getState("minecraft:cardinal_direction");
            const blockFaceLocation = block.permutation.getState("minecraft:cardinal_direction");

            
            if(blockAboveFaceLocation === blockFaceLocation){
                if( blockAbove.hasTag('cabinet') && !blockAbove.hasTag('double')) {
                    block.setPermutation(block.permutation.withState("r:double","bottom"))
                    blockAbove.setPermutation(blockAbove.permutation.withState("r:double","upper"))
                }
            }
            if(blockBelowFaceLocation === blockFaceLocation){
                if( blockBelow.hasTag('cabinet') && !blockBelow.hasTag('double')) {
                    block.setPermutation(block.permutation.withState("r:double","upper"))
                    blockBelow.setPermutation(blockBelow.permutation.withState("r:double","bottom"))
                }
            }
            
        },
        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:cabinet_sa"){
                    entity.triggerEvent("minecraft:despawn_event")
                }
                else if(entity.typeId === "rustic:cabinet_na"){
                    entity.triggerEvent("minecraft:despawn_event")
                }
                else if(entity.typeId === "rustic:cabinet_ea"){
                    entity.triggerEvent("minecraft:despawn_event")
                }
                else if(entity.typeId === "rustic:cabinet_wa"){
                    entity.triggerEvent("minecraft:despawn_event")
                }
            }
        },
    });
});

system.beforeEvents.startup.subscribe(eventData => {
    eventData.blockComponentRegistry.registerCustomComponent("rustic:closeCabinet", {
        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:cabinet_sa"){
                    entity.triggerEvent("minecraft:despawn_event")
                }
                else if(entity.typeId === "rustic:cabinet_na"){
                    entity.triggerEvent("minecraft:despawn_event")
                }
                else if(entity.typeId === "rustic:cabinet_ea"){
                    entity.triggerEvent("minecraft:despawn_event")
                }
                else if(entity.typeId === "rustic:cabinet_wa"){
                    entity.triggerEvent("minecraft:despawn_event")
                }
            }
        },
        
    });
});

//ticking 6,6