import { world } from '@minecraft/server';
import ScriptingModule from '../scriptingModule';
import { getInfectedEntities } from '../infectionStatus/utils';
import { infectNearbyBlock, isLocationInfected } from './main';
import { ChunkDataMap } from './chunk';
/*
import {
  debugChunkInfo,
  debugDumpProperties,
  debugInfect, debugNoiseMap, debugNoiseVis, debugSetProperty, debugTransform
} from './debug';
*/
const FOG_THRESHOLDS = [
    3, 7, 10, 15,
    // 20
];
export default class InfectWorldModule extends ScriptingModule {
    constructor() {
        super('InfectWorldModule');
    }
    onInitialize() {
        /*
        this.addScriptEvent('debug:transform', debugTransform);
        this.addScriptEvent('debug:infect', debugInfect);
        this.addScriptEvent('debug:noise_vis', debugNoiseVis);
        this.addScriptEvent('debug:noise_map', debugNoiseMap);
        this.addScriptEvent('debug:chunk_info', debugChunkInfo);
        this.addScriptEvent('debug:dump_properties', debugDumpProperties);
        this.addScriptEvent('debug:clear_properties', () => world.clearDynamicProperties());
        this.addScriptEvent('debug:set_property', debugSetProperty);
        */
    }
    infectWorldAroundEntities() {
        for (const status of getInfectedEntities()) {
            const { entity } = status;
            for (let i = 0; i < status.getInfectionLevel(); i++) {
                infectNearbyBlock(entity.location);
            }
        }
    }
    onTick() {
        this.infectWorldAroundEntities();
    }
    on3Seconds() {
        ChunkDataMap.save();
    }
    onSecond() {
        world.getAllPlayers().forEach((p) => {
            let fogScore = (p.getDynamicProperty('fog_score') ?? 0);
            const fogLevel = (p.getDynamicProperty('fog_level') ?? 0);
            if (isLocationInfected(p.location)) {
                fogScore = Math.min(fogScore + 1, 25);
            }
            else {
                fogScore = Math.max(fogScore - 2, 0);
            }
            if (p.hasTag('noFog')) {
                fogScore = 0;
            }
            const corruptionRange = {
                min: FOG_THRESHOLDS[fogLevel - 1] ?? 0,
                max: FOG_THRESHOLDS[fogLevel] ?? 99999999,
            };
            if (fogScore < corruptionRange.min) {
                const newLevel = fogLevel - 1;
                p.runCommand(`fog @s remove twb_wisu:wither_${fogLevel}`);
                p.setDynamicProperty('fog_level', newLevel);
            }
            else if (fogScore >= corruptionRange.max) {
                const newLevel = fogLevel + 1;
                p.runCommand(`fog @s push twb_wisu:fog_wither_${newLevel} twb_wisu:wither_${newLevel}`);
                p.setDynamicProperty('fog_level', newLevel);
            }
            p.setDynamicProperty('fog_score', fogScore);
            // p.onScreenDisplay.setActionBar(
            //   `${corruptionRange.min} - ${fogScore} - ${corruptionRange.max} (${fogLevel})`
            // );
        });
    }
}
