package client.gui;

import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.lwjgl.opengl.GL11;

import client.renderer.GlState;
import client.renderer.texture.TextureUtil;
import client.util.FileUtils;
import common.log.Log;

public class Font {
	public static final FontChar[] SIZES = new FontChar[256];
	
	public static int XGLYPH = 12;
	public static int YGLYPH = 14;

	private static int texture;
	
	public static void bindTexture() {
		GlState.bindTexture(texture);
	}

	private static int stride(int width, int height, int x, int y, int c) {
		return ((c & 15) * width + x) + (((c >> 4) & 15) * height + y) * (width << 4); // << 2;
	}
	
	private static byte specialChar(int width, int ch) {
		return (byte)((ch == Log.CHR_UNK) ? width : ((ch == Log.CHR_SPC) ? (width / 6) : 127));
	}
	
	private static void calculate(int[] data, FontChar[] glyphs, int width, int height, int page) {
		int off;
		for(int z = 0; z < 256; z++) {
			byte s, t, u, v;
			if((u = specialChar(width, (page << 8) + z)) != 127) {
				s = t = 0;
				v = (byte)(((int)u) * height / width);
				if(((page << 8) + z) != Log.CHR_UNK) {
					for(int x = 0; x < width; x++) {
						for(int y = 0; y < height; y++) {
							off = stride(width, height, x, y, z);
							data[off/*+3*/] = 0;
						}
					}
				}
				glyphs[z] = new FontChar(s, t, u, v);
				continue;
			}
			s = t = 127;
			u = v = 0;
			for(int x = 0; x < width; x++) {
				for(int y = 0; y < height; y++) {
					off = stride(width, height, x, y, z);
					if((data[off/*+3*/] & 0xff000000) != 0) {
						s = x < s ? (byte)x : s;
						t = y < t ? (byte)y : t;
						u = x > u ? (byte)x : u;
						v = y > v ? (byte)y : v;
					}
				}
			}
			if(s == 127 && t == 127 && u == 0 && v == 0) {
				s = t = 0;
			}
			else {
				u += 1;
				v += 1;
			}
			glyphs[z] = new FontChar(s, t, u, v);
		}
	}
	
	public static void load(boolean tiny) {
		XGLYPH = tiny ? 6 : 12;
		YGLYPH = tiny ? 7 : 14;
        BufferedImage img = null;
		try {
			img = TextureUtil.readImage(FileUtils.getResource("textures/font" + (tiny ? "_tiny" : "") + ".png"));
		}
		catch(FileNotFoundException e) {
			Log.IO.error("Konnte Font-Textur nicht laden: Datei nicht vorhanden");
		}
		catch(IOException e) {
			Log.IO.error(e, "Konnte Font-Textur nicht laden");
		}
		if(img != null && (img.getWidth() != XGLYPH * 16 || img.getHeight() != YGLYPH * 16)) {
    		Log.IO.error("Konnte Font-Textur nicht laden: Größe ist nicht %dx%d", XGLYPH * 16, YGLYPH * 16);
			img = null;
		}
		if(img == null)
			throw new IllegalStateException("Konnte erforderliche Schriftart nicht laden");
        int[] data = new int[XGLYPH * 16 * YGLYPH * 16];
        img.getRGB(0, 0, XGLYPH * 16, YGLYPH * 16, data, 0, XGLYPH * 16);
		calculate(data, SIZES, XGLYPH, YGLYPH, 0);
		texture = GL11.glGenTextures();
		TextureUtil.uploadImage(texture, img);
		Log.RENDER.debug("Font-Textur wurde mit ID #%d geladen", texture);
	}
	
	public static void unload() {
		if(texture != 0) {
			GL11.glDeleteTextures(texture);
			Log.RENDER.debug("Font-Textur mit ID #%d wurde gelöscht", texture);
			texture = 0;
		}
	}
}
