#pragma once

#include "collision.h"
#include "vector2d.h"
#include "utils.h"
#include "colour.h"

namespace Halley {

	namespace Animation {
		class Player;
	}

	class Frame;
	class String;

	class Sprite {
		friend class Renderer;

	public:
		Sprite();
		virtual ~Sprite();

		bool isAlive() const { return alive; }
		void setAlive(bool a) { alive = a; }

		bool step(float time);
		virtual void render(Renderer& renderer) const;

		Collision::Type getColType() const { return colType; }
		int getLayer() const { return layer; }
		int getTextureId() const { return texId; }
		int getRenderType() const { return renderType; }
		float getAngle() const { return angle; }
		float getScale() const { return scale; }
		Vector2f getPos() const { return pos; }
		Vector2f getPivot() const { return pivot; }
		bool isParticle() const { return particle; }

		void setPos(Vector2f p) { pos = p; }
		void setPivot(Vector2f p) { pivot = p; }
		void setLayer(int n) { layer = n; }

		void setParent(Frame* frame);

		bool operator<(const Sprite& b) const { return pos.y < b.pos.y; }
		void addTrigger(Collision::Mask mask, Collision::Trigger trigger);
		Collision::TriggerMap& getTriggers();
		bool hasTriggers() const { return hasTrigger; }

	protected:
		Vector2f pos;
		Vector2f pivot;
		float angle;
		float scale;
		int texId;
		int renderType;
		int layer;
		bool particle;
		bool alive;
		bool hasTrigger;
		bool flip;
		Colour colour;
		SpriteIter iter;
		Collision::Type colType;
		shared_ptr<Animation::Player> anim;

		virtual bool step(float deltaTime, float curTime)=0;
		Frame& getFrame() const;

		void playSound(String name, float volume=1.0f); 
		bool isInsideField(float tolerance) const;

		void setAnimationGroup(String name);
		void setAnimationSequence(String name);
		void setAnimationDirection(int n);

	private:
		float curTime;
		Collision::TriggerMap triggers;
		
		Frame* frame;
	};

}
