package client.gui;

import client.Client;
import client.gui.element.MultiLabel;
import client.gui.element.NavButton;
import common.Version;
import common.util.Color;
import common.util.Util;

public class GuiInfo extends Gui {
	private static final String VER = 
	Color.GREEN + "" + Util.repeatString("\u009a", Version.RELEASE.getId()) + Color.DARK_VIOLET + "|-| " + Color.VIOLET + Client.VERSION + Color.DARK_VIOLET + " |-|" + 
	Color.GREEN + Util.repeatString("\u009a", Version.RELEASE.getId());

	private static final String INFO = "Ein Spiel zur Simulation, zum Testen, für Rollenspiele, Mehrspieler und vieles mehr." + "\n" + 
			"Optimiert für Geschwindigkeit, Stabilität und\u007f\u007f [Speicherzugriffsfehler]";
	private static final String HACKED = "Ein weiterer Release von WAAAAAAAAAAAAGHDRIVE!!!1!!!ONEoneOnetyone!1!!!" + "\n" + 
			"Update 0.2 - Läuft jetzt auch mit nur 512KB Fast-RAM!";

	private static final String[] LIBRARIES = {
		"LWJGL 3.3.6",
		"LWJGL-GLFW 3.3.6",
		"LWJGL-OpenGL 3.3.6"
	};
	private static final String[] CODE = {
		"Albert Pham - WorldEdit (Snippets)",
		"Joonas Vali - NameGenerator",
		"LWJGL 2.9.4-nightly-20150209 - Project, Vector*, Matrix*",
		"Guava 17.0 - collect, ChunkBuilder, Predicates",
		"Netty 4.0.23-Final - net.*",
		"JOrbis 20101023 (JCraft) - jogg, jorbis, CodecJOrbis"
	};

	public static final GuiInfo INSTANCE = new GuiInfo("Über dieses Programm", getFormat(false));
	public static final GuiInfo HAX = new GuiInfo("Üb3r d1es3n Cr4ck", getFormat(true));

	private final String header;
	private final String info;
	
	private static String getFormat(boolean hax) {
		return getInfo(hax) + "\n\n" + getCredits(hax) + "\n\n" + getLibraries(hax) + "\n\n" + getCode(hax) + "\n\n" + getColors();
	}
	
	private static String getHeader(boolean hax, String normal, String hacked) {
		return (hax ? Color.RED : Color.YELLOW) + (hax ? hacked : normal) + "\n" + 
				(hax ? Color.CYAN : Color.WHITE) + "==========================+==========================";
	}

	private static void addLines(StringBuilder sb, boolean hax, String alternate, String category, String... authors) {
		sb.append("\n" + (hax ? Color.BLUE : Color.GRAY) 
				+ (hax ? alternate : category) + "\n    ");
		for(int z = 0; z < authors.length; z++) {
			if(z > 0)
				sb.append((hax ? Color.VIOLET : Color.GRAY) + ", ");
			sb.append((hax ? Color.DARK_VIOLET : Color.WHITE) + authors[z]);
		}
	}
	
	private static String getInfo(boolean hax) {
		return getHeader(hax, VER, VER) + "\n" + (hax ? (Color.VIOLET + HACKED) : (Color.LIGHT_GRAY + INFO));
	}

	private static String getLibraries(boolean hax) {
		StringBuilder sb = new StringBuilder(getHeader(hax, "Verwendete Programmbibliotheken", "U$3d 3xpl0its"));
		for(String lib : LIBRARIES) {
			sb.append("\n" + Color.NEON + lib);
		}
		return sb.toString();
	}

	private static String getCode(boolean hax) {
		StringBuilder sb = new StringBuilder(getHeader(hax, "Zusätzlicher Quellcode", "M0ar 3xpl01ts"));
		for(String lib : CODE) {
			sb.append("\n" + Color.NEON + lib);
		}
		return sb.toString();
	}
	
	private static String getColors() {
		StringBuilder sb = new StringBuilder(Color.RESET + "#D ");
		int num = 0;
		for(Color color : Color.values()) {
			if(num == Color.values().length / 2)
				sb.append("\n" + Color.COMMAND + "#d ");
			sb.append(color + "#" + color.colorId + ' ');
			num++;
		}
		return sb.toString();
	}

	private static String getCredits(boolean hax) {
		StringBuilder sb = new StringBuilder(getHeader(hax, "Mitwirkende dieses Programms", "Das Team -- TCQ"));

		addLines(sb, hax, "Die dunklen Herrscher", "Quellcode, Design, Grafiken, Refactoring und Code-Cleanup", 
				Color.CYAN + "Sen der \"kleine\" Dämon", 
				Color.RED + "Shen, Herrscher des Schattenlandes");
		
		return sb.toString();
	}
	
	public GuiInfo(String header, String info) {
		this.header = header;
		this.info = info;
	}
	
	public void init(int width, int height) {
		this.add(new MultiLabel(10, 10, width - 20, height - 44, this.info, false));
		this.add(new NavButton((width - 280) / 2, height - 20, 280, 0, GuiMenu.INSTANCE, "Zurück"));
	}
	
	public String getTitle() {
		return this.header;
	}
}
