//	Darkstar Engine  (c) UnderWorld Interactive Studio (all coding by Svarog    2007-2009)
//
//      Данный файл является частью Darkstar Engine. Любое использование кода файла
//      без согласия компании, является нарушением закона об авторском праве (в слу-
//      чае если не оговорено обратное).
//
//      Имя файла: button.cpp
//      Краткое описание: simple gui button
//

#include <Darkstar/Graphic System/gui/button.hpp>
#include <Darkstar/Kernel/parser.hpp>
#include <Darkstar/Graphic System/effect_main.hpp>
#include <Darkstar/Kernel/file_system.hpp>
#include <Darkstar/Graphic System/gui/gui_common.hpp>
#include <Darkstar/Resource System/Textures/textureManager.hpp>

using gs::Button;
using rs::StaticModel;
using gs::EffectManager;
using ker::Parser;
using ker::DFile;
using ker::Section;
using ker::PairInfo;
using gs::GUI_Rect;
using rs::BaseTexture;
using rs::TextureManager;

Button::Button(const std::string& desc_file)
{
	DFile button_desc(desc_file);
	Parser p(button_desc);

	if(p.getIsParsedSuccessfully())
	{
		shared_ptr<Section> root = p.getRootSection();
		std::vector<shared_ptr<Section> >::iterator it;
		std::map<std::string, PairInfo>::iterator vit;
		shared_ptr<Effect> effect;
		GUI_Rect unp_but, p_but;
		shared_ptr<BaseTexture> pressed_button, unpressed_button, pressed_button_mask, unpressed_button_mask;
		
		for(it = root->sections.begin(); it != root->sections.end(); ++it)
		{
			if((*it)->section_name == "effect")
			{
				effect = EffectManager::ptr()->load((*it)->section_value);
				if(!effect)
					return;
				vit = (*it)->values.find("technique");
				if(vit != (*it)->values.end())
					effect->setCurTechnique(vit->second.value);
			};
			if((*it)->section_name == "button_unpressed" || ((*it)->section_name == "button_pressed"))
			{
				GUI_Rect* but_rect;
				shared_ptr<BaseTexture>* texture;
				shared_ptr<BaseTexture>* texture_mask;
				if((*it)->section_name == "button_unpressed")
				{
					but_rect = &unp_but;
					texture = &unpressed_button;
					texture_mask = &unpressed_button_mask;
				}
				else
				{
					but_rect = &p_but;
					texture = &pressed_button;
					texture_mask = &pressed_button_mask;
				};
				vit = (*it)->values.find("x");
				if(vit != (*it)->values.end())
					but_rect->x = (unsigned int)atoi(vit->second.value.c_str());
				vit = (*it)->values.find("y");
				if(vit != (*it)->values.end())
					but_rect->y = (unsigned int)atoi(vit->second.value.c_str());
				vit = (*it)->values.find("width");
				if(vit != (*it)->values.end())
					but_rect->width = (unsigned int)atoi(vit->second.value.c_str());
				vit = (*it)->values.find("height");
				if(vit != (*it)->values.end())
					but_rect->height = (unsigned int)atoi(vit->second.value.c_str());
				vit = (*it)->values.find("graphic_file");
				if(vit != (*it)->values.end())
					*texture = TextureManager::ptr()->load(vit->second.value);
				vit = (*it)->values.find("mask_file");
				if(vit != (*it)->values.end())
					*texture_mask = TextureManager::ptr()->load(vit->second.value);
			};
		};

	};
};

Button::~Button()
{

};