#pragma once

#include "texture_backend.h"
#include <glad/gles2.h>
#include <glad/egl.h>

/**
 * OpenGL ES implementation of ITextureBackend.
 * Imports AHardwareBuffer via EGL image and blits to a standard GL_TEXTURE_2D.
 */
class OpenGLTextureBackend : public ITextureBackend
{
public:
    OpenGLTextureBackend() = default;
    ~OpenGLTextureBackend() override { Destroy(); }

    bool Create(AHardwareBuffer* buffer) override;
    void Update() override;
    void Destroy() override;
    void* GetNativeTexturePtr() override;
    int GetWidth() const override { return m_Width; }
    int GetHeight() const override { return m_Height; }
    bool IsVulkan() const override { return false; }

    /**
     * Initialize GLAD for OpenGL ES.
     * Must be called on the render thread before Create().
     * @return true if initialization succeeded
     */
    static bool InitializeGLAD();

private:
    void Blit();

    GLuint m_SourceTexture = 0;      // GL_TEXTURE_EXTERNAL_OES from HardwareBuffer
    GLuint m_SourceFramebuffer = 0;
    GLuint m_DestTexture = 0;        // GL_TEXTURE_2D for Unity
    GLuint m_DestFramebuffer = 0;
    GLint m_Width = 0;
    GLint m_Height = 0;
    bool m_Created = false;
};
