import { world, ItemStack, system } from '@minecraft/server';
import { ActionFormData } from "@minecraft/server-ui";
import { OnTickGrowComponent } from "./tick_components";
import { OnInteractGrowComponent } from "./interaction_components";
import { HurtBlockComponent } from "./hurt_block";
import { SlowBlockComponent } from "./slow_block";
//import { StructureDebugger } from "./structure_debugger";

import './customLogs';
import './customFoliage';
import './fallenLeaves';
import './customCrystals';
import './smokePillar';
import './tallBush';
import './slab';
import './trapdoor';
import './hotrock';
import './customGrass';
import './entities';
import './boneMeal';
import './doors';
import './fences';
import './stairs';
import './fenceGate';
import './items';
import './branches';
import { customWorld } from './util';
import { FogHandler } from './fog_handler';
import { SoundHandler } from './sound_handler';
import './lamps';
import './leaves';
import { VanillaSpawner } from "./vanilla_spawner";

//import './blockDataView'

new VanillaSpawner();

//new StructureDebugger([
//    "pu_biomes:builds/autumn/cabin",
//    "pu_biomes:builds/autumn/pumpkin",
//    "pu_biomes:builds/autumn/temple",
//    "pu_biomes:builds/autumn/well",
//    "pu_biomes:builds/enchanted/castle",
//    "pu_biomes:builds/enchanted/fountain",
//    "pu_biomes:builds/enchanted/pavilion",
//    "pu_biomes:builds/enchanted/tower",
//    "pu_biomes:builds/horror/tower",
//    "pu_biomes:builds/horror/ruin_1",
//    "pu_biomes:builds/horror/ruin_2",
//    "pu_biomes:builds/horror/ruin_3",
//    "pu_biomes:builds/horror/ruin_4",
//    "pu_biomes:builds/horror/skull",
//    "pu_biomes:builds/lunarwood/ruins",
//    "pu_biomes:builds/lunarwood/tower",
//    "pu_biomes:builds/lunarwood/gate",
//    "pu_biomes:builds/lunarwood/post",
//    "pu_biomes:builds/mushroom/large_house",
//    "pu_biomes:builds/mushroom/well",
//    "pu_biomes:builds/mushroom/double_house",
//    "pu_biomes:builds/mushroom/house",
//    "pu_biomes:builds/red/tower",
//    "pu_biomes:builds/red/gate",
//    "pu_biomes:builds/red/house",
//    "pu_biomes:builds/red/shrine",
//    "pu_biomes:builds/red/tora_gate",
//    "pu_biomes:builds/sky/pavilion",
//    "pu_biomes:builds/sky/shrine",
//    "pu_biomes:builds/sky/statue",
//    "pu_biomes:builds/toxic/hut",
//    "pu_biomes:builds/toxic/skull",
//    "pu_biomes:builds/toxic/trunk",
//    "pu_biomes:builds/volcano/spikes",
//    "pu_biomes:builds/volcano/volcano",
//    "pu_biomes:builds/volcano/geyser",
//    "pu_biomes:builds/volcano/magma_1",
//    "pu_biomes:builds/volcano/magma_2"
//])


world.afterEvents.playerSpawn.subscribe((eventData) => {
    const player = eventData.player;
    if (player.hasTag('pu_biomes_initial_tag'))
        return;
    player.dimension.spawnItem(new ItemStack('pu_biomes:info_book'), { x: player.location.x, y: player.location.y, z: player.location.z });
    player.addTag('pu_biomes_initial_tag');
});

world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent(OnTickGrowComponent.id, new OnTickGrowComponent());
    initEvent.blockComponentRegistry.registerCustomComponent(OnInteractGrowComponent.id, new OnInteractGrowComponent());
    initEvent.blockComponentRegistry.registerCustomComponent(HurtBlockComponent.id, new HurtBlockComponent());
    initEvent.blockComponentRegistry.registerCustomComponent(SlowBlockComponent.id, new SlowBlockComponent());
});

const biomeIndex = new Map();

customWorld.afterEvents.BiomeChangedEvent.subscribe((event) => {
    biomeIndex.set(event.player.id, event.biome);
    new FogHandler(event.player, event.biome)
})
customWorld.afterEvents.PlayerThreadImpulseEvent.subscribe((event) => {
    const biome = biomeIndex.get(event.player.id);
    if (!biome) return;
    new SoundHandler(event.player, biome)
})