#pragma once
#include <Engine/Animation.h>
#include <Widgets/LayerWidget.h>
#include <Widgets/WidgetRegistry.h>
#include <Engine/Graphics.h>

using namespace gge;

class AnimSelect : public widgets::LayerWidget {
public:

	AnimSelect() : anim(NULL), state(false), ChangeEvent("Click", this) {
		selection=&widgets::WR.Borders.Selection->CreateAnimation(true);
		focus=&widgets::WR.Borders.Focus->CreateAnimation(true);
		GetLayer().MouseCallback.Set(*this, &AnimSelect::layer_mouse);
		Refresh();
	}

	void SetAnimation(animation::RectangularGraphic2DAnimation *anim) {
		this->anim=anim;
		Refresh();
	}
	animation::RectangularGraphic2DAnimation *GetAnimation() const {
		return anim;
	}

	void Toggle() {
		state=!state;
		Refresh();
		ChangeEvent();
	}
	void Set() {
		if(!state) Toggle();
	}
	void SetState(bool state) {
		if(state ^ this->state)
			Toggle();
	}
	void Clear() {
		if(state) Toggle();
	}
	bool Get() const {
		return state;
	}

	using LayerWidget::Resize;
	virtual void Resize(utils::Size Size) {
		LayerWidget::Resize(Size);

		Refresh();
	}

	void Refresh() {
		GetLayer().Clear();

		selection->DrawIn(GetLayer(), 0,0, size);
		if(state)
			focus->DrawIn(GetLayer(), 2,2, size-utils::Size(4,4));

		if(anim) {
			anim->DrawIn(GetLayer(), gge::graphics::SizeController2D::SingleMiddleCenter);

			if(anim->HasController())
				anim->GetController().ResetProgress();
		}
	}

	bool layer_mouse(input::mouse::Event::Type event, utils::Point location) {
		if(event==input::mouse::Event::Left_Click)
			Set();

		return !input::mouse::Event::isScroll(event);
	}

	utils::EventChain<AnimSelect> ChangeEvent;

protected:
	animation::RectangularGraphic2DAnimation *anim;
	widgets::BorderData *selection, *focus;

	bool state;
};
