package speiger.src.coreengine.rendering.guiOld.components.tree;

import java.util.Map;
import java.util.UUID;

import speiger.src.coreengine.math.color.ColorUtils;
import speiger.src.coreengine.rendering.guiOld.GuiBase;
import speiger.src.coreengine.rendering.guiOld.GuiComponent;
import speiger.src.coreengine.rendering.guiOld.components.TextComponent;
import speiger.src.coreengine.rendering.guiOld.components.window.debug.PieProfilerWindow;
import speiger.src.coreengine.rendering.guiOld.helper.Align;
import speiger.src.coreengine.rendering.inputOld.Keyboard;
import speiger.src.coreengine.utils.profiler.IProfiler.IProfilerEntry;

public class ProfilerTreeEntry extends BaseTreeEntry
{
	TextComponent text = new TextComponent().limit(false).align(Align.LEFT_TOP, Align.LEFT_TOP).special(true).setTextScale(0.4F).set(0F, 1F).cast();
	IProfilerEntry entry;
	
	public ProfilerTreeEntry(IProfilerEntry entry)
	{
		this.entry = entry;
	}
	
	@Override
	public float getWidth()
	{
		return text.getMetadata().getMaxWidth();
	}
	
	@Override
	public void init(GuiComponent comp, GuiBase owner)
	{
		super.init(comp, owner);
		text.setOwner(owner);
	}
	
	@Override
	public void updateState(GuiComponent comp, float scale)
	{
		text.setScale(scale);
	}
	
	@Override
	public void onClosed()
	{
		text.onClosed();
	}
	
	@Override
	public void onFixedUpdate()
	{
		if(text.length() > 0 && (Keyboard.isAltDown() || text.getGui().getGlobalClock() % 2 != 0))
		{
			return;
		}
		StringBuilder builder = new StringBuilder();
		builder.append(entry.getName()).append(": ");
		builder.append(getColor(entry.getMinTime())).append(getTime(entry.getMinTime())).append("§<reset>").append(" / ");
		builder.append(getColor(entry.getNanoTime())).append(getTime(entry.getNanoTime())).append("§<reset>").append(" / ");
		builder.append(getColor(entry.getMaxTime())).append(getTime(entry.getMaxTime())).append("§<reset>");
		text.setText(builder.toString());
	}
	
	@Override
	public void onRender(GuiComponent comp, boolean enabled, int mouseX, int mouseY, float particalTicks)
	{
		text.setEnabled(enabled).render(mouseX, mouseY, particalTicks);
	}

	@Override
	public void collectTooltips(GuiComponent comp, int mouseX, int mouseY, float particalTicks, Map<UUID, GuiComponent> collector)
	{
		
	}
	
	@Override
	public int hashCode()
	{
		return entry.getPathName().hashCode();
	}
	
	@Override
	public boolean equals(Object obj)
	{
		if(obj instanceof ProfilerTreeEntry)
		{
			return ((ProfilerTreeEntry)obj).entry == entry;
		}
		return false;
	}
	
	public String getTime(long time)
	{
		if(time >= 2000000L)
		{
			return PieProfilerWindow.PERCENT_FORMAT.format(time / 1000000.0D) + "ms";
		}
		if(time >= 2000L)
		{
			return PieProfilerWindow.PERCENT_FORMAT.format(time / 1000.0D) + "µs";
		}
		return PieProfilerWindow.PERCENT_FORMAT.format(time) + "ns";
	}
	
	public String getColor(long time)
	{
		time /= 1000000L;
		if(time < 1)
		{
			return "§<color="+ColorUtils.LIGHT_BLUE+">";
		}
		if(time >= 12)
		{
			return "§<color="+ColorUtils.RED+">";
		}
		if(time >= 5)
		{
			return "§<color="+ColorUtils.YELLOW+">";
		}
		return "§<color="+ColorUtils.GREEN+">";
	}
	
	@Override
	public String toString()
	{
		return entry.getPathName();
	}
}
