#include <windows.h>
#include "ui.h"
//#include "uitooltip.h"

// ----------------------------------------------------------------------------------
CUiTooltip::CUiTooltip(const char* name, CUiLayout* pParent, CUi* pUi)
	: CUiFrame(name, pParent, pUi)
{
	m_uiType			= eUiType_Tooltip;

	m_lines				= 0;
	m_maxWidth			= 0.0f;

	memset(m_warp, 0, sizeof(m_warp));
}

// ----------------------------------------------------------------------------------
CUiTooltip::~CUiTooltip()
{
}

// ----------------------------------------------------------------------------------
void CUiTooltip::ClearLines()
{
	CUiFontString *str;
	char tmp[256];

	for ( int i = 0; i < m_lines; i++ )
	{
		sprintf( tmp, "%sTextLeft%d", m_name, i+1 );
		str = (CUiFontString*)m_pUi->FindObject(tmp);
		if ( str )
			str->SetVisible(false);

		sprintf( tmp, "%sTextRight%d", m_name, i+1 );
		str = (CUiFontString*)m_pUi->FindObject(tmp);
		if ( str )
			str->SetVisible(false);
	}
	m_maxWidth = 0.0f;
	m_lines = 0;
	memset(m_warp, 0, sizeof(m_warp));
}

// ----------------------------------------------------------------------------------
void CUiTooltip::AddDoubleLine(const char* textL, const char*textR, float rL, float gL, float bL, float rR, float gR, float bR )
{
	CUiFontString *strL, *strR;
	char tmp[256];
	CUiSize size;

	sprintf( tmp, "%sTextLeft%d", m_name, m_lines+1 );
	strL = (CUiFontString*)m_pUi->FindObject(tmp);
	sprintf( tmp, "%sTextRight%d", m_name, m_lines+1 );
	strR = (CUiFontString*)m_pUi->FindObject(tmp);
	if ( strL && strR )
	{
		CUiAnchor anchor;
		strL->SetAnchorSize(CUiSize());
		strR->SetAnchorSize(CUiSize());

		strL->SetColor(rL, gL, bL);
		strL->SetTextA(textL);
		strL->SetVisible(true);
		strR->SetColor(rR, gR, bR);
		strR->SetTextA(textR);
		strR->SetVisible(true);
		m_lines++;
		
		TooltipUpdate(strL->GetWidth() + strR->GetWidth() + 40);
	}	
}

// ----------------------------------------------------------------------------------
void CUiTooltip::AddLine(const char* text, float r, float g, float b, bool warp)
{	
	CUiFontString* str;	
	char tmp[256];

	sprintf( tmp, "%sTextLeft%d", m_name, m_lines+1 );
	str = (CUiFontString*)m_pUi->FindObject(tmp);
	if ( str )
	{		
		m_warp[m_lines] = warp;
		if ( warp )
			str->SetAnchorSize(CUiSize(m_maxWidth, 0.0f));
		else
			str->SetAnchorSize(CUiSize());

		str->SetColor(r, g, b);
		str->SetTextA(text);
		str->SetVisible(true);
		m_lines++;

		TooltipUpdate(str->GetWidth());
	}
}

// ----------------------------------------------------------------------------------
void CUiTooltip::SetText(const char* text, float r, float g, float b, bool warp)
{
	ClearLines();
	AddLine(text, r, g, b, warp);
}

// ----------------------------------------------------------------------------------
void CUiTooltip::TooltipUpdate(float width)
{
	CUiFontString *strL, *strR;
	float scale = GetRealScale();
	char tmp[256];

	if ( width > 260.0f )
		width = 260.0f;

	if ( width > m_maxWidth )
	{
		m_maxWidth = width;
		for ( int i = 0; i < m_lines; i++ )
		{
			sprintf( tmp, "%sTextLeft%d", m_name, i+1 );
			strL = (CUiFontString*)m_pUi->FindObject(tmp);
			sprintf( tmp, "%sTextRight%d", m_name, i+1 );
			strR = (CUiFontString*)m_pUi->FindObject(tmp);

			if ( strR->IsVisible() )
			{
				CUiAnchor anchor;
				anchor.m_relativeTo		= strL;
				anchor.m_point			= CUiAnchor::eAnchor_Left;
				anchor.m_relativePoint	= CUiAnchor::eAnchor_Right;
				anchor.m_offset.m_y		= 0.0f;
				anchor.m_offset.m_x		= m_maxWidth - strL->GetWidth() - strR->GetWidth();
				strR->ClearAllAnchors();
				strR->SetAnchor(anchor);
			}
			else if ( m_warp[i] )
			{
				strL->SetAnchorSize(CUiSize(m_maxWidth, 0.0f));
			}
		}
	}

	float top, bottom;
	sprintf( tmp, "%sTextLeft%d", m_name, 1 );
	strL = (CUiFontString*)m_pUi->FindObject(tmp);
	top = strL->GetPos().m_y;

	sprintf( tmp, "%sTextLeft%d", m_name, m_lines );
	strL = (CUiFontString*)m_pUi->FindObject(tmp);
	sprintf( tmp, "%sTextRight%d", m_name, m_lines );
	strR = (CUiFontString*)m_pUi->FindObject(tmp);
	if ( strL )
		bottom = strL->GetPos().m_y + strL->GetHeight() * scale;
	if ( strR && strR->IsVisible() )
	{
		float height = strL->GetPos().m_y + strL->GetHeight() * scale;
		if ( height > bottom )
			bottom = height;
	}

	SetSize( CUiSize(m_maxWidth + 20.0f, (bottom - top) / scale + 20.0f) );
}