#pragma once
#include "SpriteRenderer.hpp"
#include "GameObject.hpp"
#include "Transform.hpp"
#include "Logger.hpp"
#include "ToString.hpp"

SpriteRenderer::SpriteRenderer(GameObject* gameObject) : Component(gameObject) {
	gameObject->OnEnable += Delegate<void()>::Create<SpriteRenderer, &SpriteRenderer::registerToDrawEvent>(this);
	gameObject->OnDisable += Delegate<void()>::Create<SpriteRenderer, &SpriteRenderer::deregisterFromDrawEvent>(this);
}

void SpriteRenderer::SetSprite(const sre::Sprite& sprite) {
	this->sprite = sprite;
}

void SpriteRenderer::renderSprite(sre::SpriteBatch::SpriteBatchBuilder& spriteBatchBuilder) {
	if (!gameObject.IsActive())return;
	auto transform = gameObject.GetTransform();
	sprite.setPosition(transform->GetPosition());
	sprite.setRotation(transform->GetRotation());
	sprite.setScale(transform->GetScale());
	spriteBatchBuilder.addSprite(sprite);
}

void SpriteRenderer::registerToDrawEvent() {
	*drawEvent += Delegate<void(sre::SpriteBatch::SpriteBatchBuilder&)>::Create<SpriteRenderer, &SpriteRenderer::renderSprite>(this);
}

void SpriteRenderer::deregisterFromDrawEvent() {
	*drawEvent -= Delegate<void(sre::SpriteBatch::SpriteBatchBuilder&)>::Create<SpriteRenderer, &SpriteRenderer::renderSprite>(this);
}
