package xyz.illuc.neuralgiacore.items;

import net.adinvas.prototype_pain.PlayerHealthProvider;
import net.adinvas.prototype_pain.client.MinigameOpener;
import net.adinvas.prototype_pain.item.IAllowInMedicbags;
import net.adinvas.prototype_pain.item.IMedicalMinigameUsable;
import net.adinvas.prototype_pain.item.INbtDrivenDurability;
import net.adinvas.prototype_pain.limbs.Limb;
import net.adinvas.prototype_pain.limbs.PlayerHealthData;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;
import net.minecraft.util.CommonColors;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.DistExecutor;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public abstract class NeuralgiaBandage extends Item implements IMedicalMinigameUsable, IAllowInMedicbags, INbtDrivenDurability {
    public NeuralgiaBandage() {
        super(new Properties().stacksTo(1));
    }


    @Override
    public void appendHoverText(ItemStack pStack, @Nullable Level pLevel, List<Component> pTooltipComponents, TooltipFlag pIsAdvanced) {
        super.appendHoverText(pStack, pLevel, pTooltipComponents, pIsAdvanced);
        pTooltipComponents.add(Component.literal((int)(getNbtDurabilityRatio(pStack)*100)+"% left").withStyle(Style.EMPTY.withColor(CommonColors.GRAY)));

        //pTooltipComponents.add(Component.translatable("Makeshift wraps made from straw and plants, will almost defenitely get infected").withStyle(ChatFormatting.GRAY));
    }

    @Override
    public void openMinigameScreen(Player target, ItemStack stack, @Nullable Limb limb, InteractionHand hand) {
        DistExecutor.unsafeRunWhenOn(Dist.CLIENT,() -> () ->{
            MinigameOpener.OpenBandageMinigame(target,stack,limb,hand);
        });
    }

    @Override
    public void openMinigameBagScreen(Player target, ItemStack stack, ItemStack bagStack,int slot, @Nullable Limb limb, InteractionHand hand) {
        DistExecutor.unsafeRunWhenOn(Dist.CLIENT,() -> () ->{
            MinigameOpener.OpenBandageMinigame(target,stack,bagStack,slot,limb,hand);
        });
    }
    @Override
    public void useMinigameAction(float scalableAmount, Player target, @Nullable Limb limb) {
        target.getCapability(PlayerHealthProvider.PLAYER_HEALTH_DATA).ifPresent(h->{
            healActions(scalableAmount, h, limb, target);
        });
    }

    public void healActions(float scalableAmount, PlayerHealthData target, Limb limb, Player player){
        target.addDelayedChange(((0.003f*scalableAmount)/20f)/60f,200,limb);
        float painRed = Math.max(0f, 1f - 0.02f * scalableAmount);
        target.setLimbPain(limb,target.getLimbPain(limb)*painRed);
        target.setLimbSkinHealth(limb,target.getLimbSkinHealth(limb)+0.12f*scalableAmount);
        float fractRed = Math.max(0f, 1f - 0.0002f * scalableAmount);
        target.setLimbFracture(limb,target.getLimbFracture(limb)*fractRed);
        target.setLimbDislocation(limb,target.getLimbDislocated(limb)*fractRed);
    }

    @Override
    public Component getName(ItemStack pStack) {
        Component finalcomp = super.getName(pStack);
        // finalcomp = Component.empty().append(finalcomp)

        return finalcomp;
    }

    @Override
    public void onCraftedBy(ItemStack pStack, Level pLevel, Player pPlayer) {
        setupDefaults(pStack);
        super.onCraftedBy(pStack, pLevel, pPlayer);
    }


}