#include "DeerStudio/StudioAPI/FrameBuffer.h"
#include "DeerRender/FrameBuffer.h"

namespace Deer {
	namespace StudioAPI {

		int FrameBufferStruct::getWidth() {
			Resource<FrameBuffer> frameBufferResource = Resource<FrameBuffer>::unsafeFromId(frameBufferId);
			FrameBuffer& frameBuffer = frameBufferResource.getData();
			return frameBuffer.getSpecification().width;
		}

		int FrameBufferStruct::getHeight() {
			Resource<FrameBuffer> frameBufferResource = Resource<FrameBuffer>::unsafeFromId(frameBufferId);
			FrameBuffer& frameBuffer = frameBufferResource.getData();
			return frameBuffer.getSpecification().height;
		}

		void FrameBufferStruct::resize(int x, int y) {
			Resource<FrameBuffer> frameBufferResource = Resource<FrameBuffer>::unsafeFromId(frameBufferId);
			FrameBuffer& frameBuffer = frameBufferResource.getData();

			frameBuffer.resize(x, y);
		}

		void FrameBufferStruct::clearRGBA(int r, int g, int b, int a) {

			Resource<FrameBuffer> frameBufferResource = Resource<FrameBuffer>::unsafeFromId(frameBufferId);
			FrameBuffer& frameBuffer = frameBufferResource.getData();

			frameBuffer.bind();
			frameBuffer.clear();
			uint8_t clearColor[4]{(uint8_t)r, (uint8_t)g, (uint8_t)b, (uint8_t)a};
			frameBuffer.clearBuffer(0, &clearColor);
			frameBuffer.unbind();
		}

		std::string FrameBufferStruct::getName() {
			return Resource<FrameBuffer>::unsafeFromId(frameBufferId).getStorageId();
		}

		FrameBufferStruct createLoadRGBA8FrameBuffer(std::string& name, int sizeX, int sizeY, int samples) {
			FrameBufferData fbd(sizeX, sizeY, samples, FrameBufferData::FrameBufferType::RGBA8);
			Resource<FrameBuffer> frameBuffer = ResourceManager<FrameBuffer>::loadResourceFromData(fbd, name);

			return FrameBufferStruct(frameBuffer.getResourceId());
		}

		void frameBuffer_constructor(FrameBufferHandleStruct* mem) {
			new (mem) FrameBufferHandleStruct(0);
		}

		bool FrameBufferStruct::isValid() {
			return frameBufferId >= 0 && frameBufferId < 100;
		}
	} // namespace StudioAPI
} // namespace Deer