



#include <flare_gui/button.h>



#include <allegro5/allegro_font.h>
#include <allegro5/allegro_primitives.h>

#include <allegro_flare/motion_control.h>
#include <allegro_flare/color.h>

#include <flare_gui/widget.h>
#include <flare_gui/collision_box.h>

#include <allegro_flare/allegro_flare.h> // for af::time_now





FGUIButton::FGUIButton(FGUIParent *parent, std::string text, ALLEGRO_FONT *font, float x, float y, float w, float h)
	: FGUIWidget(parent, new FGUICollisionBox(x, y, w, h))
	, text(text)
	//, text_alignment(ALLEGRO_ALIGN_CENTER)
	, content_alignment(0.5)
	, font(font)
	, icon(NULL)
//	, motion(4)
{}



void FGUIButton::on_timer() {}



void FGUIButton::set_icon(ALLEGRO_BITMAP *icon)
{
	if (!icon) return;
	this->icon = icon;
}



void FGUIButton::on_draw()
{
	placement2d &placement = collision_area->placement;

	al_draw_filled_rounded_rectangle(0, 0, placement.w, placement.h, 3, 3, color::mintcream);
	al_draw_rounded_rectangle(0, 0, placement.w, placement.h, 3, 3, color::color(color::black, 0.4), 2);

	// draw the icon and/or the text
	// in this case, the icon is always drawn to the left of the text
	// with a padding of 10px

	// get the width of the inner content
	float inner_content_width = 0;
	float start_x = 0;
	if (icon) inner_content_width += al_get_bitmap_width(icon); // the width of the icon
	if (font && !text.empty() && icon) inner_content_width += 10; // the padding between the icon and the text
	if (font && !text.empty()) inner_content_width += al_get_text_width(font, text.c_str());
	start_x = placement.w * content_alignment - inner_content_width * content_alignment;

	// draw the stuff
	if (icon)
	{
		al_draw_bitmap(icon, start_x, placement.h/2 - al_get_bitmap_height(icon)/2, NULL);
		start_x += al_get_bitmap_width(icon);
	}
	if (font && !text.empty() && icon)
	{
		start_x += 10;
	}
	if (font && !text.empty())
	{
		al_draw_text(font, color::black, start_x, placement.h/2-al_get_font_line_height(font)/2, ALLEGRO_ALIGN_LEFT, text.c_str());
	}

	// draw the shade
	float padding = 3;
	al_draw_filled_rounded_rectangle(padding, placement.h/2 + padding, placement.w-padding, placement.h-padding, 2, 2, color::color(color::orange, 0.15));
	//placement.restore_transform();
}



void FGUIButton::set_text(std::string text)
{
	text = text;
}



void FGUIButton::on_click()
{
	placement2d &placement = collision_area->placement;

	if (!gimmie_motion()) return;
	MotionManager &motion = (*gimmie_motion());

	motion.clear_animations_on(&placement.scale_x);
	motion.clear_animations_on(&placement.scale_y);
	motion.animate(&placement.scale_x, placement.scale_x, placement.scale_x*0.9, af::time_now, af::time_now+0.05, interpolator::doubleFastIn, NULL, NULL);
	motion.animate(&placement.scale_y, placement.scale_y, placement.scale_y*0.9, af::time_now, af::time_now+0.05, interpolator::doubleFastIn, NULL, NULL);
	motion.animate(&placement.scale_x, placement.scale_x*0.9, placement.scale_x, af::time_now+0.05, af::time_now+0.1, interpolator::doubleFastIn, NULL, NULL);
	motion.animate(&placement.scale_y, placement.scale_y*0.9, placement.scale_y, af::time_now+0.05, af::time_now+0.1, interpolator::doubleFastIn, NULL, NULL);
	//std::cout << "gurp" << std::endl;
	//click_func();
}



void FGUIButton::on_mouse_enter()
{
	placement2d &placement = collision_area->placement;
	MotionManager &motion = (*gimmie_motion());

	motion.clear_animations_on(&placement.scale_x);
	motion.clear_animations_on(&placement.scale_y);
	motion.animate(&placement.scale_x, placement.scale_x, 1.1, af::time_now, af::time_now+0.1, interpolator::doubleFastIn, NULL, NULL);
	motion.animate(&placement.scale_y, placement.scale_y, 1.1, af::time_now, af::time_now+0.1, interpolator::doubleFastIn, NULL, NULL);
}



void FGUIButton::on_mouse_leave()
{
	placement2d &placement = collision_area->placement;
	MotionManager &motion = (*gimmie_motion());

	motion.clear_animations_on(&placement.scale_x);
	motion.clear_animations_on(&placement.scale_y);
	motion.animate(&placement.scale_x, placement.scale_x, 1.0, af::time_now, af::time_now+0.1, interpolator::doubleFastIn, NULL, NULL);
	motion.animate(&placement.scale_y, placement.scale_y, 1.0, af::time_now, af::time_now+0.1, interpolator::doubleFastIn, NULL, NULL);
}



//void FGUIButton::click_func() {}
