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

import java.util.List;
import java.util.UUID;

import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.collections.objects.lists.ObjectList;
import speiger.src.collections.objects.maps.impl.hash.Object2ObjectLinkedOpenHashMap;
import speiger.src.collections.objects.maps.interfaces.Object2ObjectMap;
import speiger.src.collections.objects.utils.maps.Object2ObjectMaps;
import speiger.src.coreengine.math.color.ColorUtils;
import speiger.src.coreengine.rendering.guiOld.GuiComponent;
import speiger.src.coreengine.rendering.guiOld.helper.box.IGuiBox;
import speiger.src.coreengine.rendering.guiOld.helper.box.ParentBox;
import speiger.src.coreengine.rendering.guiOld.helper.constrains.Constrains;
import speiger.src.coreengine.rendering.guiOld.helper.constrains.MenuConstrain;
import speiger.src.coreengine.rendering.guiOld.helper.constrains.ParentConstrain;

public class TooltipPanel extends PanelComponent
{
	ObjectList<UUID> helperList = new ObjectArrayList<>();
	List<GuiComponent> indexedComponents = new ObjectArrayList<>();
	Object2ObjectMap<UUID, GuiComponent> renderedTooltips = new Object2ObjectLinkedOpenHashMap<>();
	int color = ColorUtils.BLACK;
	int borderColor = ColorUtils.DARK_GRAY;
	IGuiBox box = new ParentBox(1.2F);
	
	@Override
	public void init()
	{
		super.init();
		addBox(box);
	}
	
	public void updateTooltips(Object2ObjectMap<UUID, GuiComponent> newTooltips)
	{
		helperList.addAll(renderedTooltips.keySet());
		helperList.removeAll(newTooltips.keySet());
		if(helperList.size() > 0)
		{
			for(int i = 0;i<helperList.size();i++)
			{
				GuiComponent com = renderedTooltips.remove(helperList.get(i));
				removeChild(com);
				indexedComponents.remove(com);
			}
			helperList.clear();
		}
		newTooltips.keySet().removeAll(renderedTooltips.keySet());
		for(Object2ObjectMap.Entry<UUID, GuiComponent> entry : Object2ObjectMaps.fastIterable(newTooltips))
		{
			renderedTooltips.put(entry.getKey(), entry.getValue());
			indexedComponents.add(entry.getValue());
			addChild(entry.getValue(), new Constrains(new ParentConstrain(3.2F), new MenuConstrain<>(indexedComponents, () -> 1F), null, null));
		}
		float[] data = new float[]{
				Float.MAX_VALUE,
				Float.MAX_VALUE,
				Float.MIN_VALUE,
				Float.MIN_VALUE
		};
		for(GuiComponent child : indexedComponents)
		{
			if(child.isVisible()) child.calculateActualBounds(data, false);
		}
		bounds(data[2]-data[0] + 6.4F, data[3]-data[1] + 2.5F);
	}
	
	@Override
	protected boolean renderSelf(int mouseX, int mouseY, float particalTicks)
	{
		getRenderer().drawQuad(getBox(), -0.01F, color).drawQuad(box, 0F, borderColor);
		return true;
	}
	
	public float isOutsideScreen(int mouseX, int width)
	{
		return mouseX + getBox().getWidth() > width ? -(getBox().getWidth() + 5F) : 10F;
	}
}