package projectzombie.display;

import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glViewport;

import java.nio.ByteBuffer;

import org.lwjgl.opengl.GL33;

import gl_engine.matrix.Matrix4;
import projectzombie.Main;
import projectzombie.entity.player.EntityPlayer;
import projectzombie.settings.SettingQuality;
import projectzombie.util.ColorRange;
import projectzombie.world.chunk.ChunkEventHandler;
import projectzombie.world.layer.layergen.LayerGen;

public class DisplayRender
{
	public static int shadow_fbo;
	public static int shadow_depth;
	public static int shadow_size;
	private static SettingQuality shadow_quality;
	
	public static SettingQuality setShadowQuality(SettingQuality quality)
	{
		int size;
		
		switch(quality)
		{
		case LOWEST:
			size = 1024;
			break;
		case LOW:
			size = 2048;
			break;
		case MEDIUM:
			size = 4096;
			break;
		case HIGH:
			size = 8192;
			break;
		case EXTREME:
			size = 16384;
			break;
		default:
			size = 2048;
			quality = SettingQuality.LOW;
			break;
		}
		
		if(Main.window != null && size > Main.window.texture_max_size) {
			quality = SettingQuality.LOWEST;
			size = 1024;
		}
		
		shadow_size = size;
		shadow_quality = quality;
		
		return quality;
	}
	
	public static SettingQuality getShadowQuality() {
		return shadow_quality;
	}
	
	public static void updateShadowQuality()
	{
		GL33.glBindFramebuffer(GL33.GL_FRAMEBUFFER, shadow_fbo);
		GL33.glDeleteTextures(shadow_depth);
		
		shadow_depth = generateDepthTexture(shadow_size, shadow_size);
	}
	
	private static int generateDepthTexture(int width, int height)
	{
		int depth = GL33.glGenTextures();
		
		GL33.glBindTexture(GL33.GL_TEXTURE_2D, depth);
		
		GL33.glTexImage2D(
				GL33.GL_TEXTURE_2D, 0, GL33.GL_DEPTH_COMPONENT32, width, height,
				0, GL33.GL_DEPTH_COMPONENT, GL33.GL_FLOAT, (ByteBuffer) null);
		
		GL33.glTexParameteri(GL33.GL_TEXTURE_2D, GL33.GL_TEXTURE_MIN_FILTER, GL33.GL_LINEAR);
		GL33.glTexParameteri(GL33.GL_TEXTURE_2D, GL33.GL_TEXTURE_MAG_FILTER, GL33.GL_LINEAR);
		GL33.glTexParameteri(GL33.GL_TEXTURE_2D, GL33.GL_TEXTURE_WRAP_S, GL33.GL_CLAMP_TO_BORDER);
		GL33.glTexParameteri(GL33.GL_TEXTURE_2D, GL33.GL_TEXTURE_WRAP_T, GL33.GL_CLAMP_TO_BORDER);
		GL33.glTexParameterfv(GL33.GL_TEXTURE_2D, GL33.GL_TEXTURE_BORDER_COLOR, new float[] {1, 1, 1, 1});
		GL33.glFramebufferTexture(GL33.GL_FRAMEBUFFER, GL33.GL_DEPTH_ATTACHMENT, depth, 0);
		
		return depth;
	}
	
	public static void init()
	{
		shadow_fbo = GL33.glGenFramebuffers();
		GL33.glBindFramebuffer(GL33.GL_FRAMEBUFFER, shadow_fbo);
		GL33.glDrawBuffer(GL33.GL_NONE);
		GL33.glReadBuffer(GL33.GL_NONE);
		
		shadow_depth = generateDepthTexture(shadow_size, shadow_size);
	}
	
	public static void render(int w, int h)
	{
		// Setup GL and clear the colour
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glViewport(0, 0, w, h);
		
		GL33.glEnable(GL33.GL_DEPTH_TEST);
		GL33.glUniform4f(Main.window.glsl_color, 1, 1, 1, 1);
		GL33.glUniform1i(Main.window.glsl_do_discard_coords, 0);
		
		ColorRange range = Main.world.getLayer().layergen.getLightLevel();
		
		GL33.glUniform3f(Main.window.glsl_day_low, (float)range.min.x, (float)range.min.y, (float)range.min.z);
		GL33.glUniform3f(Main.window.glsl_day_high, (float)range.max.x, (float)range.max.y, (float)range.max.z);
		
		if(Main.menu.doGameRender)
		{
			if(ChunkEventHandler.loaded)
			{
				EntityPlayer player = Main.player;
				Camera camera = new Camera(w, h);
				Camera.camera = camera;
				
				Matrix4 rotated = Matrix4.rotate(-camera.angle, 0, 1, 0);
				Matrix4 billboard = Matrix4.multiply(Matrix4.rotate(-45, 1, 0, 0), rotated);
				
				Main.window.environmentRenderer.use();
				GL33.glUniformMatrix4fv(Main.window.glsl_camera, true, Camera.camera.matrix.getArray());
				GL33.glUniformMatrix4fv(Main.window.glsl_billboard, true, billboard.getArray());
				GL33.glUniformMatrix4fv(Main.window.glsl_rotated, true, rotated.getArray());
				GL33.glUniformMatrix4fv(Main.window.glsl_projection_sun, true, Camera.camera.projection_sun.getArray());
				GL33.glUniform1i(Main.window.glsl_time, (int)((System.currentTimeMillis() / 10) % 1000));
				GL33.glUniform1i(Main.window.glsl_do_lighting, 1);
				
				Main.world.markPoolDirty();
				
				LayerGen layergen = Main.world.getLayer().layergen;
				//double sunPosition = MathHelpers.mod(layergen.getSunPosition(), 360);
				
				if(layergen.hasSun() /*&& sunPosition > 0 && sunPosition < 180*/ && shadow_size != 1)
				{
					// Set up the suns frame buffer
					GL33.glBindFramebuffer(GL33.GL_FRAMEBUFFER, shadow_fbo);
					glClear(GL_DEPTH_BUFFER_BIT);
					
					// Set up the suns perspective
					GL33.glUniformMatrix4fv(Main.window.glsl_projection, true,
							Camera.camera.projection_sun.getArray());
					GL33.glUniform1i(Main.window.glsl_mode, 1);
					
					// Render from the suns perspective
					glViewport(0, 0, shadow_size, shadow_size);
					Main.world.render(camera);
					
					// Set back to the players frame buffer
					GL33.glBindFramebuffer(GL33.GL_FRAMEBUFFER, 0);
					glViewport(0, 0, w, h);
				}
				
				// Set to the players perspective and render
				GL33.glUniformMatrix4fv(Main.window.glsl_projection, true,
						Camera.camera.projection.getArray());
				GL33.glUniform1i(Main.window.glsl_mode, 0);
				
				Main.world.render(camera);
				
				player.chunk = Main.world.getLayer().getChunk(player.getPos().xz());
				
				GL33.glUniform1i(Main.window.glsl_do_lighting, 0);
			}
		}
		
		// Render the user interface
		DisplayRenderUI.render();
	}
}
