#pragma once
#include <Eigen/Eigen>
#include <vector>

#include "renderer.h"

class renderer_texture {
public:
    explicit renderer_texture(const texture_format in_format) : format(in_format) {}
    virtual ~renderer_texture() = default;

    virtual void* lock(uint32_t* out_row_pitch) = 0;
    virtual void unlock() = 0;

    virtual void update_subresource(const Eigen::AlignedBox2i& in_area, const void* in_buffer, uint32_t in_row_pitch) = 0;

    virtual bool resize(const Eigen::Vector2i& size) = 0;
    virtual Eigen::Vector2i size() = 0;

    virtual void* get_native_handle() = 0;
    virtual void* get_shader_resource_view() = 0;
    [[nodiscard]] texture_format get_format() const { return format; }

    static void convert_rgb_to_rgba(const unsigned char* rgb, unsigned char* rgba, int width, int height) {
        for (int i = 0; i < width * height; ++i) {
            rgba[i * 4 + 0] = rgb[i * 3 + 0]; // R
            rgba[i * 4 + 1] = rgb[i * 3 + 1]; // G
            rgba[i * 4 + 2] = rgb[i * 3 + 2]; // B
            rgba[i * 4 + 3] = 255; // A (完全不透明)
        }
    }
private:
    const texture_format format;
};

class texture_format_converter {
public:
    texture_format_converter(Eigen::Vector2i size, texture_format in_format, texture_format out_format) : size(size), in_format(in_format), out_format(out_format) {}

private:
    Eigen::Vector2i size;
    texture_format in_format;
    texture_format out_format;
};
