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

const crystalTypes = [
    'citrine', 'onyx', 'peridot', 'ruby', 'sapphire', 'selenite', 'topaz', 'amethyst', 'apatite'
];

world.afterEvents.playerPlaceBlock.subscribe(eventData => {
    const { block, player } = eventData;


    system.run(() => {
        if (!block) {
            return;
        }
        if (!block.hasTag('pu_biomes:crystal')) {
            return;
        }
        

        const [namespace, blockName] = block.typeId.split(':');
        const crystalType = crystalTypes.find(type => blockName === `${type}_crystal`);
        if (!crystalType) {
            return;
        }
        

        // Check block above for stacking logic
        const aboveBlock = block.above();
        if (aboveBlock && aboveBlock.typeId === block.typeId) {
            let updatedBlockPermutation = BlockPermutation.resolve(block.typeId, { 'pu_biomes:state': 'body' });
            block.setPermutation(updatedBlockPermutation);
        } else {
            let updatedBlockPermutation = BlockPermutation.resolve(block.typeId, { 'pu_biomes:state': 'top' });
            block.setPermutation(updatedBlockPermutation);
        }

        // Ensure the block immediately below 'top' is set to 'body2'
        const belowBlock = block.below();
        if (belowBlock && belowBlock.hasTag('pu_biomes:crystal')) {
            let updatedBelowBlockPermutation = BlockPermutation.resolve(belowBlock.typeId, { 'pu_biomes:state': 'body2' });
            belowBlock.setPermutation(updatedBelowBlockPermutation);
        }

        // Ensure all other blocks below body2 are body
        const belowBelowBlock = belowBlock ? belowBlock.below() : null;
        if (belowBelowBlock && belowBelowBlock.hasTag('pu_biomes:crystal')) {
            let updatedBelowBelowBlockPermutation = BlockPermutation.resolve(belowBelowBlock.typeId, { 'pu_biomes:state': 'body' });
            belowBelowBlock.setPermutation(updatedBelowBelowBlockPermutation);
        }

        // Check if there is a geodite block directly below and update its state
        const geoditeBlock = block.below();
        if (geoditeBlock && geoditeBlock.typeId === 'pu_biomes:geodite') {
            let updatedGeoditePermutation = BlockPermutation.resolve(geoditeBlock.typeId, { 'pu_biomes:crystal': crystalType });
            geoditeBlock.setPermutation(updatedGeoditePermutation);
        }
    });
});

world.afterEvents.playerBreakBlock.subscribe(eventData => {
    const { block, player } = eventData;


    system.run(() => {

        // Check block below for stacking logic
        const belowBlock = block.below();
        if (belowBlock && belowBlock.hasTag('pu_biomes:crystal')) {
            const belowBlockState = belowBlock.permutation.getState('pu_biomes:state');
            
            if (belowBlockState === 'body2') {
                let updatedBelowBlockPermutation = BlockPermutation.resolve(belowBlock.typeId, { 'pu_biomes:state': 'top' });
                belowBlock.setPermutation(updatedBelowBlockPermutation);
                
                const belowBelowBlock = belowBlock.below();
                if (belowBelowBlock && belowBelowBlock.hasTag('pu_biomes:crystal')) {
                    let updatedBelowBelowBlockPermutation = BlockPermutation.resolve(belowBelowBlock.typeId, { 'pu_biomes:state': 'body2' });
                    belowBelowBlock.setPermutation(updatedBelowBelowBlockPermutation);
                }
            } else if (belowBlockState === 'body') {
                let updatedBelowBlockPermutation = BlockPermutation.resolve(belowBlock.typeId, { 'pu_biomes:state': 'top' });
                belowBlock.setPermutation(updatedBelowBlockPermutation);
                
                const belowBelowBlock = belowBlock.below();
                if (belowBelowBlock && belowBelowBlock.hasTag('pu_biomes:crystal')) {
                    let updatedBelowBelowBlockPermutation = BlockPermutation.resolve(belowBelowBlock.typeId, { 'pu_biomes:state': 'body2' });
                    belowBelowBlock.setPermutation(updatedBelowBelowBlockPermutation);
                }
            }
        } else {
        }

        // Check if there is a geodite block below and remove its crystal state
        const geoditeBlock = block.below();
        if (geoditeBlock && geoditeBlock.typeId === 'pu_biomes:geodite') {
            let updatedGeoditePermutation = BlockPermutation.resolve(geoditeBlock.typeId, { 'pu_biomes:crystal': 'none' });
            geoditeBlock.setPermutation(updatedGeoditePermutation);
        }
    });
});