#pragma once

#include "../Common/ImageList.h"
#include <Gorgon/Game/Animation.h>

using namespace gge;

class AnimationEditor : public ImageList {
public:

	AnimationEditor() : animation(), dontadd(false), duration(125) {
		AllowReorder=true;
	}


	void SetAnimation(gorgon::Animation *animation) {
		if(animation==this->animation) return;

		DeleteAll();

		if(animation==NULL) {
			Disable();
			return;
		}

		this->animation=animation;

		auto &animations=animation->GetFrames();

		dontadd=true;
		for(unsigned i=0;i<animations.size();i++) {
			Add(animations[i].Image);
		}
		dontadd=false;
	}

	gorgon::Animation *GetAnimation() {
		return animation;
	}

	int duration;

protected:
	virtual void itemadded(ItemType &item) {
		ImageList::itemadded(item);

		if(dontadd) return;

		animation->Insert(dynamic_cast<gorgon::Image*>((resource::Image*)item.Value), FindLocation(item), duration);
	}

	virtual void itemremoving(ItemType &item) {
		ImageList::itemremoving(item);

		animation->Remove(ImageList::FindLocation(item));
	}
	
	virtual void movebefore(widgets::IListItem<resource::Image*, NullString> &item, widgets::IListItem<resource::Image*, NullString> *before) {
		animation->Move(FindLocation(dynamic_cast<ItemType&>(item)),FindLocation(dynamic_cast<ItemType*>(before)));

		ImageList::movebefore(item, before);
	}

	gorgon::Animation *animation;
	bool dontadd;
};
