#include "cButton.hpp"

/*** Custom Header Files ***/
#include "GUIHelpers/Enums.hpp"
#include "../VideoEngine/cRenderer.hpp"
#include "../FXEngine/cGFX.hpp"

using GUIEngine::cButton;

using MathEngine::iVector2;
using MathEngine::iVector4;

/*static*/ const cString cButton::sNAME = "button";

cButton::cButton()
{}

cButton::cButton( cWindow* parent, const signed int id, const GUIHelpers::eAlign& align /*= sALIGN*/, const GUIHelpers::eLayout& layout /*= sLAYOUT*/,
	const GUIHelpers::Position& pos /*= sPOSITION*/, const GUIHelpers::Size& size /*= sSIZE*/, const GUIHelpers::Padding& padding/* = sPADDING*/,
	const cString& name /*= sNAME*/, VideoEngine::cImage** image /*= nullptr*/ )
	: cWindow(parent, id, sORIENTATION, align, layout, pos, size, padding, name)
{
	//Create(parent, id, align, layout, pos, size, padding, name, image);
	mp_texture = nullptr;
	mp_texturePress = nullptr;

	mpp_image = image;

	GenerateImage();
}

/*virtual*/ cButton::~cButton()
{
	delete mp_texture;
	mp_texture = nullptr;

	delete mp_texturePress;
	mp_texturePress = nullptr;

}

///Functions
void cButton::Create( cWindow* parent, const signed int id, const GUIHelpers::eAlign& align /*= sALIGN*/, const GUIHelpers::eLayout& layout /*= sLAYOUT*/, 
	const GUIHelpers::Position& pos /*= sPOSITION*/,	const GUIHelpers::Size& size /*= sSIZE*/, const GUIHelpers::Padding& padding /*= sPADDING*/, 
	const cString& name /*= sNAME*/, VideoEngine::cImage** image /*= nullptr*/ )
{
	cWindow::Create(parent, id, sORIENTATION, align, layout, pos, size, padding, name);
	mp_texture = nullptr;
	mp_texturePress = nullptr;

	mpp_image = image;	

	GenerateImage();
}

/*virtual*/ void cButton::Press()
{
	mp_texture->SaveImage("hello.bmp");
}

// void cButton::setLabel( const cString& label )
// {
// 	if (label.size() > 0) {
// 		if (mp_label == nullptr)
// 			mp_label = new TextTypeEngine::cText(label);
// 		else
// 			mp_label->setText(label);
// 	}
// }

///Gets
// const cString cButton::getLabel() const
// {
// 	cString rtn = "";
// 	if (mp_label != nullptr)
// 		rtn = mp_label->getText();
// 	return rtn;
// }

/*virtual*/ const GUIHelpers::eType cButton::getType() const
{
	return GUIHelpers::eType::CBUTTON;
}


//private
void cButton::GenerateImage()
{
	GenerateTexture();
	//GenerateTexture( mp_texturePress );

	this->setImage((VideoEngine::cImage**)(&mp_texture));

	this->setImageArea(mp_texture->getWH());
}

void cButton::GenerateTexture()
{
// 	SDL_Rect t = { 0, 0, 0, 0 };
// 	if (mp_label != nullptr)
// 		t = mp_label->getWH();
// 	SDL_Rect centerText = { 0, 0, 0, 0 };
// 	SDL_Rect centerButton = { 0, 0, 0, 0 };
// 	SDL_Rect dim = this->getImageArea();
// 
// 	GUIHelpers::Padding TXTPADDING = PADDING;
// 	///Find the min padding around the text.
// 	if (dim.w < (t.w + TXTPADDING.x + TXTPADDING.w) )
// 		dim.w += (t.w + TXTPADDING.x + TXTPADDING.w);
// 
// 	if (dim.h < (t.h + TXTPADDING.y + TXTPADDING.z) )
// 		dim.h += (t.h + TXTPADDING.y + TXTPADDING.z);
// 
// 	///Find the center of the button	
// 	centerButton.x = dim.w / 2;
// 	centerButton.y = dim.h / 2;
// 
// 	///Find the center of the text
// 	centerText.x = t.w / 2;
// 	centerText.y = t.h / 2;
// 
// 	if (centerText.x < centerButton.x)
// 		t.x = centerButton.x - centerText.x;
// 	
// 	if (centerText.y < centerButton.y)
// 		t.y = centerButton.y - centerText.y;
// 	
// 	SDL_Texture* temptext = VideoEngine::cRenderer::Inst().NewTexture(dim.w, dim.h, SDL_TEXTUREACCESS_TARGET );
// 
// 	GUIHelpers::RBGA colour = { 100, 100, 100, 255 };
// 
// 	FXEngine::cGFX::Inst().RoundedRectangle(dim, 4, colour, temptext);
// 
// 	
//  	if (mp_label != nullptr)
// 		VideoEngine::cRenderer::Inst().RenderToTexture(mp_label->getImage(), nullptr, temptext, &t);

	
	//cRenderer::Inst().Render(temptext, nullptr, nullptr);

	//cRenderer::Inst().RenderToTexture(temptext, nullptr, text->getImage(), nullptr);

	/*SDL_Surface* tempsurface = SDL_CreateRGBSurface(cVideo::Inst().getVideoSettings(), dimensions.w, dimensions.h, cVideo::Inst().getColour(),
		0, 0, 0, 0);

	SDL_FillRect(tempsurface, nullptr, SDL_MapRGB(tempsurface->format, 0, 255, 0));

	/*if (roundedBoxRGBA(tempsurface, dimensions.x, dimensions.y, dimensions.w, dimensions.h, 10, 255, 255, 0, 255) != 0)
		cerr << "Unable to generate GUI Texture." << endl << SDL_GetError() << endl;*/

	
	/*if (text != nullptr) {
		SDL_Rect srcrect, dstrect = dimensions;
		cVideo::Inst().Render(text->getImage(), &srcrect, temptext, &dstrect);
	}

	this->setImage(temptext);
	this->setTransparent();*/

// 	delete mp_texture;
// 	mp_texture = nullptr;
// 
// 	mp_texture = new VideoEngine::cImage(temptext);
// 
// 	GUIHelpers::Size size = { 0, 0 };
// 	mp_texture->getWH((unsigned long&)size.x, (unsigned long&)size.y);
// 	cWindow::setSize(size);
//	image->setImage(temptext);
	//image->setImage(temptext);
}
