#include "StdAfx.h"
#include "appearance.h"

Appearance::Appearance(void)
{
	location.X = 0;
	location.Y = 0;
	size.Width = 100;
	size.Height = 10;
	fontname = L"Arial";
	fontsize = 10;
	textlocation.X = 0;
	textlocation.Y = 0;
	fromright = 0;
	frombottom = 0;
	taborder = -1;
	text = L"";
	
	SetState(AppearanceState::normal);
}

Appearance::~Appearance(void)
{
}

Appearance& Appearance::operator=(Appearance& x)
{
	Set(x);
	return *this;
}

void Appearance::Set(Appearance& x)
{
	fontname = x.fontname;
	fontsize = x.fontsize;
	location = x.location;
	size = x.size;
	textlocation = x.textlocation;
	normal = x.normal;
	selected = x.selected;
	mouseover = x.mouseover;
	pressed = x.pressed;
	fromright = x.fromright;
	frombottom = x.frombottom;
	taborder = x.taborder;
	SetState(x.state);
}

Attributes::Attributes()
{

	bgcolor = Color(0,0,0,0);
	bordercolor = Color(0,0,0,0);
	textcolor = Color(0,0,0,0);
	borderwidth = 0;
	image = NULL;
	imagex = 0;
	imagey = 0;
	opacity = 100;
	drawtext = 1;
}

Attributes& Attributes::operator=(Attributes& x)
{
	bgcolor = x.bgcolor;
	image = x.image;
	bordercolor = x.bordercolor;
	borderwidth = x.borderwidth;
	textcolor = x.textcolor;
	imagex = x.imagex;
	imagey = x.imagey;
	opacity = x.opacity;
	drawtext = x.drawtext;
	return *this;
}

void Appearance::SetState(AppearanceState s)
{
	state = s;
	switch(s){
		case AppearanceState::normal:
			current = &normal;
			break;
		case AppearanceState::selected:
			current = & selected;
			break;
		case AppearanceState::mouseover:
			current = &mouseover;
			break;
		case AppearanceState::pressed:
			current = &pressed;
			break;
		default:
			current= &normal;
	}
}

// loads this appearance from given namespace in the configuration
void Attributes::Load(const char * b)
{
	bgcolor = env.GetColor(b,"bgcolor",0xff000000);
	std::wstring filename = env.GetWideString(b,"image",L"none");
	if(filename!=L"none")
		image = LoadImageResource(filename.data());
	else
		image = NULL;
	imagex = env.GetInt(b,"imagex",0);
	imagey = env.GetInt(b,"imagey",0);
	bordercolor = env.GetColor(b,"bordercolor",0xffffffff);
	borderwidth = env.GetDouble(b,"borderwidth",1.0);
	textcolor = env.GetColor(b,"textcolor",0xffffffff);
	opacity = env.GetDouble(b,"opacity",100);
	drawtext = env.GetInt(b,"drawtext",1);
}

void Appearance::Draw(Graphics& graphics)
{
	try {
		
		if(current->bgcolor.GetAlpha()>0) {

			// Create a SolidBrush object.
			SolidBrush bgbrush(current->bgcolor);

			// Create a Rect object.
			Rect fillRect(Point(0,0),size);

			// Fill the rectangle.
			graphics.FillRectangle(&bgbrush, fillRect);

			if(current->borderwidth>0){
				// Create a Pen object.
				Pen borderpen(current->bordercolor, current->borderwidth);

				// Draw the rectangle that bounds the image.
				fillRect.Width--;
				fillRect.Height--;
				graphics.DrawRectangle(&borderpen, fillRect);
			}
		} 

		if(current->image){
			DrawTransparentImage(&graphics,current->image,current->imagex,current->imagey,size.Width,size.Height);
		}

		if(current->drawtext && text.size()>0){
			// draw the text
			FontFamily   fontFamily(fontname.data());
			Font         font(&fontFamily, fontsize, FontStyleRegular, UnitPoint);
			SolidBrush brush(current->textcolor);
			PointF		origin(location.X+textlocation.X,
								location.Y+textlocation.Y);
			StringFormat stringFormat;
			// left-justify each line of text.
			stringFormat.SetAlignment(StringAlignmentNear);
			stringFormat.SetLineAlignment(StringAlignmentNear);
			graphics.DrawString(text.data(), -1, &font, origin, &stringFormat, &brush);
		}

	} catch(...) {
		print(L"Error drawing Appearance.\n");
		return;
	}
}

void Appearance::Load(const char * b)
{
	std::string name;
	name = b;
	name += "::normal";
	normal.Load(name.data());
	name = b;
	name += "::selected";
	selected.Load(name.data());
	name = b;
	name += "::mouseover";
	mouseover.Load(name.data());
	name = b;
	name+= "::pressed";
	pressed.Load(name.data());

	location.X = env.GetInt(b,"x",0);
	location.Y = env.GetInt(b,"y",0);
	size.Width = env.GetInt(b,"width",100);
	size.Height = env.GetInt(b,"height",100);
	textlocation.X = env.GetInt(b,"textx",0);
	textlocation.Y = env.GetInt(b,"texty",0);
	fontname = env.GetWideString(b,"fontname",L"Arial");
	fontsize = env.GetDouble(b,"fontsize",10);
	description = env.GetWideString(b,"description",L"");
	fromright = env.GetInt(b,"fromright",0);
	frombottom = env.GetInt(b,"frombottom",0);
	taborder = env.GetInt(b,"taborder",-1); //-1 means skip

	int useimagesize = env.GetInt(b,"useimagesize",0);
	if(useimagesize && normal.image){
		size.Width = normal.image->GetWidth();
		size.Height = normal.image->GetHeight();
	}
}