let data = JsonIO.read('kubejs/config/food.json')  // GSON object
const MobEffectInstance = Java.loadClass('net.minecraft.world.effect.MobEffectInstance')

console.log('Loaded tooltips')

const nutritionTooltip = (item, lines) => {
    let healing = item.foodProperties.getNutrition()
    
    if(healing == 0) {
        return
    }

    let codes = '0'.repeat(parseInt(healing / 2))
    if(healing % 2) {
        codes += '1'
    }

    lines.add(1, { text: codes, font: "pwb:healing" })
    // return [ { text: codes, font: "pwb:healing" } ]
}

const formatDuration = duration => {
    let ticks = parseInt(duration / 20)
    let minute = Math.floor(ticks / 60).toString().padStart(2, '0')
    let seconds = parseInt(ticks % 60).toString().padStart(2, '0')

    return minute + ':' + seconds
}

const formatEffect = mobEffect => {
    const effect = mobEffect.effect

    return {
        color: effect.beneficial ? "blue" : "red",
        text: `${effect.displayName.getString()} (${formatDuration(mobEffect.duration)})`
    }
}

const formatChance = chance => {
    return {
        text: Math.floor(chance * 100) + "% "
    }
}

const givesEffectTooltip = (mobEffect, chance, lines) => {
    let components = []

    if(chance < 1) {
        components.push(formatChance(chance))
    }

    components.push(formatEffect(mobEffect))
    lines.push(components)
    // lines.add(components)
}

const queueAttributeTooltips = (mobEffect, attrLines) => {
    mobEffect.effect.attributeModifiers.forEach((attribute, modifier) => {
        let attrName = Text.translate(attribute.descriptionId).getString();
        let prefix = "+"
        if (modifier.operation.ordinal() == 0) {
            let amount = modifier.amount;
            prefix += Number.isInteger(amount) ? amount.toString() : amount.toFixed(2);
        } else {
            let amount = modifier.amount * 100;
            prefix += Number.isInteger(amount) ? amount.toString() : amount.toFixed(2);
            prefix += "%"
        }
        let effectString = modifier.amount > 0 ? "§9" : "§c";
        attrLines.push(`${effectString}${prefix} ${attrName}`)
    })
}

const attributeTooltips = (lines, attrLines) => {
    if (attrLines.length > 0) {
        lines.add("")
        lines.add({
            translate: "tooltip.kubejs.effect.applied",
            color: "dark_purple"
        })
        lines.addAll(attrLines)
    }
}

const tooltipForCustomEffect = (dataEntry, lines, attrLines) => {
    for(let effectDef of dataEntry.effects) {
        switch(effectDef.type) {
            case "give":
                const mobEffect = new MobEffectInstance(effectDef.effect, effectDef.time, effectDef.level, false, false)
                givesEffectTooltip(mobEffect, effectDef.chance, lines)
                // queueAttributeTooltips(mobEffect, attrLines)
                break
        }
    }
}

const hasFoodProperties = (stack) => {
    let foodProperties = stack.item.foodProperties;
    return foodProperties != null && !foodProperties.effects.isEmpty()
}

ItemEvents.tooltip(event => {
    console.log('Reloading tooltips')

    // for(let item of Ingredient.all.itemTypes.toArray()) {
    //     let foodProperties = item.foodProperties
    //     if(foodProperties == null) {
    //         continue
    //     }

    //     event.add(nutritionTooltip(item))
    // }

    event.addAdvanced(Ingredient.all.itemIds, (stack, advanced, lines) => {
        let foodProperties = stack.item.foodProperties
        if(foodProperties == null) {
            return
        }

        // let attrLines = []

        nutritionTooltip(stack.item, lines)

        // let dataEntry = data[stack.item.arch$registryName().toString()]
        // if(dataEntry != null) {
        //     tooltipForCustomEffect(dataEntry, lines, attrLines)
        // }

        // attributeTooltips(lines, attrLines)
    })

    for(let itemId of Object.keys(data)) {
        let lines = []
        tooltipForCustomEffect(data[itemId], lines)
        event.add(itemId, lines)
    }

    // const addEffect = (targets, effects) => {
        
    // }

    // for(let id in data) {
    //     console.log(id)
    //     addEffect(id, data[id].effects)
    // }
})