#pragma once
#include "../Assets/Animation.h"
#include "../Animator.h"
#include "../Basic.h"
#include <Engine/Animation.h>
#include <string>
#include <Utils/Point2D.h>
#include <string>
#include <Engine/GraphicLayers.h>
#include <Resource/Animation.h>

namespace graphics
{
	class Animation: public Animator
	{
	public:
		Animation(assets::AnimationProvider &given_provider);
		virtual ~Animation() {
			if(animation)
				animation->DeleteAnimation();
		}
		virtual void setAnimation(Direction direction, std::string animationID, bool isLooping = true, float speed = 1)
		{
			this->isLooping = isLooping;
			controller.ResetProgress();
			controller.SetSpeed(speed);
			setDirection(direction, animationID);
		}
		virtual void setDirection(Direction direction, std::string animationID)
		{
			if(animation)
				animation->DeleteAnimation();

			if(!provider.animations.count(animationID))
				animationID = provider.animations.begin()->first;

			if(!provider.animations[animationID]->directions[direction] || provider.animations[animationID]->directions[direction]->GetFrameCount()==0)
				direction = North;


			animation = &provider.animations[animationID]->directions[direction]->CreateResizableObject(controller, false);
			
		}
		void loopAnimation()
		{
			if(isLooping)
			{
				controller.Play();
			}
		}
		void draw(gge::graphics::ImageTarget2D &targetLayer,
			      const gge::utils::Point &location,
				  float zoomAmount)
		{
			gge::utils::Point attachmentPoint = provider.attachmentPoint;
			animation->DrawStretched(targetLayer, (location-attachmentPoint), animation->GetSize()*zoomAmount);
		}

		gge::utils::Size getSize() const {
			if(!animation)
				return gge::utils::Size(0,0);
			else
				return animation->GetSize();
		}

	private:
		bool isLooping;
		assets::AnimationProvider &provider;
		gge::animation::Controller controller;
		gge::animation::RectangularGraphic2DAnimation *animation;
	};
}