#include "PluginToolButton.h"

#include "config-features.h"  // for ENABLE_PLUGINS

#ifdef ENABLE_PLUGINS

#include <utility>  // for move

#include "plugin/Plugin.h"  // for ToolbarButtonEntry
#include "util/glib_casts.h"
#include "util/gtk4_helper.h"


PluginToolButton::PluginToolButton(ToolbarButtonEntry* t):
        AbstractToolItem(std::move(t->toolbarId), Category::PLUGINS), t(t) {}

PluginToolButton::~PluginToolButton() = default;

auto PluginToolButton::createItem(bool) -> xoj::util::WidgetSPtr {
    GtkWidget* btn = gtk_button_new();
    gtk_widget_set_can_focus(btn, false);  // todo(gtk4) not necessary anymore

    gtk_button_set_relief(GTK_BUTTON(btn), GTK_RELIEF_NONE);
    gtk_button_set_icon_name(GTK_BUTTON(btn), t->iconName.c_str());
    gtk_widget_set_tooltip_text(btn, t->description.c_str());

    g_signal_connect(btn, "clicked", G_CALLBACK(+[](GtkButton*, gpointer d) {
                         auto* te = static_cast<ToolbarButtonEntry*>(d);
                         te->plugin->executeToolbarButton(te);
                     }),
                     this->t);

    GtkToolItem* it = gtk_tool_item_new();
    gtk_container_add(GTK_CONTAINER(it), btn);

    auto createProxy = [this]() {
        GtkWidget* proxy = gtk_menu_item_new();

        auto* box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
        gtk_container_add(GTK_CONTAINER(proxy), box);
        gtk_box_append(GTK_BOX(box), getNewToolIcon());
        gtk_box_append(GTK_BOX(box), gtk_label_new(getToolDisplayName().c_str()));

        g_signal_connect(proxy, "activate", G_CALLBACK(+[](GtkMenuItem*, gpointer d) {
                             auto* te = static_cast<ToolbarButtonEntry*>(d);
                             te->plugin->executeToolbarButton(te);
                         }),
                         this->t);

        return proxy;
    };
    gtk_tool_item_set_proxy_menu_item(it, "", createProxy());

    return xoj::util::WidgetSPtr(GTK_WIDGET(it), xoj::util::adopt);
}

auto PluginToolButton::getToolDisplayName() const -> std::string { return this->t->description; }

auto PluginToolButton::getNewToolIcon() const -> GtkWidget* {
    return gtk_image_new_from_icon_name(t->iconName.c_str(), GTK_ICON_SIZE_SMALL_TOOLBAR);
}

#endif /* ENABLE_PLUGINS */
