#pragma once
#include "Behaviour.hpp"
#include "Event_forward.hpp"
#include "sre/SDLRenderer.hpp"
#include <SDL_events.h>

class PhysicsBody;
class SpriteRenderer;
class CharacterAnimator;

class CharacterController : public Behaviour {
public:
	CharacterController(GameObject* gameObject);

	bool left = false;
	bool right = false;
	bool jump = false;
	bool isGrounded = false;
	
	class Mutator {
	private:
		inline static void SetKeyEvent(CharacterController& target, Event<void(SDL_Event&)>* keyEvent) {
			target.keyEvent = keyEvent;
		}
		friend class LevelBuilder;
	};

protected:
	virtual void Update(float deltaTime) override;
	virtual void InitialiseSelf() override;
	virtual void Initialise() override{}
	
private:
	std::shared_ptr<PhysicsBody> physicsBody;
	std::shared_ptr<CharacterAnimator> animator;

	float directionalForceAmount = 10000.0f;
	float jumpForceAmount = 100000.0f;
	float maxVelocity = 50.0f;
	glm::vec2 playerSpawn;
	glm::vec2 newDirectionalForce = glm::vec2(0.0f, 0.0f);

	void registerEvents();
	void deregisterEvents();

	Event<void(SDL_Event&)>* keyEvent;

	void OnKey(SDL_Event&);
	void OnCollisionEnter(GameObject&);
	void OnCollisionExit(GameObject&);

	friend class Mutator;
};