package com.vanaf3.overlay;

import com.vanaf3.config.VanafConfig;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.MathHelper;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraftforge.client.event.RenderGameOverlayEvent;

public class HUDRenderer {

    private Minecraft mc = Minecraft.getMinecraft();

    @SubscribeEvent
    public void onRenderOverlay(RenderGameOverlayEvent.Pre event) {
        // Annuler le rendu du debug vanilla (F3)
        if (event.type == RenderGameOverlayEvent.ElementType.DEBUG) {
            if (mc.gameSettings.showDebugInfo) {
                event.setCanceled(true);
            }
        }
    }

    @SubscribeEvent
    public void onRenderText(RenderGameOverlayEvent.Text event) {
        // SEULEMENT si F3 est actif
        if (!mc.gameSettings.showDebugInfo) {
            return;
        }

        FontRenderer fontRenderer = mc.fontRenderer;
        EntityPlayer player = mc.thePlayer;

        if (player == null) return;

        int leftX = 2;
        int rightX = event.resolution.getScaledWidth() - 2;
        int y = 2;

        // GAUCHE - Texte custom en haut
        String versionText = "1.7.10 " + VanafConfig.customText;
        fontRenderer.drawStringWithShadow(versionText, leftX, y, 0xFFFFFF);
        y += 10;

        // Ligne vide
        y += 10;

        // Coordonnees
        if (VanafConfig.showCoords) {
            int x = MathHelper.floor_double(player.posX);
            int playerY = MathHelper.floor_double(player.posY);
            int z = MathHelper.floor_double(player.posZ);

            fontRenderer.drawStringWithShadow("X: " + x, leftX, y, 0xFFFFFF);
            y += 10;
            fontRenderer.drawStringWithShadow("Y: " + playerY, leftX, y, 0xFFFFFF);
            y += 10;
            fontRenderer.drawStringWithShadow("Z: " + z, leftX, y, 0xFFFFFF);
            y += 10;
        }

        // Direction
        if (VanafConfig.showDirection) {
            String direction = getDirection(player);
            fontRenderer.drawStringWithShadow("Direction: " + direction, leftX, y, 0xFFFFFF);
            y += 10;
        }

        // Ligne vide
        y += 10;

        // Biome
        if (VanafConfig.showBiome) {
            BiomeGenBase biome = mc.theWorld.getBiomeGenForCoords(
                    MathHelper.floor_double(player.posX),
                    MathHelper.floor_double(player.posZ)
            );
            fontRenderer.drawStringWithShadow("Biome: " + biome.biomeName, leftX, y, 0xFFFFFF);
        }

        // DROITE - RAM
        if (VanafConfig.showRAM) {
            String ramText = getRAMInfo();
            int ramWidth = fontRenderer.getStringWidth(ramText);
            fontRenderer.drawStringWithShadow(ramText, rightX - ramWidth, 2, 0xFFFFFF);
        }
    }

    private String getDirection(EntityPlayer player) {
        int heading = MathHelper.floor_double((double)(player.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;

        switch (heading) {
            case 0: return "Sud";
            case 1: return "Ouest";
            case 2: return "Nord";
            case 3: return "Est";
            default: return "?";
        }
    }

    private String getRAMInfo() {
        Runtime runtime = Runtime.getRuntime();
        long maxMemory = runtime.maxMemory();
        long totalMemory = runtime.totalMemory();
        long freeMemory = runtime.freeMemory();
        long usedMemory = totalMemory - freeMemory;

        long usedMB = usedMemory / 1024L / 1024L;
        long allocatedMB = totalMemory / 1024L / 1024L;
        long maxMB = maxMemory / 1024L / 1024L;

        return String.format("RAM Used: %dMB - %dMB / %dMB", usedMB, allocatedMB, maxMB);
    }
}