import { MolangVariableMap, Player, system, world } from '@minecraft/server';
import ScriptingModule from '../scriptingModule';
import EntityStatus from './main';
import { getInfectedEntities } from './utils';
export default class InfectionStatusModule extends ScriptingModule {
    constructor() {
        super('InfectionStatusModule');
    }
    getNumberParameterFromScriptEvent(e) {
        if (!e.sourceEntity)
            return undefined;
        const isPlayer = e.sourceEntity instanceof Player;
        if (!e.message) {
            if (isPlayer)
                e.sourceEntity.sendMessage('§cMissing message parameter!');
            return undefined;
        }
        const timeLeft = parseInt(e.message, 10);
        if (Number.isNaN(timeLeft)) {
            if (isPlayer)
                e.sourceEntity.sendMessage('§cMessage parameter should be a valid number!');
            return undefined;
        }
        return timeLeft;
    }
    storeLastDisconnectTime(player) {
        player.setDynamicProperty('twb_wisu:last_seen', system.currentTick);
    }
    updateStatusDurationOnReconnect(player) {
        const lastSeen = player.getDynamicProperty('twb_wisu:last_seen');
        if (!lastSeen)
            return;
        const status = new EntityStatus(player);
        status.correctForOfflineTime(lastSeen);
    }
    onInitialize() {
        // Update status duration after reconnecting
        world.beforeEvents.playerLeave.subscribe((e) => this.storeLastDisconnectTime(e.player));
        world.afterEvents.playerJoin.subscribe((e) => {
            // Wait until player is loaded
            const runId = system.runInterval(() => {
                const player = world.getEntity(e.playerId);
                if (!player)
                    return;
                this.updateStatusDurationOnReconnect(player);
                system.clearRun(runId);
            }, 1);
            // Safeguard to stop the interval after 30 seconds
            system.runTimeout(() => {
                console.log(`Player ${e.playerName} not found within 30 seconds of joining`);
                system.clearRun(runId);
            }, 20 * 30);
        });
        // Script events to set status effects
        this.addScriptEvent('twb_wisu:set_infection_time', (e) => {
            const timeLeft = this.getNumberParameterFromScriptEvent(e);
            if (timeLeft === undefined)
                return;
            new EntityStatus(e.sourceEntity).setRemainingInfectionTime(timeLeft, true);
            if (e.sourceEntity instanceof Player)
                e.sourceEntity.sendMessage(`Set your remaining infection time to ${timeLeft} ticks!`);
        });
        this.addScriptEvent('twb_wisu:set_infection_level', (e) => {
            const level = this.getNumberParameterFromScriptEvent(e);
            if (level === undefined)
                return;
            new EntityStatus(e.sourceEntity).setInfectionLevel(level, true);
            if (e.sourceEntity instanceof Player)
                e.sourceEntity.sendMessage(`Set your infection level to ${level}!`);
        });
    }
    spawnParticlesAtInfectedEntities() {
        const molang = new MolangVariableMap();
        molang.setColorRGBA('variable.color', {
            red: 0, green: 0, blue: 0, alpha: 1
        });
        getInfectedEntities().forEach((s) => {
            s.entity.dimension.spawnParticle('minecraft:mobspell_emitter', s.entity.location, molang);
        });
    }
    onHalfSecond() {
        // this.spawnParticlesAtInfectedEntities();
    }
}
