import { system, world } from "@minecraft/server";
import { updateBlockState } from "lib_mcmv/blocks_mcmv";
import { randomNum } from "lib_mcmv/mcmv_logic";
system.runInterval(() => {
    for (const player of world.getAllPlayers()) {
        if (!player.isValid())
            return;
        const happyGhast = player.getComponent("riding")?.entityRidingOn;
        const ridingHappyGhast = player.hasTag("riding_mini_happy_ghast_pet");
        // console.warn(ridingHappyGhast)
        if (!happyGhast && ridingHappyGhast) {
            player.removeTag("riding_mini_happy_ghast_pet");
            return;
        }
        else if (happyGhast?.typeId == "reborn:mini_happy_ghast_pet" && !ridingHappyGhast) {
            player.addTag("riding_mini_happy_ghast_pet");
            return;
        }
    }
}, 2);
world.beforeEvents.worldInitialize.subscribe((data) => {
    data.blockComponentRegistry.registerCustomComponent("mini_happy_ghast_pet:main_detector", {
        onTick(e) {
            const block = e.block;
            const waterlogged = block.isWaterlogged;
            const waterloggedState = block.permutation.getState("mini_happy_ghast_pet:waterlogged");
            const MAX_SECONDARY_TIMER = 10;
            const MAX_MEMORY = 4;
            const MAX_MAIN_TIMER = 15;
            const secondaryTimer = block.permutation.getState("mini_happy_ghast_pet:secondary_timer");
            const memory = block.permutation.getState("mini_happy_ghast_pet:memory");
            const mainTimer = block.permutation.getState("mini_happy_ghast_pet:main_timer");
            const rehydrationLevel = block.permutation.getState("mini_happy_ghast_pet:rehydration_level");
            let actualMainTimer = mainTimer;
            const newSecondaryTimer = secondaryTimer + 1;
            if (waterlogged && !waterloggedState) {
                updateBlockState(block, { "mini_happy_ghast_pet:waterlogged": true, "mini_happy_ghast_pet:main_timer": 0, "mini_happy_ghast_pet:secondary_timer": 0, "mini_happy_ghast_pet:memory": 0 });
                return;
            }
            else if (!waterlogged && waterloggedState) {
                updateBlockState(block, { "mini_happy_ghast_pet:waterlogged": false, "mini_happy_ghast_pet:main_timer": 0, "mini_happy_ghast_pet:secondary_timer": 0, "mini_happy_ghast_pet:memory": 0 });
                return;
            }
            if (!waterloggedState && rehydrationLevel <= 0)
                return;
            if (newSecondaryTimer < MAX_SECONDARY_TIMER) {
                updateBlockState(block, { "mini_happy_ghast_pet:secondary_timer": newSecondaryTimer });
                if (waterlogged && waterloggedState && Math.random() > 0.9 && Math.random() < 0.4) {
                    const randomValueX = randomNum(0.1, 0.5);
                    const randomValueZ = randomNum(0.1, 0.5);
                    const shouldAddX = Math.random() < 0.5;
                    const shouldAddZ = Math.random() < 0.5;
                    block.dimension.spawnParticle("minecraft:villager_happy", { x: shouldAddX ? block.center().x + randomValueX : block.center().x - randomValueX, y: block.above().y - 0.25, z: shouldAddZ ? block.center().z + randomValueZ : block.center().z - randomValueZ });
                }
            }
            else {
                const newMemory = memory + 1;
                updateBlockState(block, { "mini_happy_ghast_pet:secondary_timer": 0, "mini_happy_ghast_pet:memory": newMemory });
                if (newMemory >= MAX_MEMORY) {
                    actualMainTimer = mainTimer + 1;
                    updateBlockState(block, { "mini_happy_ghast_pet:memory": 0, "mini_happy_ghast_pet:main_timer": actualMainTimer });
                }
            }
            if (actualMainTimer >= MAX_MAIN_TIMER) {
                if (waterloggedState && rehydrationLevel + 1 < 4) {
                    updateBlockState(block, { "mini_happy_ghast_pet:main_timer": 0, "mini_happy_ghast_pet:rehydration_level": rehydrationLevel + 1 });
                }
                else if (waterloggedState && rehydrationLevel + 1 > 3) {
                    const ghastling = block.dimension.spawnEntity("reborn:mini_happy_ghast_pet", block.center());
                    ghastling.triggerEvent("minecraft:spawn_baby");
                    block.setType("minecraft:air");
                }
                if (!waterloggedState && rehydrationLevel - 1 >= 0) {
                    updateBlockState(block, { "mini_happy_ghast_pet:main_timer": 0, "mini_happy_ghast_pet:rehydration_level": rehydrationLevel - 1 });
                }
            }
        },
    });
});
