#include "pch.h"
#include "app.h"
#include "canvas.h"
#include "log.h"
#include "renderer_gl/opengl_capabilities.h"

namespace {

void enable_opengl_state(std::uint32_t state) noexcept
{
    glEnable(static_cast<GLenum>(state));
}

void disable_opengl_state(std::uint32_t state) noexcept
{
    glDisable(static_cast<GLenum>(state));
}

void set_opengl_blend_func(std::uint32_t source_factor, std::uint32_t destination_factor) noexcept
{
    glBlendFunc(static_cast<GLenum>(source_factor), static_cast<GLenum>(destination_factor));
}

void set_opengl_blend_equation(std::uint32_t equation) noexcept
{
    glBlendEquation(static_cast<GLenum>(equation));
}

void apply_convert_command_state()
{
    const auto status = pp::renderer::gl::apply_panopainter_convert_command_state(
        pp::renderer::gl::OpenGlConvertCommandStateDispatch {
            .enable = enable_opengl_state,
            .disable = disable_opengl_state,
            .blend_func = set_opengl_blend_func,
            .blend_equation = set_opengl_blend_equation,
        });
    if (!status.ok())
        LOG("OpenGL convert command state failed: %s", status.message);
}

}

void App::cmd_convert(std::string pano_path, std::string out_path)
{
    apply_convert_command_state();

    Canvas* command_canvas = new Canvas;
    const int canvas_resolution = default_canvas_resolution();
    command_canvas->create(canvas_resolution, canvas_resolution);
    command_canvas->project_open_thread(pano_path);
    command_canvas->export_equirectangular_thread(out_path);
}
