import { BlockPermutation, EquipmentSlot, GameMode, ItemStack, world } from '@minecraft/server';
import ScriptingModule from '../scriptingModule';
import AshLayerComponent from './components/ashLayer';
import BloomingWiltComponent from './components/bloomingWilt';
import CarrotCakeComponent from './components/carrotCake';
import DoorComponent from './components/door';
import HollowedCactusComponent from './components/hollowedCactus';
import TallFlowerComponent from './components/tallFlower';
import TrapdoorComponent from './components/trapdoor';
import TurnIntoAshComponent from './components/turnIntoAsh';
// import WitherParticleEmitterComponent from './components/witherParticleEmitter';
import WitheredLogComponent from './components/witheredLog';
import WitheredSoulLogComponent from './components/witheredSoulLog';
import { createNoise3D } from '../util/simplexNoise';
import { mulberryPRNG } from '../util/random';
import { decreaseItemAmountInHand, getItemInSlot, setItemInSlot } from '../util/utils';
import { landAshOnBlock } from './ash';
import SoulFireComponent from './components/soulFire';
const sampleNoise = createNoise3D(mulberryPRNG(54321));
export default class BlocksModule extends ScriptingModule {
    constructor() {
        super('BlocksModule');
    }
    onInitialize() {
        world.beforeEvents.worldInitialize.subscribe((e) => {
            e.blockComponentRegistry.registerCustomComponent('twb_wisu:ash_layer', new AshLayerComponent());
            e.blockComponentRegistry.registerCustomComponent('twb_wisu:blooming_wilt', new BloomingWiltComponent());
            e.blockComponentRegistry.registerCustomComponent('twb_wisu:carrot_cake', new CarrotCakeComponent());
            e.blockComponentRegistry.registerCustomComponent('twb_wisu:door', new DoorComponent());
            e.blockComponentRegistry.registerCustomComponent('twb_wisu:hollowed_cactus', new HollowedCactusComponent());
            e.blockComponentRegistry.registerCustomComponent('twb_wisu:soul_fire', new SoulFireComponent());
            e.blockComponentRegistry.registerCustomComponent('twb_wisu:tall_flower', new TallFlowerComponent());
            e.blockComponentRegistry.registerCustomComponent('twb_wisu:trapdoor', new TrapdoorComponent());
            e.blockComponentRegistry.registerCustomComponent('twb_wisu:turn_into_ash', new TurnIntoAshComponent());
            // eslint-disable-next-line max-len
            // e.blockComponentRegistry.registerCustomComponent('twb_wisu:wither_particle_emitter', new WitherParticleEmitterComponent());
            e.blockComponentRegistry.registerCustomComponent('twb_wisu:withered_log', new WitheredLogComponent());
            e.blockComponentRegistry.registerCustomComponent('twb_wisu:withered_soul_log', new WitheredSoulLogComponent());
        });
        world.afterEvents.itemUseOn.subscribe((e) => {
            if (e.itemStack.typeId !== 'twb_wisu:witherlily')
                return;
            const block = e.block.above();
            if (!block || !block.isAir)
                return;
            e.source.playSound('hit.grass', { volume: 0.5 });
            const noise = sampleNoise(block.x * 0.5, block.y * 0.5, block.z * 0.5);
            let facing;
            if (noise <= -0.3)
                facing = 0;
            else if (noise <= 0.0)
                facing = 1;
            else if (noise <= 0.3)
                facing = 2;
            else
                facing = 3;
            // eslint-disable-next-line @typescript-eslint/naming-convention
            block.setPermutation(BlockPermutation.resolve('twb_wisu:witherlily_block', { 'twb_wisu:facing': facing, }));
            decreaseItemAmountInHand(e.source, 1, true);
        });
        world.afterEvents.playerPlaceBlock.subscribe((e) => {
            const below = e.block.below();
            if (below?.permutation.matches('twb_wisu:ash_layer', { 'twb_wisu:level': 7 })) {
                below.setType('twb_wisu:ash');
            }
        });
        this.addScriptEvent('twb_wisu:falling_ash_land', (e) => this.onFallingAshLand(e));
    }
    onTwoTick() {
        world.getPlayers({ gameMode: GameMode.creative }).forEach((p) => {
            const heldItem = getItemInSlot(p, EquipmentSlot.Mainhand);
            if (heldItem?.typeId === 'twb_wisu:witherlily_block') {
                setItemInSlot(p, EquipmentSlot.Mainhand, new ItemStack('twb_wisu:witherlily', heldItem.amount));
            }
        });
    }
    onFallingAshLand(e) {
        if (e.sourceEntity?.typeId !== 'twb_wisu:falling_ash')
            return;
        const amount = e.sourceEntity.getProperty('twb_wisu:level');
        const block = e.sourceEntity.dimension.getBlock(e.sourceEntity.location);
        e.sourceEntity.triggerEvent('twb_wisu:despawn');
        if (block)
            landAshOnBlock(block, amount);
    }
}
