import { EntityEquippableComponent, EquipmentSlot, Player } from "@minecraft/server"
import { EntityHelper as EHelper } from "../../entities/utils/entityHelper"
import { WorldHelper } from "../../world/utils/worldHelper"

export const DUSKITE_ARMOR = {
    _headArmorId: "piki_beyondsurvival:duskite_helmet",
    _chestArmorId: "piki_beyondsurvival:duskite_chestplate",
    _legsArmorId: "piki_beyondsurvival:duskite_leggings",
    _bootsArmorId: "piki_beyondsurvival:duskite_boots",

    _equippedEffects: [
        EHelper.createEntityEffect("speed", 60, 0, true),
        EHelper.createEntityEffect("night_vision", 60, 0, true)
    ],
    _effectTimeRequirement: "Night",

    /**
     * 
     * @param {EntityEquippableComponent} equippable 
     * @returns 
     */
    shouldApplyArmorEffect: function(equippable) {
        if (WorldHelper.worldTimeString() != this._effectTimeRequirement) return false

        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

        return (headArmorItemId == this._headArmorId  &&
                chestArmorItemId?.includes(this._chestArmorId) &&
                legsArmorItemId == this._legsArmorId &&
                bootsArmorItemId == this._bootsArmorId
        )
    },
    
    /**
     * 
     * @param {Player} player 
     */
    applyEquippedEffect: function(player) {
        for (const effect of this._equippedEffects) {
            const {effectType, duration, amplifier, showParticles} = effect
            if (player.getEffect(effectType)) continue
            player.addEffect(effectType, duration, {amplifier: amplifier, showParticles: showParticles})
        }
    }
} 