package com.tiedup.remake.util;

import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import org.jetbrains.annotations.Nullable;

/**
 * Calculates the effects of feeding food items to NPCs.
 *
 * Effects include:
 * - Hunger restoration (based on nutrition)
 * - Mood boost (based on food quality)
 * - HP healing (based on saturation)
 * - Affinity increase (based on nutrition)
 */
public class FoodEffects {

    /**
     * Calculate the effects of feeding a food item to an NPC.
     *
     * @param food The food item stack
     * @return FeedResult with all effects, or null if not a food item
     */
    @Nullable
    public static FeedResult calculateFeedEffects(ItemStack food) {
        if (food.isEmpty()) return null;

        FoodProperties props = food.getItem().getFoodProperties();
        if (props == null) return null;

        int nutrition = props.getNutrition();
        float saturation = props.getSaturationModifier();

        // Base hunger restoration (nutrition value directly, feed() multiplies by 5)
        float hungerRestore = nutrition;

        // Mood boost based on food quality (nutrition 1-8 maps to mood 3-20)
        int moodBoost = Math.max(3, (int) (nutrition * 2.5f));

        // Special items get bonus mood
        if (food.is(Items.CAKE)) {
            moodBoost = 25;
        } else if (food.is(Items.GOLDEN_APPLE)) {
            moodBoost = 30;
        } else if (food.is(Items.ENCHANTED_GOLDEN_APPLE)) {
            moodBoost = 50;
        } else if (food.is(Items.COOKIE)) {
            moodBoost = 15;
        } else if (food.is(Items.HONEY_BOTTLE)) {
            moodBoost = 20;
        } else if (food.is(Items.PUMPKIN_PIE)) {
            moodBoost = 18;
        } else if (
            food.is(Items.SWEET_BERRIES) || food.is(Items.GLOW_BERRIES)
        ) {
            moodBoost = 12;
        } else if (food.is(Items.CHORUS_FRUIT)) {
            moodBoost = 8; // Weird taste
        } else if (food.is(Items.ROTTEN_FLESH) || food.is(Items.SPIDER_EYE)) {
            moodBoost = -5; // Disgusting
        } else if (food.is(Items.POISONOUS_POTATO)) {
            moodBoost = -10; // Very bad
        }

        // Heal HP based on saturation (saturation * 2, min 1)
        float healAmount = Math.max(1.0f, saturation * 2.0f);

        // Affinity boost based on nutrition (min 1, max 4)
        int affinityBoost = Math.max(1, Math.min(4, nutrition / 2));

        // Special items get bonus affinity
        if (
            food.is(Items.GOLDEN_APPLE) || food.is(Items.ENCHANTED_GOLDEN_APPLE)
        ) {
            affinityBoost = 8;
        } else if (food.is(Items.CAKE)) {
            affinityBoost = 6;
        }

        return new FeedResult(
            hungerRestore,
            moodBoost,
            healAmount,
            affinityBoost
        );
    }

    /**
     * Result of feeding calculation.
     *
     * @param hunger   How much hunger to restore (passed to NpcNeeds.feed())
     * @param mood     How much mood to add
     * @param heal     How much HP to heal
     * @param affinity How much affinity to add with the feeding player
     */
    public record FeedResult(
        float hunger,
        int mood,
        float heal,
        int affinity
    ) {}
}
