#ifndef _H_COMPONENT_H
#define _H_COMPONENT_H

#include "graphics/mesh.h"
#include "graphics/sprite.h"
#include "graphics/shader.h"
#include "graphics/animation.h"
#include "gameplay/ai.h"
#include "events.h"

#include <glm/gtc/type_ptr.hpp>

class Component 
{
public:
	Component(const std::shared_ptr<EventManager>& eventManager) : eventManager(eventManager) {}
	virtual ~Component() {};
	virtual void bind() = 0;
	virtual void update() = 0;
	virtual void render() = 0;
	virtual void play() = 0;
	virtual void idle() = 0;

	int ownerid = 0;
private:
	std::shared_ptr<EventManager> eventManager;
};

class MeshComponent : public Component
{
public:
	MeshComponent(Mesh* mesh, const std::shared_ptr<EventManager>& eventManager) : Component(eventManager), mesh(mesh) {};

	void bind() override {}
	void update() override {}
	void render() override {
		if (mesh) mesh->draw();
	}
	void play() override { /*unused*/ }
	void idle() override { /*unused*/ }
	

	~MeshComponent() { mesh->~Mesh(); }

private:
	Mesh* mesh;

};

class SpriteComponent : public Component
{
public:
	SpriteComponent(std::shared_ptr<Sprite> sprite) : sprite(sprite), Component(nullptr) {}

	void bind() override {
		if (sprite) sprite->bind();
	}
	void update() override { /*unused*/ }
	void render() override {
		if (sprite) sprite->draw();
	}
	void play() override { /*unused*/ }
	void idle() override { /*unused*/ }

	std::shared_ptr<Sprite>& getSprite() { return sprite; }

	~SpriteComponent() { /*sprite->~Sprite();*/ }

private:
	std::shared_ptr<Sprite> sprite;
};

class AnimationComponent : public Component
{
public:
	AnimationComponent(std::shared_ptr<AnimationSet> animSet) : animSet(animSet), Component(nullptr) {};
	AnimationComponent(std::shared_ptr<AnimationSet> animSet, const std::shared_ptr<EventManager>& eventManager) :
		animSet(animSet), Component(nullptr)
	{
		this->animSet->attachEventManager(eventManager);
	}

	void bind() override {
		if (animSet)
			animSet->bind();
	}
	void update() override {/*unused*/}
	void render() override { 
		if (animSet)
			animSet->draw(); 
	}
	void play() override { 
		if (animSet)
			animSet->play(); 
	}
	void idle() override { 
		if (animSet)
			animSet->stop(); 
	}

	std::shared_ptr<AnimationSet>& getAnimationSet() { return animSet; }

	~AnimationComponent() {  }

private:
	std::shared_ptr<AnimationSet> animSet;
};

class ShaderComponent : public Component
{
public:
	ShaderComponent(const char* vertexPath, const char* shaderPath) :
		shader(vertexPath, shaderPath), Component(nullptr) {};

	void bind() override {
		shader.use();
	}
	void update() override {}
	void render() override {}
	void play() override { /*unused*/ }
	void idle() override { /*unused*/ }

	Shader& getShader() { return shader; }
			
	~ShaderComponent() { }

private:
	Shader shader;
};

class AIComponent : public Component {
public:
	AIComponent(std::shared_ptr<AI> ai) : ai(ai), Component(nullptr) {}

	void bind() override { /*unused*/ }
	void update() override { if (ai) ai->update(); }
	void render() override { /*unused*/ }
	void play() override { /*unused*/ }
	void idle() override { /*unused*/ }

private:
	std::shared_ptr<AI> ai;
};

#endif // _H_COMPONENT_H