package me.zacharias.compadability.modules;

import com.momosoftworks.coldsweat.core.init.ModAttributes;
import mekanism.api.gear.*;
import mekanism.common.util.MekanismUtils;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.EquipmentSlotGroup;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import com.momosoftworks.coldsweat.api.util.Temperature;
import net.neoforged.neoforge.event.ItemAttributeModifierEvent;

import java.util.function.Consumer;

import static me.zacharias.compadability.Compadability.MODID;

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

    @Override
    public void addHUDElements(IModule<ClimateControlUnit> module, IModuleContainer moduleContainer, ItemStack stack, Player player, Consumer<IHUDElement> hudElementAdder) {
        hudElementAdder.accept(IModuleHelper.INSTANCE.hudElement(icon, Component.literal("Temp: "+"%.1f".formatted(GetTempC(player, Temperature.Trait.WORLD))), IHUDElement.HUDColor.REGULAR));
    }

    @Override
    public void adjustAttributes(IModule<ClimateControlUnit> module, ItemAttributeModifierEvent event) {
        event.addModifier(ModAttributes.HEAT_DAMPENING.getDelegate(), new AttributeModifier(ResourceLocation.fromNamespaceAndPath(MODID, "climate_control_unit_modifier"), 1, AttributeModifier.Operation.ADD_VALUE), EquipmentSlotGroup.ANY);
        event.addModifier(ModAttributes.COLD_DAMPENING.getDelegate(), new AttributeModifier(ResourceLocation.fromNamespaceAndPath(MODID, "climate_control_unit_modifier"), 1, AttributeModifier.Operation.ADD_VALUE), EquipmentSlotGroup.ANY);
    }

    public static double GetTempC(Player player, Temperature.Trait trait) {
        return Temperature.convertIfNeeded(Temperature.get(player, trait), trait, Temperature.Units.C);
    }
}
