#include "pch.h"
#include "app.h"
#include "app_core/main_toolbar.h"
#include "legacy_app_shell_services.h"
#include "legacy_history_services.h"
#include "node_button.h"
#include "node_button_custom.h"

namespace {

void execute_main_toolbar_plan(App& app, const pp::app::MainToolbarPlan& plan)
{
    pp::panopainter::execute_legacy_main_toolbar_plan(app, plan);
}

} // namespace

namespace pp::panopainter {

void bind_legacy_main_toolbar(App& app)
{
    if (auto* button = app.layout[app.main_id]->find<NodeButton>("btn-anim"))
    {
        button->on_click = [&app, button](Node*) {
            if (app.canvas)
            {
                //app.canvas->m_canvas->export_anim();
            }
        };
    }
    if (auto* button = app.layout[app.main_id]->find<NodeButton>("btn-open"))
    {
        button->on_click = [&app, button](Node*) {
            const auto plan = pp::app::plan_main_toolbar_command(
                pp::app::MainToolbarCommand::open_document);
            if (plan)
                execute_main_toolbar_plan(app, plan.value());
        };
    }
    if (auto* button = app.layout[app.main_id]->find<NodeButton>("btn-save"))
    {
        button->on_click = [&app, button](Node*) {
            const auto plan = pp::app::plan_main_toolbar_command(
                pp::app::MainToolbarCommand::save_document);
            if (plan)
                execute_main_toolbar_plan(app, plan.value());
        };
    }
    if (auto* button = app.layout[app.main_id]->find<NodeButtonCustom>("btn-undo"))
    {
        button->on_click = [&app, button](Node*) {
            const auto history = pp::panopainter::legacy_history_snapshot();
            const auto plan = pp::app::plan_main_toolbar_command(
                pp::app::MainToolbarCommand::undo,
                history.undo_count);
            if (plan)
                execute_main_toolbar_plan(app, plan.value());
        };
    }
    if (auto* button = app.layout[app.main_id]->find<NodeButtonCustom>("btn-redo"))
    {
        button->on_click = [&app, button](Node*) {
            const auto history = pp::panopainter::legacy_history_snapshot();
            const auto plan = pp::app::plan_main_toolbar_command(
                pp::app::MainToolbarCommand::redo,
                0,
                history.redo_count);
            if (plan)
                execute_main_toolbar_plan(app, plan.value());
        };
    }
    if (auto* button = app.layout[app.main_id]->find<NodeButtonCustom>("btn-clean-memory"))
    {
        button->on_click = [&app](Node*) {
            const auto history = pp::panopainter::legacy_history_snapshot();
            const auto plan = pp::app::plan_main_toolbar_command(
                pp::app::MainToolbarCommand::clear_history,
                history.undo_count,
                history.redo_count,
                history.memory_bytes);
            if (plan)
                execute_main_toolbar_plan(app, plan.value());
        };
    }
    if (auto* button = app.layout[app.main_id]->find<NodeButton>("btn-clear"))
    {
        button->on_click = [&app](Node*) {
            const auto plan = pp::app::plan_main_toolbar_command(
                pp::app::MainToolbarCommand::clear_canvas,
                0,
                0,
                0,
                static_cast<bool>(app.canvas));
            if (plan)
                execute_main_toolbar_plan(app, plan.value());
        };
    }
    if (auto* button = app.layout[app.main_id]->find<NodeButton>("btn-popup"))
    {
        button->on_click = [&app](Node*) {
            const auto plan = pp::app::plan_main_toolbar_command(
                pp::app::MainToolbarCommand::show_message_box);
            if (plan)
                execute_main_toolbar_plan(app, plan.value());
        };
    }
    if (auto* button = app.layout[app.main_id]->find<NodeButtonCustom>("btn-settings"))
    {
        button->on_click = [&app](Node*) {
            const auto plan = pp::app::plan_main_toolbar_command(
                pp::app::MainToolbarCommand::show_settings);
            if (plan)
                execute_main_toolbar_plan(app, plan.value());
        };
    }
}

} // namespace pp::panopainter
