package net.yyc12345.teyvatcraft.items;

import java.util.EnumSet;
import java.util.List;

import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.item.FoodComponent;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.item.FoodComponent.Builder;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting;
import net.minecraft.util.Rarity;
import net.minecraft.world.World;

public class TeyvatFoodItem extends Item {
    public TeyvatFoodItem(ItemGroup ig, int star, EnumSet<TeyvatFoodType> types) {
        super(new FabricItemSettings().group(ig).rarity(getRarity(star)).food(getTeyvatFoodComponent(star, types)));
        
        mStar = star;
        mEffect = types;
    }

    private int mStar;
    private EnumSet<TeyvatFoodType> mEffect;

    private static FoodComponent getTeyvatFoodComponent(int star, EnumSet<TeyvatFoodType> types) {
        Builder foodComponent = (new FoodComponent.Builder()).hunger(2 * star).saturationModifier(3.0F * star).alwaysEdible();

        if (types.contains(TeyvatFoodType.Revive)) ;
        if (types.contains(TeyvatFoodType.DecreaseColdAccumulation)) ;
        if (types.contains(TeyvatFoodType.DecreaseStamina_Sprint)) foodComponent.statusEffect(new StatusEffectInstance(StatusEffects.SPEED, 900 * 20, star - 1), 1.0F);
        if (types.contains(TeyvatFoodType.DecreaseStamina_Climb)) foodComponent.statusEffect(new StatusEffectInstance(StatusEffects.JUMP_BOOST, 900 * 20, star - 1), 1.0F);
        if (types.contains(TeyvatFoodType.DecreaseStamina_Misc)) foodComponent.statusEffect(new StatusEffectInstance(StatusEffects.HASTE, 900 * 20, star - 1), 1.0F);
        if (types.contains(TeyvatFoodType.RestoreHP)) foodComponent.statusEffect(new StatusEffectInstance(StatusEffects.INSTANT_HEALTH, 1, star - 1), 1.0F);
        if (types.contains(TeyvatFoodType.GraduallyRestoreHP)) foodComponent.statusEffect(new StatusEffectInstance(StatusEffects.REGENERATION, 30 * 20, star - 1), 1.0F);
        if (types.contains(TeyvatFoodType.RestoreStamina)) foodComponent.statusEffect(new StatusEffectInstance(StatusEffects.SATURATION, 50 * 20, star - 1), 1.0F);
        if (types.contains(TeyvatFoodType.IncreaseATK_CRIT_DMG)) foodComponent.statusEffect(new StatusEffectInstance(StatusEffects.STRENGTH, 300 * 20, star - 1), 1.0F);
        if (types.contains(TeyvatFoodType.IncreaseDEF)) foodComponent.statusEffect(new StatusEffectInstance(StatusEffects.RESISTANCE, 300 * 20, star - 1), 1.0F);
        if (types.contains(TeyvatFoodType.IncreaseShieldStrength)) foodComponent.statusEffect(new StatusEffectInstance(StatusEffects.ABSORPTION, 300 * 20, star - 1), 1.0F);

        return foodComponent.build();
    }

    private static Rarity getRarity(int star) {
        switch (star) {
            case 1:
            case 2:
            case 3:
                return Rarity.COMMON;
            case 4:
                return Rarity.UNCOMMON;
            case 5:
                return Rarity.RARE;
            default:
                return Rarity.COMMON;
        }
    }

    @Override
    public void appendTooltip(ItemStack itemStack, World world, List<Text> tooltip, TooltipContext tooltipContext) {
        tooltip.add(new TranslatableText("item.teyvatcraft.misc.foodStar", mStar).formatted(Formatting.ITALIC, Formatting.GRAY));

        if (mEffect.contains(TeyvatFoodType.Revive)) ;
        if (mEffect.contains(TeyvatFoodType.DecreaseColdAccumulation)) ;
        if (mEffect.contains(TeyvatFoodType.DecreaseStamina_Sprint)) tooltip.add(new TranslatableText("effect.minecraft.speed").formatted(Formatting.ITALIC, Formatting.GRAY));
        if (mEffect.contains(TeyvatFoodType.DecreaseStamina_Climb)) tooltip.add(new TranslatableText("effect.minecraft.jump_boost").formatted(Formatting.ITALIC, Formatting.GRAY));
        if (mEffect.contains(TeyvatFoodType.DecreaseStamina_Misc)) tooltip.add(new TranslatableText("effect.minecraft.haste").formatted(Formatting.ITALIC, Formatting.GRAY));
        if (mEffect.contains(TeyvatFoodType.RestoreHP)) tooltip.add(new TranslatableText("effect.minecraft.instant_health").formatted(Formatting.ITALIC, Formatting.GRAY));
        if (mEffect.contains(TeyvatFoodType.GraduallyRestoreHP)) tooltip.add(new TranslatableText("effect.minecraft.regeneration").formatted(Formatting.ITALIC, Formatting.GRAY));
        if (mEffect.contains(TeyvatFoodType.RestoreStamina)) tooltip.add(new TranslatableText("effect.minecraft.saturation").formatted(Formatting.ITALIC, Formatting.GRAY));
        if (mEffect.contains(TeyvatFoodType.IncreaseATK_CRIT_DMG)) tooltip.add(new TranslatableText("effect.minecraft.strength").formatted(Formatting.ITALIC, Formatting.GRAY));
        if (mEffect.contains(TeyvatFoodType.IncreaseDEF)) tooltip.add(new TranslatableText("effect.minecraft.resistance").formatted(Formatting.ITALIC, Formatting.GRAY));
        if (mEffect.contains(TeyvatFoodType.IncreaseShieldStrength)) tooltip.add(new TranslatableText("effect.minecraft.absorption").formatted(Formatting.ITALIC, Formatting.GRAY));
    }

}
