#include "VisibilityHandler.h"
#include "Font.h"

#include <Renderer/PngImage.h>
#include <Utility/Log.h>

//////////////////////////////////////////////////////////////
namespace Dubious {
namespace Panels {

//////////////////////////////////////////////////////////////
VisibilityHandler::VisibilityHandler()
{
}

//////////////////////////////////////////////////////////////
VisibilityHandler::~VisibilityHandler()
{
}

//////////////////////////////////////////////////////////////
void VisibilityHandler::BindTexture( Utility::NativeAssetFilePtr pNativeFile, GLuint Texture )
{
	Renderer::PngImagePtr pImage( new Renderer::PngImage( pNativeFile ) );
	BindTexture( pImage, Texture );
}

//////////////////////////////////////////////////////////////
void VisibilityHandler::BindTexture( Renderer::PngImagePtr pImage, GLuint Texture )
{
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
	glBlendFunc(GL_ONE, GL_SRC_COLOR);
	glBindTexture(GL_TEXTURE_2D, Texture);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pImage->Dimensions().X(), pImage->Dimensions().Y(),
				 0, GL_RGBA, GL_UNSIGNED_BYTE, pImage->Data());
}

//////////////////////////////////////////////////////////////
void VisibilityHandler::CreateGLVertexArrays( const Math::Size& Dimensions, GLPointVector& Points, std::vector<unsigned short>& Indices )
{
	Points.push_back( GLPoint( 0, Dimensions.Y(), 	0, 1 ) );
	Points.push_back( GLPoint( Dimensions.X(), 	Dimensions.Y(), 	1, 1 ) );
	Points.push_back( GLPoint( 0, 0, 	0, 0 ) );
	Points.push_back( GLPoint( Dimensions.X(), 	0, 	1, 0 ) );

	Indices.push_back( 0 );
	Indices.push_back( 1 );
	Indices.push_back( 2 );
	Indices.push_back( 3 );
}

//////////////////////////////////////////////////////////////
void VisibilityHandler::CreateGLVertexArrays( const std::string& Text, const Font& TheFont, GLPointVector& Points, std::vector<unsigned short>& Indices )
{
	int x = 0;
	int Height = TheFont.Height();
	for (int i=0; i<Text.size(); ++i) {
		Math::RectangleYDown Char = TheFont.LetterRectangle( Text[i] );
		float TexX 	= static_cast<float>(Char.Origin().X()) / static_cast<float>(TheFont.Image()->Dimensions().X());
		float TexY 	= static_cast<float>(Char.Origin().Y()) / static_cast<float>(TheFont.Image()->Dimensions().Y());
		float TexX2 = TexX + static_cast<float>(Char.Dimensions().X()) / static_cast<float>(TheFont.Image()->Dimensions().X());
		float TexY2 = TexY + static_cast<float>(Char.Dimensions().Y()) / static_cast<float>(TheFont.Image()->Dimensions().Y());

		// Triangle 1
		Points.push_back( GLPoint( x, Height, 							TexX, TexY2 ) );
		Indices.push_back( Points.size()-1 );
		Points.push_back( GLPoint( x+Char.Dimensions().X(), Height, 	TexX2, TexY2 ) );
		Indices.push_back( Points.size()-1 );
		Points.push_back( GLPoint( x, 0, 								TexX, TexY ) );
		Indices.push_back( Points.size()-1 );

		// Triangle 2
		Points.push_back( GLPoint( x, 0, 								TexX, TexY ) );
		Indices.push_back( Points.size()-1 );
		Points.push_back( GLPoint( x+Char.Dimensions().X(), Height, 	TexX2, TexY2 ) );
		Indices.push_back( Points.size()-1 );
		Points.push_back( GLPoint( x+Char.Dimensions().X(), 0, 			TexX2, TexY ) );
		Indices.push_back( Points.size()-1 );

		x += Char.Dimensions().X() + TheFont.LetterPad();

	}
}

}
}

