package com.haxademic.core.debug;

import java.util.LinkedHashMap;
import java.util.Map;

import com.haxademic.core.app.P;
import com.haxademic.core.draw.util.DrawUtil;
import com.haxademic.core.text.StringFormatter;

import processing.core.PApplet;
import processing.core.PFont;

public class DebugView {
	
	protected PApplet p;
	protected PFont debugFont;	
	protected LinkedHashMap<String, String> debugLines;
	protected LinkedHashMap<String, String> helpLines;
	protected int fontSize = 14;

	public DebugView( PApplet p ) {
		this.p = p;
		createFont();
		debugLines = new LinkedHashMap<String, String>();
		helpLines = new LinkedHashMap<String, String>();
		updateAppInfo();
	}
	
	protected void createFont() {
		p.textMode( P.SCREEN );
		debugFont = p.createFont("Arial", fontSize);
	}
	
	public void addValue(String key, String val) {
		debugLines.put(key, val);
	}
	
	public void addValue(String key, float val) {
		debugLines.put(key, ""+val);
	}
	
	public void addValue(String key, int val) {
		debugLines.put(key, ""+val);
	}
	
	public void addValue(String key, boolean val) {
		String bool = (val == true) ? "true" : "false";
		debugLines.put(key, ""+bool);
	}
	
	public void addHelpLine(String key, String val) {
		helpLines.put(key, val);
	}
	
	protected void updateAppInfo() {
		debugLines.put("RUN TIME", "");
		debugLines.put("Frame", ""+p.frameCount);
		debugLines.put("Time", StringFormatter.timeFromSeconds(p.millis() / 1000, true));
		debugLines.put("", "");
		debugLines.put("PERFORMANCE", "");
		debugLines.put("FPS", ""+P.round(p.frameRate));
		debugLines.put("Memory Allocated", StringFormatter.formattedInteger(DebugUtil.memoryAllocated()));
		debugLines.put("Memory Free", StringFormatter.formattedInteger(DebugUtil.memoryFree()));
		debugLines.put("Memory Max", StringFormatter.formattedInteger(DebugUtil.memoryMax()));
		debugLines.put("", "");
		debugLines.put("\nCUSTOM", "");
	}
	
	public void updateInputs() {
		addValue("mouseX", p.mouseX);
		addValue("mouseY", p.mouseY);
		addValue("key", p.key);
		addValue("keyCode", p.keyCode);
		addValue("keyPressed", p.keyPressed);
	}
	
	protected String stringFromHashMap(LinkedHashMap<String, String> hashMap) {
		// build string by iterating over LinkedHashMap
		String string = "";
		for (Map.Entry<String, String> item : hashMap.entrySet()) {
		    String key = item.getKey();
		    String value = item.getValue();
		    if(value.length() > 0) 	string += key + ": " + value + "\n";
		    else 					string += key + "\n";
		}
		return string;
	}
	
	public void draw() {
		// update core app stats
		updateAppInfo();
		
		// set up flat drawing
		DrawUtil.setDrawCorner(p);
		DrawUtil.setDrawFlat2d(p, true);

		// draw debug text block
		String debugStr = stringFromHashMap(debugLines);
		
		p.textFont( debugFont );
		p.textAlign(P.LEFT, P.TOP);
		p.textSize(fontSize);
		float textW = p.textWidth(debugStr) + 20;
		p.noStroke();
		p.fill(0,225);
		p.rect(0, 0, textW + 20, p.height);
		p.fill(255);
		p.text(debugStr, 10, 10, textW, p.height - 20);
		
		// draw help lines
		String helpStr = stringFromHashMap(helpLines);

		p.textAlign(P.LEFT, P.TOP);
		p.textSize(fontSize);
		textW = p.textWidth(helpStr) + 20;
		p.noStroke();
		p.fill(0,225);
		p.rect(p.width - textW - 10, 0, textW + 20, p.height);
		p.fill(255);
		p.text(helpStr, p.width - textW, 10, textW, p.height - 20);


		DrawUtil.setDrawFlat2d(p, false);
	}
}

@@ -3046,16 +3046,54 @@
 p lines%0A
+%09%09if(helpLines.isEmpty() == false) %7B%0A%09
 %09%09String
@@ -3133,17 +3133,19 @@
 Lines);%0A
-%0A
+%09%0A%09
 %09%09p.text
@@ -3158,32 +3158,33 @@
 P.LEFT, P.TOP);%0A
+%09
 %09%09p.textSize(fon
@@ -3193,16 +3193,17 @@
 ize);%0A%09%09
+%09
 textW = 
@@ -3223,32 +3223,33 @@
 elpStr) + 20;%0A%09%09
+%09
 p.noStroke();%0A%09%09
@@ -3238,32 +3238,33 @@
 %09%09p.noStroke();%0A
+%09
 %09%09p.fill(0,225);
@@ -3262,24 +3262,25 @@
 l(0,225);%0A%09%09
+%09
 p.rect(p.wid
@@ -3316,32 +3316,33 @@
 0, p.height);%0A%09%09
+%09
 p.fill(255);%0A%09%09p
@@ -3330,32 +3330,33 @@
 %09%09%09p.fill(255);%0A
+%09
 %09%09p.text(helpStr
@@ -3401,16 +3401,19 @@
  - 20);%0A
+%09%09%7D
 %0A%0A%09%09Draw

Don't show info string if empty