#pragma once

#include <Widgets/Panel.h>
#include <Widgets/RadioButton.h>
#include <Widgets/LayerWidget.h>
#include <Widgets/InlinePanel.h>

#include "AnimSelect.h"

using namespace gge;

class DirectionSelector : public widgets::InlinePanel {
public:
	enum Direction {
		North=0,
		East,
		South,
		West,
		end
	};

	DirectionSelector() : direction(North),
	ChangedEvent("Changed", this)
	{
		Padding=0;

		for(int i=0;i<end;i++) {
			layers[i].Resize(68,68);
			layers[i].Move(i*70,0);
			layers[i].SetContainer(this);
		}

		layers[0].ChangeEvent.RegisterLambda([&]{ if(layers[0].Get()) SetDirection(North); });
		layers[1].ChangeEvent.RegisterLambda([&]{ if(layers[1].Get()) SetDirection(East); });
		layers[2].ChangeEvent.RegisterLambda([&]{ if(layers[2].Get()) SetDirection(South); });
		layers[3].ChangeEvent.RegisterLambda([&]{ if(layers[3].Get()) SetDirection(West); });


		SetContainer(widgets::TopLevel);
		Resize(280, 140);
		Resize(280+GetOverheadMargins().TotalX(), 70+GetOverheadMargins().TotalX());

		layers[0].Set();
	}

	void SetAnimations(utils::Collection<animation::RectangularGraphic2DAnimation> animations) {
		for(int i=0;i<4;i++) {
			if(this->layers[i].GetAnimation())
				this->layers[i].GetAnimation()->DeleteAnimation();
			
			this->layers[i].SetAnimation(animations(i));
		}
	}

	Direction GetDirection() const {
		return direction;
	}

	void SetDirection(Direction direction) {
		if(this->direction==direction) return;

		this->direction=direction;
		for(int i=0;i<4;i++)
			if(i!=direction)
				layers[i].Clear();

		layers[direction].Set();
		
		ChangedEvent();
	}
	
	void Refresh() {
		for(int i=0;i<4;i++)
			layers[i].Refresh();
	}
	
	utils::EventChain<DirectionSelector> ChangedEvent;

protected:
	Direction direction;

	AnimSelect layers[4];
};
