




#include <flare_gui/widget_parent.h>

#include <allegro_flare/allegro_flare.h>





FGUIParent::FGUIParent(FGUIParent *parent, FGUICollisionArea *collision_area)
	: mouse_blocked(false)
	, FGUIWidget(parent, collision_area)
{}



#include <iostream>

FGUIParent::~FGUIParent()
	// ?
{
	// detach the widget from the parent
	parent->detach_widget(this);

	// delete the collision area
	delete collision_area;

	// delete the children that were created in this->ctor
	for (unsigned i=0; i<children.size(); i++)
	{
		delete children[i];
		std::cout << "child deleted" << std::endl;
		i--;
	}
}




bool FGUIParent::delete_child(FGUIWidget *child)
{
	for (unsigned i=0; i<children.size(); i++)
	{
		if (children[i] == child)
		{
			delete children[i]; // child will self-detach
			std::cout << "child deleted" << std::endl;
			i--;
			return true;
		}
	}
	return false;
}





void FGUIParent::draw_func()
{
	FGUIWidget::draw_func();

	placement2d *placement = gimmie_placement();
	placement->start_transform();

	for (unsigned i=0; i<children.size(); i++)
	{
		children[i]->draw_func();
	}

	placement->restore_transform();
}




void FGUIParent::primary_timer_func()
{
	FGUIWidget::primary_timer_func();

	for (unsigned i=0; i<children.size(); i++)
		children[i]->primary_timer_func();
}



void FGUIParent::attach_widget(FGUIWidget *widget)
{
	children.push_back(widget);
	widget->parent = this;
}



#include <iostream>
void FGUIParent::detach_widget(FGUIWidget *widget)
{
	for (unsigned i=0; i<children.size(); i++)
	{
		if (children[i] == widget)
		{
			children.erase(children.begin()+i);
			i--;
			std::cout << "destroy_widget(): widget found and deleted successfully." << std::endl;
			return;
		}
		//children.push_back(widget);
	}
	std::cout << "destroy_widget() error: widget not found" << std::endl;
}




void FGUIParent::receive_message(std::string message) {};




void FGUIParent::mouse_axes_func(float mx, float my, float mdx, float mdy)
{
	float tmx = mx;
	float tmy = my;
	float tmdx = mdx;
	float tmdy = mdy;

	collision_area->placement.transform_coordinates(&tmx, &tmy);
	collision_area->placement.transform_coordinates(&tmdx, &tmdy);

	if (parent && parent->mouse_blocked) mouse_blocked = true;
	else mouse_blocked = false;

	for (int i=(int)children.size()-1; i>=0; i--)
		children[i]->mouse_axes_func(tmx, tmy, mdx, mdy);

	FGUIWidget::mouse_axes_func(mx, my, mdx, mdy);
}



void FGUIParent::mouse_down_func()
{
	for (unsigned i=0; i<children.size(); i++)
		children[i]->mouse_down_func();
	FGUIWidget::mouse_down_func();
}



void FGUIParent::mouse_up_func()
{
	for (unsigned i=0; i<children.size(); i++)
		children[i]->mouse_up_func();
	FGUIWidget::mouse_up_func();
}



void FGUIParent::key_down_func()
{
	for (unsigned i=0; i<children.size(); i++)
		children[i]->key_down_func();
	FGUIWidget::key_down_func();
}



void FGUIParent::key_up_func()
{
	for (unsigned i=0; i<children.size(); i++)
		children[i]->key_up_func();
	FGUIWidget::key_up_func();
}



void FGUIParent::key_char_func()
{
	for (unsigned i=0; i<children.size(); i++)
		children[i]->key_char_func();
	FGUIWidget::key_char_func();
}



bool FGUIParent::set_focus_to(FGUIWidget *child)
	// returns true if the child was set from an unfocused state to a focused state
{
	// check that this parent has this child
	if (!has_child(child)) return false;

	// make sure the child is not already focused
	if (child->is_focused()) return false;

	// if there are other widgets that have focus, blur them
	for (unsigned i=0; i<children.size(); i++)
	{
		if (children[i]->is_focused())
		{
			children[i]->set_as_unfocused();
		}
	}

	child->focused = true;

	return true;
}


bool FGUIParent::has_child(FGUIWidget *widget)
{
	for (unsigned i=0; i<children.size(); i++)
		if (widget == children[i]) return true;
	return false;
}