import { world, system, ItemStack } from "@minecraft/server";
import { fo } from './utilities/fo_constants.js';




export function initEnderDragonInjure() {

    
    if (world.getDynamicProperty("EnderDragonInjure") === undefined) {
        world.setDynamicProperty("EnderDragonInjure", false);
    }

    if (world.getDynamicProperty("EnderDragonInjure") === false) {
        const theEnd = fo.theEnd;
        const players = theEnd.getPlayers();
        if (players.length > 0) return;
        fightSigns(theEnd);

    }
};

function fightSigns(theEnd) {
    const endSpikeCoords = [
        { x: 42, y: 120, z: 0 },
        { x: 33, y: 120, z: 24 },
        { x: -12, y: 120, z: 39 },
        { x: -33, y: 120, z: 24 },
        { x: -42, y: 120, z: 0 },
        { x: -33, y: 120, z: -24 },
        { x: -12, y: 120, z: -39 },
        { x: 12, y: 120, z: -39 },
    ]

    theEnd.runCommand(`tickingarea add 45 0 45 -45 0 -45 foTheEnd`);

    system.runTimeout(() => {

        
        const ExplosionPos1 = theEnd.getTopmostBlock({ x: 5, y: 64, z: 1 });
        const ExplosionPos2 = theEnd.getTopmostBlock({ x: 3, y: 64, z: -3 });
        const ExplosionPos3 = theEnd.getTopmostBlock({ x: -2, y: 64, z: -3 });

        const explosionsRules = {
            breaksBlocks: true,
            causesFire: true,
            allowUnderwater: false
        }

        if (ExplosionPos1 && ExplosionPos2 && ExplosionPos3) {
            theEnd.createExplosion(ExplosionPos1.above(1).location, 8, explosionsRules);
            theEnd.createExplosion(ExplosionPos2.above(1).location, 8, explosionsRules);
            theEnd.createExplosion(ExplosionPos3.above(1).location, 8, explosionsRules);

            system.runTimeout(() => {
                theEnd.createExplosion(ExplosionPos1.above(1).east(2).location, 8, explosionsRules);
                theEnd.createExplosion(ExplosionPos2.above(1).east(2).north(1).location, 8, explosionsRules);
                theEnd.createExplosion(ExplosionPos3.above(1).north(3).location, 8, explosionsRules);


                
                const bedPos = theEnd.getTopmostBlock({ x: -3, y: 64, z: 3 });
                if (bedPos) {
                    theEnd.runCommand(`setblock ${bedPos.x} ${bedPos.y + 1} ${bedPos.z} minecraft:bed`);
                };

            }, 5)

        };

        
        const waterPos = theEnd.getTopmostBlock({ x: -5, y: 64, z: 20 });
        if (waterPos) {
            theEnd.runCommand(`setblock ${waterPos.location.x} ${waterPos.above(1).location.y} ${waterPos.location.z} minecraft:flowing_water`);
        }

        
        const arrowItem = new ItemStack("arrow", 37);
        theEnd.spawnItem(arrowItem, { x: -4, y: 110, z: 20 });

        const invisibilityItem = new ItemStack("ender_eye", 3);
        theEnd.spawnItem(invisibilityItem, { x: -4, y: 110, z: 19 });

        const glassBottleItem = new ItemStack("glass_bottle", 3);
        theEnd.spawnItem(glassBottleItem, { x: -5, y: 110, z: 19 });


        
        system.runTimeout(() => {
            for (const coord of endSpikeCoords) {
                theEnd.runCommand(`summon arrow ${coord.x} ${coord.y} ${coord.z}`)
            }
        }, 18);

        
        system.runTimeout(() => {
            theEnd.runCommand(`tickingarea remove foTheEnd`);
            world.setDynamicProperty("EnderDragonInjure", true);
        }, 20);

    }, 100)

}