#include "pch.h"
#include "log.h"
#include "node_dialog_resize.h"
#include "app_core/document_resize.h"
#include "canvas.h"
#include "node_image_texture.h"
#include <array>

Node* NodeDialogResize::clone_instantiate() const
{
    return new NodeDialogResize();
}

void NodeDialogResize::clone_finalize(Node* dest) const
{
    NodeDialogResize* n = static_cast<NodeDialogResize*>(dest);
    n->init_controls();
}

void NodeDialogResize::init()
{
    init_template_file("data/dialogs/doc-resize.xml", "dialog-resize");
    init_controls();
}

void NodeDialogResize::init_controls()
{
    btn_ok = find<NodeButton>("btn-ok");
    btn_cancel = find<NodeButton>("btn-cancel");
    combo = find<NodeComboBox>("resolution");
    text = find<NodeText>("current-res");
    resolution = Canvas::I->m_width;
    const auto state = pp::app::make_document_resize_dialog_state(resolution);
    text->set_text(state.current_resolution_text.c_str());
    if (combo && state.current_resolution_index >= 0
        && state.current_resolution_index < static_cast<int>(combo->m_items.size())) {
        combo->m_current_index = state.current_resolution_index;
    }
}

void NodeDialogResize::loaded()
{
//    Image thumb = Canvas::I->thumbnail_read(data_path);
//    auto image_tex = find<NodeImageTexture>("thumb-tex");
//    image_tex->tex.create(thumb);
}

int NodeDialogResize::get_resolution()
{
    const auto plan = pp::app::plan_document_resize(combo ? combo->m_current_index : 0);
    return plan ? plan.value().resolution : pp::app::document_resolution_values.front();
}
