import { world, EntityInventoryComponent, ItemStack, system, EquipmentSlot, GameMode, Player, EntityEquippableComponent } from '@minecraft/server'
import { daInjections } from './armors.modules';

system.runInterval(async () => {
    const players = world.getAllPlayers();

    for (const player of players) {
        const equipmentSlots = [
            player?.itemInHead,
            player?.itemInChest,
            player?.itemInLegs,
            player?.itemInFeet
        ];
        for (const item of equipmentSlots) {
            if (!daInjections.has(item?.typeId)) continue;

            const data = daInjections.get(item.typeId)?.setInterval;
            if (!data) continue;

            if (data?.requiresSneaking && !player.isSneaking) continue;
            if (data?.dependenceFullSet) {
                const toTest = data.dependenceFullSet.excludeHandsSlots ? equipmentSlots.slice(0, -2) : equipmentSlots;
                const test = toTest.every(item => item?.typeId.startsWith(data.dependenceFullSet.key));
                if (!test) continue;
            }

            if (data?.effectsToAdd) {
                for (const [effect, duration, amplifier, showParticles] of data.effectsToAdd) {
                    player.addEffect(effect, duration, { amplifier, showParticles });
                }
            }
            if (data?.effectsToRemove) {
                for (const effect of data.effectsToRemove) {
                    player.removeEffect(effect);
                }
            }
            if (data?.extraActions) {
                data.extraActions(player);
            }
        }

        await new Promise(resolve => system.runTimeout(resolve, 1));
    }
});
world.afterEvents.entityHurt.subscribe(({ hurtEntity: player, damageSource, damage }) => {
    const equipmentSlots = [
        player?.itemInHead,
        player?.itemInChest,
        player?.itemInLegs,
        player?.itemInFeet,
        player?.itemInHand,
        player?.itemInLeftHand
    ];
    for (const item of equipmentSlots) {
        if (!daInjections.has(item?.typeId)) continue;

        const data = daInjections.get(item.typeId)?.hurtEntity;
        if (!data) continue;

        if (data?.requiresSneaking && !player.isSneaking) continue;

        if (data?.effectsToAdd) {
            for (const [effect, duration, amplifier, showParticles] of data.effectsToAdd) {
                player.addEffect(effect, duration, { amplifier, showParticles });
            }
        }
        if (data?.effectsToRemove) {
            for (const effect of data.effectsToRemove) {
                player.removeEffect(effect);
            }
        }
        if (data?.extraActions) {
            data.extraActions(player, damageSource.cause, damage, damageSource?.damagingEntity || damageSource?.damagingProjectile);
        }
    }
    for (const [key, data] of daInjections.entries()) {
    if (!data?.hurtEntity?.dependenceFullSet) continue;

    const { dependenceFullSet } = data.hurtEntity;

    const toTest = dependenceFullSet.excludeHandsSlots ? equipmentSlots.slice(0, -2) : equipmentSlots;
    const isValidSet = toTest.every(item => item?.typeId?.startsWith(dependenceFullSet.key));
    if (!isValidSet) continue;
    const fullSetActions = data.hurtEntity;
    if (fullSetActions?.extraActions) {
        fullSetActions.extraActions(player, damageSource.cause, damage, damageSource?.damagingEntity || damageSource?.damagingProjectile);
    }
    }
});
world.afterEvents.entityHitEntity.subscribe(({ hitEntity, damagingEntity: player }) => {
    const equipmentSlots = [
        player?.itemInHead,
        player?.itemInChest,
        player?.itemInLegs,
        player?.itemInFeet,
        player?.itemInHand,
        player?.itemInLeftHand
    ];
    for (const item of equipmentSlots) {
        if (!daInjections.has(item?.typeId)) continue;

        const data = daInjections.get(item.typeId)?.hitEntity;
        if (!data) continue;

        if (data?.requiresSneaking && !player.isSneaking) continue;
        

        if (data?.effectsToAdd) {
            for (const [effect, duration, amplifier, showParticles] of data.effectsToAdd) {
                player.addEffect(effect, duration, { amplifier, showParticles });
            }
        }
        if (data?.effectsToRemove) {
            for (const effect of data.effectsToRemove) {
                player.removeEffect(effect);
            }
        }
        if (data?.extraActions) {
            data.extraActions(player, hitEntity);
        }
    }
    for (const [key, data] of daInjections.entries()) {
        if (!data?.hitEntity?.dependenceFullSet) continue;

        const { dependenceFullSet, extraActions } = data.hitEntity;
        const toTest = dependenceFullSet.excludeHandsSlots ? equipmentSlots.slice(0, -2) : equipmentSlots;

        const isValidSet = toTest.every(item => item?.typeId?.startsWith(dependenceFullSet.key));
        if (!isValidSet || !extraActions) continue;

        extraActions(player, hitEntity); // `player` = atacante, `target` = entidad golpeada
    }
});