// Copyright (c) 2014, Christopher "blay09" Baker
// All rights reserved.

package net.blay09.mods.eirairc.client.gui.overlay;

import net.blay09.mods.eirairc.util.Globals;
import net.blay09.mods.eirairc.util.NotificationType;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;

public class OverlayNotification extends Gui {

	private static final ResourceLocation backgroundTextures = new ResourceLocation("textures/gui/achievement/achievement_background.png");
	private static final int WIDTH = 160;
	private static final int HEIGHT = 32;
	private static final float TIME_ROLLIN = 8f;
	private static final float TIME_ROLLOUT = 200f;
	
    private final Minecraft theGame;
    
    private NotificationType type;
    private String text;
    private float notificationTime;
    private boolean visible;
    
    private int windowWidth;
    private int windowHeight;
    
    public OverlayNotification() {
    	theGame = Minecraft.getMinecraft();
    }
    
    public void showNotification(NotificationType type, String text) {
    	this.type = type;
    	this.text = text;
    	notificationTime = 0f;
    	visible = true;
    }
    
    public void updateGuiScale() {
    	GL11.glViewport(0, 0, this.theGame.displayWidth, this.theGame.displayHeight);
		GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glLoadIdentity();
        windowWidth = this.theGame.displayWidth;
        windowHeight = this.theGame.displayHeight;
        ScaledResolution scaledResolution = new ScaledResolution(this.theGame, this.theGame.displayWidth, this.theGame.displayHeight);
        windowWidth = scaledResolution.getScaledWidth();
        windowHeight = scaledResolution.getScaledHeight();
        GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0.0D, (double) windowWidth, (double) windowHeight, 0.0D, 1000.0D, 3000.0D);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glLoadIdentity();
        GL11.glTranslatef(0.0F, 0.0F, -2000.0F);
    }
    
	public void updateAndRender(float delta) {
		if(!visible) {
			return;
		}
		notificationTime += delta;
		if(notificationTime >= TIME_ROLLOUT) {
			visible = false;
			return;
		}
		updateGuiScale();
		
		int offset;
		if(notificationTime < TIME_ROLLOUT - TIME_ROLLIN) {
			offset = HEIGHT - (int) (HEIGHT * Math.min(1f, notificationTime / TIME_ROLLIN));
		} else {
			offset = HEIGHT * 2 + (int) (HEIGHT * Math.min(1f, (notificationTime - TIME_ROLLOUT - TIME_ROLLIN) / TIME_ROLLIN));
		}
		int x = windowWidth - WIDTH;
		int y = windowHeight - HEIGHT + offset;
		GL11.glEnable(GL11.GL_TEXTURE_2D);
		Minecraft.getMinecraft().getTextureManager().bindTexture(backgroundTextures);
		GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
		GL11.glDisable(GL11.GL_LIGHTING);
		// TODO render icon here
		drawTexturedModalRect(x, y, 96, 202, WIDTH, HEIGHT);
		theGame.fontRendererObj.drawSplitString(text, x + 30, y + 32 / 2 - theGame.fontRendererObj.FONT_HEIGHT * 2 / 2, WIDTH - 36, Globals.TEXT_COLOR);
		GL11.glDisable(GL11.GL_TEXTURE_2D);
		GL11.glEnable(GL11.GL_LIGHTING);
	}
	
}

@@ -2826,45 +2826,8 @@
 et;%0A
-%09%09GL11.glEnable(GL11.GL_TEXTURE_2D);%0A
 %09%09Mi
@@ -2908,110 +2908,315 @@
 ;%0A%09%09
-GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);%0A%09%09GL11.glDisable(GL11.GL_LIGHTING);%0A%09%09// TODO render icon here
+boolean wasTex2DEnabled = GL11.glIsEnabled(GL11.GL_TEXTURE_2D);%0A%09%09if(!wasTex2DEnabled) %7B%0A%09%09%09GL11.glEnable(GL11.GL_TEXTURE_2D);%0A%09%09%7D%0A%09%09boolean wasLightingEnabled = GL11.glIsEnabled(GL11.GL_LIGHTING);%0A%09%09if(wasLightingEnabled) %7B%0A%09%09%09GL11.glDisable(GL11.GL_LIGHTING);%0A%09%09%7D%0A%09%09GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
 %0A%09%09d
@@ -3414,16 +3414,42 @@
 COLOR);%0A
+%09%09if(!wasTex2DEnabled) %7B%0A%09
 %09%09GL11.g
@@ -3478,16 +3478,48 @@
 RE_2D);%0A
+%09%09%7D%0A%09%09if(wasLightingEnabled) %7B%0A%09
 %09%09GL11.g
@@ -3545,15 +3545,19 @@
 HTING);%0A
+%09%09%7D%0A
 %09%7D%0A%09%0A%7D%0A

Fix rendering issues when notifications are showing when a GUI is visible.