#include "pch.h"
#include "log.h"
#include "app.h"
#include "canvas.h"
#include "canvas_actions.h"
#include "node_panel_layer.h"

void ActionStroke::undo()
{
    if (clear_layer)
        m_canvas->m_layers[m_layer_idx]->clear({ 0, 0, 0, 0 }, m_frame_idx);
    for (int i = 0; i < 6; i++)
    {
        // empty data
        if (!m_image[i])
            continue;

        LOG("undo box %d dirty=%s [%d,%d,%d,%d] to dirty=%s [%d,%d,%d,%d]",
            i,
            m_canvas->m_layers[m_layer_idx]->face(i, m_frame_idx) ? "true" : "false",
            (int)m_canvas->m_layers[m_layer_idx]->box(i, m_frame_idx).x,
            (int)m_canvas->m_layers[m_layer_idx]->box(i, m_frame_idx).y,
            (int)m_canvas->m_layers[m_layer_idx]->box(i, m_frame_idx).z,
            (int)m_canvas->m_layers[m_layer_idx]->box(i, m_frame_idx).w,
            m_old_dirty[i] ? "true" : "false",
            (int)m_old_box[i].x,
            (int)m_old_box[i].y,
            (int)m_old_box[i].z,
            (int)m_old_box[i].w);
        m_canvas->m_layers[m_layer_idx]->box(i, m_frame_idx) = m_old_box[i];
        m_canvas->m_layers[m_layer_idx]->face(i, m_frame_idx) = m_old_dirty[i];


        glm::vec2 box_sz = zw(m_box[i]) - xy(m_box[i]);
        if (box_sz.x > 0 && box_sz.y > 0 && box_sz.x <= m_canvas->m_layers[m_layer_idx]->w && box_sz.y <= m_canvas->m_layers[m_layer_idx]->h)
        {
            App::I->render_task([&]
            {
                m_canvas->m_layers[m_layer_idx]->rtt(i, m_frame_idx).updateRgba8(
                    (int)m_box[i].x,
                    (int)m_box[i].y,
                    (int)box_sz.x,
                    (int)box_sz.y,
                    m_image[i].get());
            });
        }
        else
        {
            LOG("undo invalid box size (%d, %d)", (int)box_sz.x, (int)box_sz.y);
        }
    }
}

size_t ActionStroke::memory()
{
    size_t mem = 0;
    for (int i = 0; i < 6; i++)
    {
        glm::ivec2 sz = zw(m_box[i]) - xy(m_box[i]);
        mem += sz.x * sz.y * 4 + sizeof(*this);
    }
    return mem;
}

Action* ActionStroke::get_redo()
{
    auto action = new ActionStroke;
    auto& layer = m_canvas->m_layers[m_layer_idx];
    for (int i = 0; i < 6; i++)
    {
        if (!layer->face(i, m_frame_idx) && !m_image[i])
            continue; // no stroke on this face, skip it


        auto box = clear_layer ? glm::ivec4(layer->box(i)) : m_box[i];

        // save image before commit
        glm::vec2 box_or = xy(box);
        glm::vec2 box_sz = zw(box) - xy(box);
        if (box_sz.x > 0 && box_sz.y > 0 && box_sz.x <= layer->w && box_sz.y <= layer->h)
        {
            action->m_image[i] = std::make_unique<uint8_t[]>(
                static_cast<size_t>((int)box_sz.x) * static_cast<size_t>((int)box_sz.y) * 4U);
            layer->rtt(i, m_frame_idx).readPixelsRgba8(
                static_cast<int>(box_or.x),
                static_cast<int>(box_or.y),
                static_cast<int>(box_sz.x),
                static_cast<int>(box_sz.y),
                action->m_image[i].get());
        }
        else
        {
            LOG("create_action invalid box size (%d, %d)", (int)box_sz.x, (int)box_sz.y);
        }

        action->m_box[i] = box;
        action->m_old_box[i] = layer->box(i, m_frame_idx);
        action->m_old_dirty[i] = layer->face(i, m_frame_idx);
    }
    // save history
    action->m_layer_idx = m_layer_idx;
    action->m_frame_idx = m_frame_idx;
    action->m_canvas = m_canvas;
    //action->m_stroke = std::move(m_stroke);
    action->clear_layer = false;
    return action;
}

//////////////////////////////////////////////////////////////////////////

Action* ActionLayerClear::get_redo()
{
    auto a = new ActionLayerClear;
    a->m_direction = reverse_direction();
    a->m_snap = m_snap;
    a->m_layer = m_layer;
    a->m_frame = m_frame;
    a->m_color = m_color;
    return a;
}

void ActionLayerClear::undo()
{
    if (m_direction == Direction::Undo)
    {
        m_layer->restore(*m_snap, m_frame);
    }
    else
    {
        m_layer->clear(m_color, m_frame);
    }
}

//////////////////////////////////////////////////////////////////////////

Action* ActionImportEquirect::get_redo()
{
    if (m_direction == Direction::Undo)
    {
        auto a = new ActionImportEquirect;
        a->m_direction = reverse_direction();
        a->m_snap = m_snap;
        a->m_layer = m_layer;
        a->m_frame = m_frame;
        a->m_path = m_path;
        return a;
    }
    return nullptr;
}

void ActionImportEquirect::undo()
{
    if (m_direction == Direction::Undo)
    {
        m_layer->restore(*m_snap, m_frame);
    }
    else
    {
        Canvas::I->import_equirectangular_thread(m_path, m_layer, m_frame);
    }
}
