package net.silentchaos512.scalinghealth.client;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.client.GuiIngameForge;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.silentchaos512.scalinghealth.ScalingHealth;
import net.silentchaos512.scalinghealth.config.ConfigScalingHealth;
import net.silentchaos512.scalinghealth.utils.SHPlayerDataHandler;
import net.silentchaos512.scalinghealth.utils.SHPlayerDataHandler.PlayerData;

public class DifficultyDisplayHandler extends Gui {

  public static final ResourceLocation TEXTURE = new ResourceLocation(ScalingHealth.MOD_ID_LOWER,
      "textures/gui/hud.png");

  int lastDifficultyDisplayed = -100;
  int lastAreaDifficultyDisplayed = -100;
  int lastUpdateTime = Integer.MIN_VALUE;
  int currentTime = 0;

  @SubscribeEvent
  public void onRenderOverlay(RenderGameOverlayEvent.Pre event) {

    // TODO: Configs?
    if (event.getType() != ElementType.TEXT || ConfigScalingHealth.DIFFICULTY_MAX <= 0
        || !ConfigScalingHealth.RENDER_DIFFICULTY_METER)
      return;

    Minecraft mc = Minecraft.getMinecraft();
    EntityPlayer player = mc.thePlayer;

    int width = event.getResolution().getScaledWidth();
    int height = event.getResolution().getScaledHeight();

    PlayerData data = player != null ? SHPlayerDataHandler.get(player) : null;
    if (data == null)
      return;

    int difficulty = (int) data.getDifficulty();
    int areaDifficulty = MathHelper.clamp_int((int) ConfigScalingHealth.AREA_DIFFICULTY_MODE
        .getAreaDifficulty(player.worldObj, player.getPosition()), 0,
        (int) ConfigScalingHealth.DIFFICULTY_MAX);
    int timeSinceLastUpdate = ClientTickHandler.ticksInGame - lastUpdateTime;

    if (difficulty != lastDifficultyDisplayed) {
      lastDifficultyDisplayed = difficulty;
      lastUpdateTime = ClientTickHandler.ticksInGame;
    }
    if (areaDifficulty < lastAreaDifficultyDisplayed - 10
        || areaDifficulty > lastAreaDifficultyDisplayed + 10 && timeSinceLastUpdate > 1200) {
      lastAreaDifficultyDisplayed = areaDifficulty;
      lastUpdateTime = ClientTickHandler.ticksInGame;
    }

    currentTime = ClientTickHandler.ticksInGame;
    if (currentTime - lastUpdateTime < 160) { // TODO: Config?
      GlStateManager.enableBlend();

      mc.renderEngine.bindTexture(TEXTURE);

      GlStateManager.pushMatrix();
      // GlStateManager.scale(1f, 0.5f, 1f);

      int posX = width / 2 - 32;
      int posY = height - GuiIngameForge.left_height - 14;
      // Frame
      drawTexturedModalRect(posX, posY, 192, 0, 64, 12, 0xFFFFFF);
      // Area Difficulty
      int barLength = (int) (60 * areaDifficulty / ConfigScalingHealth.DIFFICULTY_MAX);
      drawTexturedModalRect(posX + 2, posY + 2, 194, 14, barLength, 6, 0xFFFFFF);
      // Difficulty
      barLength = (int) (60 * difficulty / ConfigScalingHealth.DIFFICULTY_MAX);
      drawTexturedModalRect(posX + 2, posY + 8, 194, 20, barLength, 2, 0xFFFFFF);

      GlStateManager.popMatrix();
      GlStateManager.disableBlend();
    }
  }

  protected void drawTexturedModalRect(int x, int y, int textureX, int textureY, int width,
      int height, int color) {

    float r = ((color >> 16) & 255) / 255f;
    float g = ((color >> 8) & 255) / 255f;
    float b = (color & 255) / 255f;
    GlStateManager.color(r, g, b);
    drawTexturedModalRect(x, y, textureX, textureY, width, height);
    GlStateManager.color(1f, 1f, 1f);
  }
}

@@ -2555,20 +2555,65 @@
 me;%0A    
-if (
+boolean showAlways = true;%0A    if (showAlways %7C%7C 
 currentT
@@ -2838,16 +2838,22 @@
 t posX =
+ 5; //
  width /
@@ -2886,16 +2886,32 @@
 height -
+ 30; // height -
  GuiInga
@@ -2936,16 +2936,17 @@
 t - 14;%0A
+%0A
       //
@@ -3011,32 +3011,33 @@
  12, 0xFFFFFF);%0A
+%0A
       // Area Di
@@ -3215,16 +3215,17 @@
 FFFFF);%0A
+%0A
       //
@@ -3308,32 +3308,32 @@
 IFFICULTY_MAX);%0A
-
       drawTextur
@@ -3395,24 +3395,319 @@
 0xFFFFFF);%0A%0A
+      // Text%0A      float textScale = 0.6f;%0A      GlStateManager.pushMatrix();%0A      GlStateManager.scale(textScale, textScale, 1.0f);%0A      mc.fontRendererObj.drawStringWithShadow(%22DIFFICULTY%22, posX / textScale + 4,%0A          posY / textScale - 9, 0xFFFFFF);%0A      GlStateManager.popMatrix();%0A%0A
       GlStat

Move and label difficulty meter