#include "ui/toolbar.h"
#include "ui/menu.h"
#include "ui/tabs.h"
#include "web/navigation.h"

static void on_back_clicked(GtkButton *button, TabManager *manager) {
  WebKitWebView *webview = tab_manager_get_current_webview(manager);
  if (webview)
    webkit_web_view_go_back(webview);
}

static void on_forward_clicked(GtkButton *button, TabManager *manager) {
  WebKitWebView *webview = tab_manager_get_current_webview(manager);
  if (webview)
    webkit_web_view_go_forward(webview);
}

static void on_reload_clicked(GtkButton *button, TabManager *manager) {
  WebKitWebView *webview = tab_manager_get_current_webview(manager);
  if (webview)
    webkit_web_view_reload(webview);
}

static void on_home_clicked(GtkButton *button, Profile *profile) {
  // We need the manager to get the webview
  TabManager *manager = g_object_get_data(G_OBJECT(button), "manager");
  WebKitWebView *webview = tab_manager_get_current_webview(manager);
  if (webview)
    navigation_load_home(webview, profile);
}

static gboolean select_all_text_idle(gpointer user_data) {
  GtkEntry *entry = GTK_ENTRY(user_data);
  gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
  return G_SOURCE_REMOVE;
}

static gboolean on_url_focus_in(GtkEntry *entry, GdkEventFocus *event,
                                gpointer user_data) {
  // Use idle callback to ensure selection happens after GTK's default handling
  g_idle_add(select_all_text_idle, entry);
  return FALSE; // Allow other handlers to run
}

static gboolean on_url_icon_press(GtkEntry *entry,
                                  GtkEntryIconPosition icon_pos,
                                  GdkEvent *event, gpointer user_data) {
  // Also select all when clicking the entry icons
  g_idle_add(select_all_text_idle, entry);
  return FALSE;
}

static void on_url_activate(GtkEntry *entry, gpointer user_data) {
  TabManager *manager = g_object_get_data(G_OBJECT(entry), "manager");
  Profile *profile = g_object_get_data(G_OBJECT(entry), "profile");
  WebKitWebView *webview = tab_manager_get_current_webview(manager);
  const char *url = gtk_entry_get_text(entry);
  if (webview)
    navigation_load_url_with_profile(webview, url, profile);
}

GtkWidget *create_toolbar(TabManager *manager, Profile *profile,
                          GtkEntry **url_entry_out) {
  GtkWidget *toolbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
  gtk_widget_set_name(toolbar, "browser-toolbar");

  GtkStyleContext *context = gtk_widget_get_style_context(toolbar);
  gtk_style_context_add_class(context, "browser-toolbar");

  gtk_container_set_border_width(GTK_CONTAINER(toolbar), 5);

  // Back Button
  GtkWidget *btn_back = gtk_button_new_from_icon_name("go-previous-symbolic",
                                                      GTK_ICON_SIZE_BUTTON);
  gtk_box_pack_start(GTK_BOX(toolbar), btn_back, FALSE, FALSE, 0);
  g_signal_connect(btn_back, "clicked", G_CALLBACK(on_back_clicked), manager);

  // Forward Button
  GtkWidget *btn_forward =
      gtk_button_new_from_icon_name("go-next-symbolic", GTK_ICON_SIZE_BUTTON);
  gtk_box_pack_start(GTK_BOX(toolbar), btn_forward, FALSE, FALSE, 0);
  g_signal_connect(btn_forward, "clicked", G_CALLBACK(on_forward_clicked),
                   manager);

  // Reload Button
  GtkWidget *btn_reload = gtk_button_new_from_icon_name("view-refresh-symbolic",
                                                        GTK_ICON_SIZE_BUTTON);
  gtk_box_pack_start(GTK_BOX(toolbar), btn_reload, FALSE, FALSE, 0);
  g_signal_connect(btn_reload, "clicked", G_CALLBACK(on_reload_clicked),
                   manager);

  // Home Button
  GtkWidget *btn_home =
      gtk_button_new_from_icon_name("go-home-symbolic", GTK_ICON_SIZE_BUTTON);
  gtk_box_pack_start(GTK_BOX(toolbar), btn_home, FALSE, FALSE, 0);
  g_object_set_data(G_OBJECT(btn_home), "manager", manager);
  g_signal_connect(btn_home, "clicked", G_CALLBACK(on_home_clicked), profile);

  // URL Entry
  GtkWidget *entry = gtk_entry_new();
  gtk_style_context_add_class(gtk_widget_get_style_context(entry),
                              "browser-url-entry");
  gtk_box_pack_start(GTK_BOX(toolbar), entry, TRUE, TRUE, 0);
  g_object_set_data(G_OBJECT(entry), "manager", manager);
  g_object_set_data(G_OBJECT(entry), "profile", profile);
  g_signal_connect(entry, "activate", G_CALLBACK(on_url_activate), NULL);
  g_signal_connect(entry, "focus-in-event", G_CALLBACK(on_url_focus_in), NULL);

  // Menu Button
  GtkWidget *menu_btn = gtk_menu_button_new();
  GtkWidget *icon =
      gtk_image_new_from_icon_name("open-menu-symbolic", GTK_ICON_SIZE_BUTTON);
  gtk_button_set_image(GTK_BUTTON(menu_btn), icon);
  gtk_box_pack_start(GTK_BOX(toolbar), menu_btn, FALSE, FALSE, 0);

  // Store profile for menu
  g_object_set_data(G_OBJECT(menu_btn), "profile", profile);
  // Mark as menu button to distinguish from other widgets with profile (like
  // entry)
  g_object_set_data(G_OBJECT(menu_btn), "is_menu_button", GINT_TO_POINTER(1));

  if (url_entry_out) {
    *url_entry_out = GTK_ENTRY(entry);
  }

  return toolbar;
}
