#pragma once
#include <tuple>

#include "pixel.h"
#include "sokol_gfx.h"

/**
 * @brief 像素格式工厂
 *
 * 提供从sg_pixel_format到对应像素类型的转换功能
 */
struct image_accessor_factory {
    /**
     * @brief 获取与sg_pixel_format对应的像素类型信息
     *
     * @param format 像素格式
     * @return std::tuple<const char*, size_t, int> 类型名称、元素大小和通道数
     */
    static std::tuple<const char*, size_t, int> get_pixel_type_info(const sg_pixel_format& format) {
        switch (format) {
            // 8位单通道格式
            case SG_PIXELFORMAT_R8:
                return {"uint8_t", sizeof(uint8_t), 1};
            case SG_PIXELFORMAT_R8SN:
                return {"int8_t", sizeof(int8_t), 1};
            case SG_PIXELFORMAT_R8UI:
                return {"uint8_t", sizeof(uint8_t), 1};
            case SG_PIXELFORMAT_R8SI:
                return {"int8_t", sizeof(int8_t), 1};

            // 16位单通道和8位双通道格式
            case SG_PIXELFORMAT_R16:
                return {"uint16_t", sizeof(uint16_t), 1};
            case SG_PIXELFORMAT_R16SN:
                return {"int16_t", sizeof(int16_t), 1};
            case SG_PIXELFORMAT_R16UI:
                return {"uint16_t", sizeof(uint16_t), 1};
            case SG_PIXELFORMAT_R16SI:
                return {"int16_t", sizeof(int16_t), 1};
            case SG_PIXELFORMAT_R16F:
                return {"half", sizeof(Eigen::half), 1};
            case SG_PIXELFORMAT_RG8:
                return {"uint8_t", sizeof(uint8_t), 2};
            case SG_PIXELFORMAT_RG8SN:
                return {"int8_t", sizeof(int8_t), 2};
            case SG_PIXELFORMAT_RG8UI:
                return {"uint8_t", sizeof(uint8_t), 2};
            case SG_PIXELFORMAT_RG8SI:
                return {"int8_t", sizeof(int8_t), 2};

            // 32位单通道和16位双通道和8位四通道格式
            case SG_PIXELFORMAT_R32UI:
                return {"uint32_t", sizeof(uint32_t), 1};
            case SG_PIXELFORMAT_R32SI:
                return {"int32_t", sizeof(int32_t), 1};
            case SG_PIXELFORMAT_R32F:
                return {"float", sizeof(float), 1};
            case SG_PIXELFORMAT_RG16:
                return {"uint16_t", sizeof(uint16_t), 2};
            case SG_PIXELFORMAT_RG16SN:
                return {"int16_t", sizeof(int16_t), 2};
            case SG_PIXELFORMAT_RG16UI:
                return {"uint16_t", sizeof(uint16_t), 2};
            case SG_PIXELFORMAT_RG16SI:
                return {"int16_t", sizeof(int16_t), 2};
            case SG_PIXELFORMAT_RG16F:
                return {"half", sizeof(Eigen::half), 2};
            case SG_PIXELFORMAT_RGBA8:
            case SG_PIXELFORMAT_SRGB8A8:
            case SG_PIXELFORMAT_BGRA8:
                return {"uint8_t", sizeof(uint8_t), 4};
            case SG_PIXELFORMAT_RGBA8SN:
                return {"int8_t", sizeof(int8_t), 4};
            case SG_PIXELFORMAT_RGBA8UI:
                return {"uint8_t", sizeof(uint8_t), 4};
            case SG_PIXELFORMAT_RGBA8SI:
                return {"int8_t", sizeof(int8_t), 4};

            // 64位双通道和32位四通道格式
            case SG_PIXELFORMAT_RG32UI:
                return {"uint32_t", sizeof(uint32_t), 2};
            case SG_PIXELFORMAT_RG32SI:
                return {"int32_t", sizeof(int32_t), 2};
            case SG_PIXELFORMAT_RG32F:
                return {"float", sizeof(float), 2};
            case SG_PIXELFORMAT_RGBA16:
                return {"uint16_t", sizeof(uint16_t), 4};
            case SG_PIXELFORMAT_RGBA16SN:
                return {"int16_t", sizeof(int16_t), 4};
            case SG_PIXELFORMAT_RGBA16UI:
                return {"uint16_t", sizeof(uint16_t), 4};
            case SG_PIXELFORMAT_RGBA16SI:
                return {"int16_t", sizeof(int16_t), 4};
            case SG_PIXELFORMAT_RGBA16F:
                return {"half", sizeof(Eigen::half), 4};

            // 128位四通道格式
            case SG_PIXELFORMAT_RGBA32UI:
                return {"uint32_t", sizeof(uint32_t), 4};
            case SG_PIXELFORMAT_RGBA32SI:
                return {"int32_t", sizeof(int32_t), 4};
            case SG_PIXELFORMAT_RGBA32F:
                return {"float", sizeof(float), 4};

            // 特殊格式和压缩格式不直接支持
            default:
                return {"unknown", 0, 0};
        }
    }

    /**
     * @brief 判断是否可以直接支持该像素格式
     *
     * @param format 像素格式
     * @return bool 如果格式可以用pixel<T,N>表示则返回true
     */
    static bool is_supported(const sg_pixel_format format) {
        auto [type_name, element_size, channels] = get_pixel_type_info(format);
        return element_size > 0 && channels > 0;
    }

    /**
     * @brief 创建新的图像访问器
     *
     * 根据像素格式创建适当类型的图像访问器
     *
     * @param format 像素格式
     * @param data 图像数据指针
     * @param width 图像宽度
     * @param height 图像高度
     * @return void* 创建的图像访问器指针(需要根据格式自行转换为正确类型)
     */
    // static image_accessor_interface* create_image_accessor(const sg_pixel_format format, void* data, int width, int height) {
    //     // 根据格式创建对应类型的图像访问器
    //     switch (format) {
    //         case SG_PIXELFORMAT_R8:
    //             return new image_accessor<pixel<uint8_t, 1>>(data, width, height);
    //         case SG_PIXELFORMAT_R8SN:
    //             return new image_accessor<pixel<int8_t, 1>>(data, width, height);
    //         case SG_PIXELFORMAT_RG8:
    //             return new image_accessor<pixel<uint8_t, 2>>(data, width, height);
    //         case SG_PIXELFORMAT_RG8SN:
    //             return new image_accessor<pixel<int8_t, 2>>(data, width, height);
    //         case SG_PIXELFORMAT_RGBA8:
    //         case SG_PIXELFORMAT_SRGB8A8:
    //             return new image_accessor<pixel<uint8_t, 4>>(data, width, height);
    //         case SG_PIXELFORMAT_RGBA8SN:
    //             return new image_accessor<pixel<int8_t, 4>>(data, width, height);
    //         case SG_PIXELFORMAT_BGRA8:
    //             return new image_accessor<pixel<uint8_t, 4>>(data, width, height); // 需要单独处理RGB顺序
    //
    //         case SG_PIXELFORMAT_R16:
    //             return new image_accessor<pixel<uint16_t, 1>>(data, width, height);
    //         case SG_PIXELFORMAT_R16SN:
    //             return new image_accessor<pixel<int16_t, 1>>(data, width, height);
    //         case SG_PIXELFORMAT_R16F:
    //             return new image_accessor<pixel<Eigen::half, 1>>(data, width, height);
    //         case SG_PIXELFORMAT_RG16:
    //             return new image_accessor<pixel<uint16_t, 2>>(data, width, height);
    //         case SG_PIXELFORMAT_RG16SN:
    //             return new image_accessor<pixel<int16_t, 2>>(data, width, height);
    //         case SG_PIXELFORMAT_RG16F:
    //             return new image_accessor<pixel<Eigen::half, 2>>(data, width, height);
    //         case SG_PIXELFORMAT_RGBA16:
    //             return new image_accessor<pixel<uint16_t, 4>>(data, width, height);
    //         case SG_PIXELFORMAT_RGBA16SN:
    //             return new image_accessor<pixel<int16_t, 4>>(data, width, height);
    //         case SG_PIXELFORMAT_RGBA16F:
    //             return new image_accessor<pixel<Eigen::half, 4>>(data, width, height);
    //
    //         case SG_PIXELFORMAT_R32F:
    //             return new image_accessor<pixel<float, 1>>(data, width, height);
    //         case SG_PIXELFORMAT_RG32F:
    //             return new image_accessor<pixel<float, 2>>(data, width, height);
    //         case SG_PIXELFORMAT_RGBA32F:
    //             return new image_accessor<pixel<float, 4>>(data, width, height);
    //
    //         default:
    //             return nullptr;
    //     }
    // }

    /**
     * @brief 删除图像访问器
     *
     * @param accessor 图像访问器指针
     */
    // static void destroy_image_accessor(image_accessor_interface* accessor) {
    // 	delete accessor;
    // }
};

