import { system, world } from "@minecraft/server";
import { isEntityLoaded, isNightTime } from "../utils/utils";

const lastBlueMoon = world.getDynamicProperty("lastBlueMoon");
const nextBlueMoon = world.getDynamicProperty("nextBlueMoon");
const isBlueMoon = world.getDynamicProperty("isBlueMoon");

const currentDay = world.getDay();

if (isBlueMoon == undefined) {
    world.setDynamicProperty("isBlueMoon", false);
}
if (lastBlueMoon == undefined) {
    world.setDynamicProperty("lastBlueMoon", 0);
}
if (nextBlueMoon == undefined) {
    world.setDynamicProperty("nextBlueMoon", 20);
}

const BLUE_MOON_HANDLER = system.runInterval(() => {
    const isBlueMoon = world.getDynamicProperty("isBlueMoon");
  //  console.warn(world.getDynamicProperty("nextBlueMoon"));
    if (isNightTime()) {
        if (isBlueMoon == true) {
            const players = world.getAllPlayers();

            for (let player of players) {
                const topBlock =
                    player?.dimension.getTopmostBlock({
                        x: player.location.x,
                        z: player.location.z,
                    }) ?? player?.dimension.getBlock(player.location);

                player?.dimension.spawnParticle("px_hc:nebline_efx", topBlock.location);
            }
        }
    }
}, 1 * 20);

const daysBetweenBlueMoon = 20;

world.afterEvents.entitySpawn.subscribe((event) => {
    const { entity } = event;

    const currentDay = world.getDay();

    const isNight = isNightTime();
    const lastBlueMoon = world.getDynamicProperty("lastBlueMoon");
    const nextBlueMoon = world.getDynamicProperty("nextBlueMoon");
    const isBlueMoon = world.getDynamicProperty("isBlueMoon");
    const lastBloodMoon = world.getDynamicProperty("lastBloodMoon");
    const nextBloodMoon = world.getDynamicProperty("nextBloodMoon");
    const isBloodMoon = world.getDynamicProperty("isBloodMoon");

    if (isNight) {
        if (nextBloodMoon == nextBlueMoon) {
            setNextBlueMoon(currentDay + daysBetweenBlueMoon + 1);
        } else if (lastBlueMoon == currentDay) {
            setNextBlueMoon(currentDay + daysBetweenBlueMoon);
        }
        if (nextBlueMoon < currentDay) {
            setNextBlueMoon(currentDay + daysBetweenBlueMoon);
        }
        if (nextBlueMoon == currentDay) {
            system.runTimeout(() => {
                world.sendMessage("§bBlue Moon§r §eis in the sky, there will be no monsters!§r");
            }, 0.1 * 20);

            setBlueMoon(true);
            setLastBlueMoon(currentDay);
        }

        if (isBlueMoon) {
            system.runTimeout(() => {
                if (isEntityLoaded(entity)) {
                    if (entity.matches({ families: ["monster"] })) {
                        entity.remove();
                    }
                }
            }, 0.1 * 20);
        }
    } else if (isNight == false) {
        if (nextBlueMoon > currentDay) {
            setBlueMoon(false);
        }
    }
});

/**
 *
 * @param {Number} day
 */
function setNextBlueMoon(day) {
    world.setDynamicProperty("nextBlueMoon", day);
}
/**
 *
 * @param {Number} day
 */
function setLastBlueMoon(day) {
    world.setDynamicProperty("lastBlueMoon", day);
}

/**
 *
 * @param {Boolean} bool
 */
function setBlueMoon(bool) {
    world.setDynamicProperty("isBlueMoon", bool);
}
