package me.zacharias.compadability.modules;

import dev.ghen.thirst.api.ThirstHelper;
import dev.ghen.thirst.foundation.common.capability.IThirst;
import dev.ghen.thirst.foundation.common.capability.ModAttachment;
import me.zacharias.compadability.Config;
import mekanism.api.gear.*;
import mekanism.api.math.MathUtils;
import mekanism.common.capabilities.Capabilities;
import mekanism.common.config.MekanismConfig;
import mekanism.common.registries.MekanismFluids;
import mekanism.common.util.MekanismUtils;
import mekanism.common.util.StorageUtils;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.material.Fluids;
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.fluids.capability.IFluidHandler;
import net.neoforged.neoforge.fluids.capability.IFluidHandlerItem;

import java.util.function.Consumer;

import static me.zacharias.compadability.utils.Fluids.WATER_STACK;

public class WaterPurifiedDrinkingUnit implements ICustomModule<WaterPurifiedDrinkingUnit> {
    private static final ResourceLocation icon = MekanismUtils.getResource(MekanismUtils.ResourceType.GUI_HUD, "nutritional_injection_unit.png");

    @Override
    public void tickServer(IModule<WaterPurifiedDrinkingUnit> module, IModuleContainer moduleContainer, ItemStack stack, Player player) {
        long usage = MekanismConfig.gear.mekaSuitEnergyUsageNutritionalInjection.get();
        IThirst thirstData = (IThirst)player.getData(ModAttachment.PLAYER_THIRST);
        if (MekanismUtils.isPlayingMode(player) && thirstData.getThirst() < 20) {
            //Check if we can use a single iteration of it
            IFluidHandlerItem handler = Capabilities.FLUID.getCapability(stack);
            if (handler != null) {
                int contained = StorageUtils.getContainedFluid(handler, WATER_STACK).getAmount();
                int needed = Math.min(20 - thirstData.getThirst(), contained / Config.MbPerThirst);
                int toFeed = Math.min(MathUtils.clampToInt(module.getContainerEnergy(stack) / usage), needed);
                if (toFeed > 0) {
                    module.useEnergy(player, stack, usage * toFeed / Config.MbPerThirst);
                    handler.drain(new FluidStack(Fluids.WATER, toFeed / Config.MbPerThirst), IFluidHandler.FluidAction.EXECUTE);
                    thirstData.setThirst(thirstData.getThirst()+needed);
                    thirstData.updateThirstData(player);
                }
            }
        }
    }

    @Override
    public void addHUDElements(IModule<WaterPurifiedDrinkingUnit> module, IModuleContainer moduleContainer, ItemStack stack, Player player, Consumer<IHUDElement> hudElementAdder) {
        if (module.isEnabled()) {
            IFluidHandlerItem handler = Capabilities.FLUID.getCapability(stack);
            double ratio = 0;
            if (handler != null) {
                handler.drain(new FluidStack(Fluids.WATER, Config.MaxWaterUnitContent), IFluidHandler.FluidAction.SIMULATE);
                FluidStack stored = StorageUtils.getContainedFluid(handler, WATER_STACK);
                ratio = StorageUtils.getRatio(stored.getAmount(), Config.MaxWaterUnitContent);
            }
            hudElementAdder.accept(IModuleHelper.INSTANCE.hudElementPercent(icon, ratio));
        }
    }
}
