import { world } from '@minecraft/server'
import { ActionFormData } from '@minecraft/server-ui'
import { actionNames, personalityNames, dinoData } from './cyd_dino_main'

const FORM = new ActionFormData()

world.beforeEvents.playerInteractWithEntity.subscribe(async (eventData) => {
    const { player, target: entityHit, itemStack } = eventData

    if (!entityHit?.typeId?.startsWith("cyd_dino:")) return
    if (itemStack?.typeId !== "cyd_dino:log") return
    //if (!player.isSneaking) return

    const translatedName = `${dinoData.get(entityHit.typeId).name ?? '? Dino'}`
    const translatedAbility = `${actionNames.get(entityHit.getProperty('cyd_dino:utility')) ?? '?'}`
    const translatedPersonality = `${personalityNames.get(entityHit.getProperty('cyd_dino:personality')) ?? '?'}`
    const tamingPercentage = `${entityHit.getProperty('cyd_dino:taming_percentage').toString() ?? '0'}`

    await showInformationMenu(translatedName, translatedAbility, translatedPersonality, tamingPercentage)
    FORM.show(player).then(result => {
        if (result.canceled) return
    })
})

function showInformationMenu(dinosaurName, dinosaurAbility, dinosaurPersonality, tamingPercentage) {
    FORM
        .title({ translate: dinosaurName })
        .body({
            translate: "cyd_dino.dynamic.information",
            "with": [
                dinosaurAbility, dinosaurPersonality,
                tamingPercentage
            ]
        })
}