import * as mc from "@minecraft/server";

//#region - ENTITY REGISTERS
// Handle entities immune to freezing/cold
const immuneToFreezing = ["hf_mzs:frostmaw", "minecraft:rabbit", "minecraft:endermite", "minecraft:silverfish", "minecraft:shulker", "minecraft:vex", "minecraft:fox", "minecraft:polar_bear", "minecraft:stray"]
export function checkIfImmuneToFreezing(typeId) {
    for (var i = 0; i < immuneToFreezing.length; i++) {
        if (immuneToFreezing[i] === typeId) {
            ;
            return true;
        }
    }
    return false;
}

// Entity register
var e = [];
// Remove entity from registry
function clear(entity) {
    if (entity === undefined)
        return;
    for (var i = 0; i < e.length; i++) {
        if (e[i].id === entity.id) {
            e.splice(i, 1);
            return;
        }
    }
}
// Check if entity is in registry
function check(entity) {
    if (entity === undefined)
        return false;
    for (var i = 0; i < e.length; i++) {
        if (e[i].id === entity.id) {
            ;
            return true;
        }
    }
    return false;
}
// Register entity
function register(entity) {
    if (check(entity))
        return;
    e.push(entity);
}
// Set property so that it doesnt return undefined
function init(entity, effect) {
    if (entity.getDynamicProperty(effect) === undefined)
        entity.setDynamicProperty(effect, 0);
}
//#endregion

// Cold/freezing effect specific - clear when entity takes fire damage
export function onEntityHurt(event) {
    // make cold effects go away on fire damage
    if (event.hurtEntity.isValid() && check(event.hurtEntity) && (event.damageSource.cause === 'fire' || event.damageSource.cause === 'campfire' || event.damageSource.cause === 'fireTick')) {
        if (event.hurtEntity.getDynamicProperty('hf_mzs:cold'))
            event.hurtEntity.setDynamicProperty('hf_mzs:cold', 0)
        if (event.hurtEntity.getDynamicProperty('hf_mzs:freezing'))
            event.hurtEntity.setDynamicProperty('hf_mzs:freezing', 1)
    }
}

//#region - APPLY EFFECTS
export function applyFreezeEffect(entity, time) {
    //player.dimension.spawnEntity('hf_mzs:ice_cube', player.location);
    if (checkIfImmuneToFreezing(entity.typeId))
        return;
    register(entity);
    init(entity, 'hf_mzs:freezing');
    if (entity.getDynamicProperty('hf_mzs:freezing') === 0)
        entity.runCommand("/playsound hf_mzs:misc.freeze @a[r=16] ~~~");
    entity.setDynamicProperty('hf_mzs:freezing', time);
    if (entity.typeId !== 'minecraft:player') {
        entity.removeEffect("slowness");
        entity.addEffect("slowness", 20 * time, {
            amplifier: 10,
            showParticles: false
        });
        entity.addEffect("weakness", 20 * time, {
            amplifier: 10,
            showParticles: false
        });
    }
    else
        entity.runCommand('/function hf/mzs/effect/freeze');
}
export function applyColdEffect(entity, time) {
    if (checkIfImmuneToFreezing(entity.typeId))
        return;
    register(entity);
    init(entity, 'hf_mzs:cold');
    //mc.world.sendMessage(`${entity.getDynamicProperty('hf_mzs:cold')}`);
    if (entity.getDynamicProperty('hf_mzs:cold') === 5)
        applyFreezeEffect(entity, 60);
    entity.setDynamicProperty('hf_mzs:cold', Math.min(entity.getDynamicProperty('hf_mzs:cold') + time, 5));
}
export function applyPoisonResistance(entity, time) {
    if (checkIfImmuneToFreezing(entity.typeId))
        return;
    register(entity);
    init(entity, 'hf_mzs:poison_resistance');
    entity.setDynamicProperty('hf_mzs:poison_resistance', entity.getDynamicProperty('hf_mzs:poison_resistance') + time);
}
//#endregion 

// Entity must be removed from list after it dies
export function removeEntity(event) {
    if (event.deadEntity !== undefined && event.deadEntity.typeId !== 'player') {
        clear(event.deadEntity);
        return;
    }
    if (event.eventId === 'death' && event.entity.getComponent('minecraft:type_family').hasTypeFamily('hf_mzs_mob')) {
        clear(event.entity);
    }
}

// Effect logic, time countdown
export function tick20() {
    if (e.length < 1)
        return;
    for (let i = 0; i < e.length; i++) {
        var entity = e[i];
        if (entity === undefined || !entity.isValid())
            continue;

        // COLD EFFECT
        if (entity.getDynamicProperty('hf_mzs:cold') > 0) {
            entity.addEffect("slowness", 20, {
                amplifier: 1,
                showParticles: false
            });
            entity.setDynamicProperty('hf_mzs:cold', entity.getDynamicProperty('hf_mzs:cold') - 1);
        }

        // POISON RESISTANCE EFFECT
        if (entity.getDynamicProperty('hf_mzs:poison_resistance') > 1) {
            entity.setDynamicProperty('hf_mzs:poison_resistance', entity.getDynamicProperty('hf_mzs:poison_resistance') - 1)
            entity.runCommand("/particle hf_mzs:poison_resistance");
            entity.removeEffect("poison");
        }
        if (entity.getDynamicProperty('hf_mzs:poison_resistance') == 11) {
            entity.runCommand("/tellraw @s {\"rawtext\":[{\"translate\":\"message.hf_mzs.poison_resistance_10\"}]}");
        }
        if (entity.getDynamicProperty('hf_mzs:poison_resistance') == 1) {
            entity.runCommand("/tellraw @s {\"rawtext\":[{\"translate\":\"message.hf_mzs.poison_resistance_expired\"}]}");
            entity.runCommand('/function hf/mzs/effect/unfreeze');
            entity.setDynamicProperty('hf_mzs:poison_resistance', 0);
        }
    }
}
export function tick1() {
    if (e.length < 1)
        return;
    for (const entity of e) {
        if (entity === undefined || !entity.isValid())
            continue;

        // FREEZING EFFECT
        if (entity.getDynamicProperty('hf_mzs:freezing') >= 1) {
            entity.setDynamicProperty('hf_mzs:freezing', entity.getDynamicProperty('hf_mzs:freezing') - 1)
            entity.runCommand("/particle hf_mzs:frozen1");
            entity.runCommand("/particle hf_mzs:frozen2");
            entity.teleport(entity.location);
        }
        if (entity.getDynamicProperty('hf_mzs:freezing') == 1) {
            entity.runCommand('/function hf/mzs/effect/unfreeze');
            entity.runCommand("/playsound hf_mzs:misc.unfreeze @a[r=16] ~~~");
            entity.setDynamicProperty('hf_mzs:freezing', 0);
            entity.setDynamicProperty('hf_mzs:cold', 0);
            entity.removeEffect("slowness");
        }
        //mc.world.sendMessage(entity.typeId+"     "+entity.getDynamicProperty('hf_mzs:cold')+"      "+ entity.getDynamicProperty('hf_mzs:freezing'));
    }
}