import { Entity, EntityEquippableComponent, EquipmentSlot, Player } from "@minecraft/server";
import { randomChance } from "../../utils/systemHelper";
import { EntityHelper as EHelper } from "../../entities/utils/entityHelper";

export const VENOMITE_ARMOR = {
    _headArmorId: "piki_beyondsurvival:venomite_helmet",
    _chestArmorId: "piki_beyondsurvival:venomite_chestplate",
    _legsArmorId: "piki_beyondsurvival:venomite_leggings",
    _bootsArmorId: "piki_beyondsurvival:venomite_boots",

    _invulnerableEffectTypes: ["poison", "fatal_poison"],
    _attackEffect: EHelper.createEntityEffect("poison", 6, 1, true),

    /**
     * 
     * @param {EntityEquippableComponent} equippable 
     * @param {boolean} checkChance
     */
    isArmorActive(equippable, checkChance) {
        if (!equippable) return

        const headArmorItem = equippable.getEquipment(EquipmentSlot.Head)
        const chestArmorItem = equippable.getEquipment(EquipmentSlot.Chest)
        const legsArmorItem = equippable.getEquipment(EquipmentSlot.Legs)
        const bootsArmorItem = equippable.getEquipment(EquipmentSlot.Feet)

        const headArmorItemId = headArmorItem?.typeId
        const chestArmorItemId = chestArmorItem?.typeId
        const legsArmorItemId = legsArmorItem?.typeId
        const bootsArmorItemId = bootsArmorItem?.typeId

        let chance = 0
        if (headArmorItemId == this._headArmorId) chance += 0.25
        if (chestArmorItemId?.includes(this._chestArmorId)) chance += 0.25
        if (legsArmorItemId == this._legsArmorId ) chance += 0.25
        if (bootsArmorItemId == this._bootsArmorId) chance += 0.25    

        if (checkChance) return randomChance(chance)
        else return chance > 0
    },

    /**
     * 
     * @param {Entity} entity 
     */
    applyPoisonAttack(entity) {
        const {effectType, duration, amplifier, showParticles} = this._attackEffect
        entity.addEffect(effectType, duration, {amplifier: amplifier, showParticles: showParticles})
    },
    /**
     * 
     * @param {Player} player 
     */
    poisonInvulnerableEffect(player) {
        this._invulnerableEffectTypes.forEach(effect => player.removeEffect(effect))
    }
}