#include "DeerStudio/EditorEngine/API/FrameBuffer.h"
#include "DeerRender/FrameBuffer.h"

namespace Deer {
    namespace EditorEngine {

        int FrameBufferStruct::getWidth() {
            return FrameBufferManager::getFrameBufferWidth(frameBufferId);
        }

        int FrameBufferStruct::getHeight() {
            return FrameBufferManager::getFrameBufferWidth(frameBufferId);
        }

        void FrameBufferStruct::resize(int x, int y) {
            FrameBufferManager::resizeFrameBuffer(frameBufferId, x, y);
        }

        void FrameBufferStruct::clearRGBA(int r, int g, int b, int a) {
            FrameBuffer& frameBuffer = FrameBufferManager::getFrameBuffer(frameBufferId);
            
            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 FrameBufferManager::getFrameBufferName(frameBufferId);
        }

        FrameBufferStruct createRGBA8FrameBuffer(std::string& name, int x, int y) {
            uint16_t id = FrameBufferManager::createRGBA8FrameBuffer(name, x, y);
            return FrameBufferStruct(id);
        }

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

        bool FrameBufferStruct::isValid() {
            return frameBufferId > 0 && frameBufferId < 100;
        }

        FrameBufferStruct getFrameBuffer(std::string& name) {
            return FrameBufferManager::getFrameBufferId(name);
        }
    }
}