#include "pch.h"
#include "app.h"
#include "node_text.h"
#include "node_panel_floating.h"
#include "app_core/about_menu.h"
#include "app_core/app_preferences.h"
#include "app_core/document_layer.h"
#include "app_core/document_canvas.h"
#include "app_core/app_status.h"
#include "app_core/tools_menu.h"
#include "legacy_app_preference_services.h"
#include "legacy_app_shell_services.h"
#include "legacy_document_layer_services.h"
#include "legacy_preference_storage.h"
#include "font.h"

namespace pp::panopainter {
void bind_legacy_main_toolbar(App& app);
void bind_legacy_file_menu(App& app);
void bind_legacy_edit_menu(App& app);
void bind_legacy_about_menu(App& app);
void bind_legacy_layer_menu(App& app);
void bind_legacy_tools_menu(App& app);
void init_layout_bootstrap(App& app);
}

namespace {

[[nodiscard]] bool should_open_tools_panel(const pp::app::ToolsPanelPlan& plan) noexcept
{
    return plan.action == pp::app::ToolsPanelAction::open_floating_panel;
}

void apply_tools_panel_chrome(NodePanelFloating& panel, const pp::app::ToolsPanelPlan& plan)
{
    if (plan.width > 0 && plan.height > 0) {
        panel.SetSize(static_cast<float>(plan.width), static_cast<float>(plan.height));
    } else {
        if (plan.width > 0)
            panel.SetWidth(static_cast<float>(plan.width));
        if (plan.height > 0)
            panel.SetHeight(static_cast<float>(plan.height));
    }
    if (plan.min_width > 0)
        panel.SetMinWidth(static_cast<float>(plan.min_width));
    if (plan.min_height > 0)
        panel.SetMinHeight(static_cast<float>(plan.min_height));
    panel.m_title->set_text(plan.title.data());
    panel.m_droppable = plan.droppable;
}

void execute_tools_menu_plan(App& app, const pp::app::ToolsMenuPlan& plan)
{
    pp::panopainter::execute_legacy_tools_menu_plan(app, plan);
}

void execute_document_layer_merge_plan(App& app, const pp::app::DocumentLayerMergePlan& plan)
{
    const auto status = pp::panopainter::execute_legacy_document_layer_merge_plan(app, plan);
    if (!status.ok())
        LOG("Layer merge failed: %s", status.message);
}

} // namespace

void App::title_update()
{
    if (auto docname = layout[main_id]->find<NodeText>("txt-docname"))
    {
        const auto title = pp::app::make_document_title(
            doc_name,
            canvas->m_canvas->m_unsaved,
            canvas->m_canvas->m_width);
        docname->set_text(title.c_str());
    }
    if (auto node = layout[main_id]->find<NodeText>("txt-dpi"))
    {
        const auto label = pp::app::make_dpi_label(zoom);
        node->set_text(label.c_str());
    }
}

void App::init_toolbar_main()
{
    pp::panopainter::bind_legacy_main_toolbar(*this);
}

void App::init_menu_file()
{
    pp::panopainter::bind_legacy_file_menu(*this);
}

void App::init_menu_edit()
{
    pp::panopainter::bind_legacy_edit_menu(*this);
}

void App::init_menu_tools()
{
    pp::panopainter::bind_legacy_tools_menu(*this);
}

void App::init_menu_about()
{
    pp::panopainter::bind_legacy_about_menu(*this);
}

void App::init_menu_layer()
{
    pp::panopainter::bind_legacy_layer_menu(*this);
}

void App::initLayout()
{
    pp::panopainter::init_layout_bootstrap(*this);
}

void App::set_ui_scale(float scale)
{
    const auto plan = pp::app::plan_ui_scale(scale, display_density);
    zoom = plan.scale;
    FontManager::change_scale(plan.font_scale);
    pp::panopainter::save_legacy_ui_scale_preference(plan.scale);
    App::I->title_update();
}
