import { Entity, EntityDamageCause, system } from "@minecraft/server"
import { Piki } from "../../utils/typingConstants"
import { EntityComponents } from "../utils/entityComponents"
import { CREEPER } from "../specific/creeper"
import { CommandHelper as CHelper } from "../../utils/commandHelper"

export const CUSTOM_ZOMBIES = {
    _familyType: "zombie",
    _lootPathSuffix: "_h",


    /**
     * 
     * @param {Entity} entity 
     * @param {Entity} damagingEntity
     * @param {EntityDamageCause} damageCause 
     */
    shouldDropHead(entity, damagingEntity, damageCause) {
        if (damageCause != EntityDamageCause.entityExplosion) return
        if (!entity.typeId.startsWith(Piki.namespace)) return
        if (!EntityComponents.getFamilyType(entity).hasTypeFamily(this._familyType)) return
        if (CREEPER.isMarkedAsCharged(damagingEntity)) return
        
        return true
    },
    
    /**
     * 
     * @param {Entity} deadEntity 
     */
    dropHead(deadEntity) {
        const typeId = deadEntity.typeId
        const lootPath = this._getDropLootPath(typeId)
        const command = CHelper.generateSpawnLootCommand(deadEntity.location, lootPath)
        system.run(() => deadEntity.runCommandAsync(command))
    },

    /**
     * HACK: Better to add this in json file
     * @param {string} typeId
     */
    _getDropLootPath(typeId) {
        return `${Piki.blockLootsPath}${typeId.replace(Piki.namespace, "")}${this._lootPathSuffix}`
    }

}