#include "ui/menu.h"
#include "downloads/download_window.h"
#include "settings/settings_window.h"
#include "ui/tabs.h"
#include "ui/window.h" // For create_window

typedef struct {
  TabManager *tab_manager;
  GtkWindow *window;
  Profile *profile;
  GtkWidget *popover;
} MenuContext;

static void on_action_clicked(GtkButton *button, gpointer user_data) {
  MenuContext *ctx = (MenuContext *)user_data;
  const char *action = g_object_get_data(G_OBJECT(button), "action");

  WebKitWebView *webview = tab_manager_get_current_webview(ctx->tab_manager);

  if (g_strcmp0(action, "settings") == 0) {
    settings_window_show(ctx->window, ctx->profile);
  } else if (g_strcmp0(action, "downloads") == 0) {
    download_window_show();
  } else if (g_strcmp0(action, "zoom_in") == 0) {
    if (webview) {
      double zoom = webkit_web_view_get_zoom_level(webview);
      webkit_web_view_set_zoom_level(webview, zoom + 0.1);
    }
  } else if (g_strcmp0(action, "zoom_out") == 0) {
    if (webview) {
      double zoom = webkit_web_view_get_zoom_level(webview);
      webkit_web_view_set_zoom_level(webview, zoom - 0.1);
    }
  } else if (g_strcmp0(action, "print") == 0) {
    if (webview) {
      WebKitPrintOperation *print_op = webkit_print_operation_new(webview);
      webkit_print_operation_run_dialog(print_op, ctx->window);
      g_object_unref(print_op);
    }
  } else if (g_strcmp0(action, "inspector") == 0) {
    if (webview) {
      WebKitWebInspector *inspector = webkit_web_view_get_inspector(webview);
      webkit_web_inspector_show(inspector);
    }
  } else if (g_strcmp0(action, "exit") == 0) {
    gtk_window_close(ctx->window);
  } else if (g_strcmp0(action, "new_tab") == 0) {
    tab_manager_new_tab(ctx->tab_manager, NULL);
  } else if (g_strcmp0(action, "new_window") == 0) {
    GtkApplication *app = gtk_window_get_application(ctx->window);
    create_window(app, ctx->profile);
  } else if (g_strcmp0(action, "new_incognito") == 0) {
    GtkApplication *app = gtk_window_get_application(ctx->window);
    Profile *private_profile = create_private_profile();
    create_window(app, private_profile);
  } else {
    g_print("Action not implemented: %s\n", action);
  }

  gtk_popover_popdown(GTK_POPOVER(ctx->popover));
}

static GtkWidget *create_menu_item(const char *label, const char *action,
                                   MenuContext *ctx) {
  GtkWidget *button = gtk_model_button_new();
  g_object_set(button, "text", label, NULL);
  g_object_set_data(G_OBJECT(button), "action", (gpointer)action);
  g_signal_connect(button, "clicked", G_CALLBACK(on_action_clicked), ctx);
  return button;
}

void menu_attach(GtkWidget *button, TabManager *tab_manager, GtkWindow *window,
                 Profile *profile) {
  GtkWidget *popover = gtk_popover_new(button);

  MenuContext *ctx = g_new0(MenuContext, 1);
  ctx->tab_manager = tab_manager;
  ctx->window = window;
  ctx->profile = profile;
  ctx->popover = popover;

  // Free context when popover is destroyed
  g_signal_connect_data(popover, "destroy", G_CALLBACK(g_free), ctx, NULL, 0);

  GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
  gtk_container_add(GTK_CONTAINER(popover), box);

  // Menu Items
  gtk_box_pack_start(GTK_BOX(box),
                     create_menu_item("Profiles", "profiles", ctx), FALSE,
                     FALSE, 0);
  gtk_box_pack_start(GTK_BOX(box),
                     gtk_separator_new(GTK_ORIENTATION_HORIZONTAL), FALSE,
                     FALSE, 0);

  gtk_box_pack_start(GTK_BOX(box), create_menu_item("New Tab", "new_tab", ctx),
                     FALSE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(box),
                     create_menu_item("New Window", "new_window", ctx), FALSE,
                     FALSE, 0);
  gtk_box_pack_start(
      GTK_BOX(box),
      create_menu_item("New Incognito Window", "new_incognito", ctx), FALSE,
      FALSE, 0);
  gtk_box_pack_start(GTK_BOX(box),
                     gtk_separator_new(GTK_ORIENTATION_HORIZONTAL), FALSE,
                     FALSE, 0);

  gtk_box_pack_start(GTK_BOX(box),
                     create_menu_item("Bookmarks", "bookmarks", ctx), FALSE,
                     FALSE, 0);
  gtk_box_pack_start(GTK_BOX(box), create_menu_item("History", "history", ctx),
                     FALSE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(box),
                     create_menu_item("Download Manager", "downloads", ctx),
                     FALSE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(box),
                     create_menu_item("Password Manager", "passwords", ctx),
                     FALSE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(box),
                     create_menu_item("Extensions Manager", "extensions", ctx),
                     FALSE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(box),
                     gtk_separator_new(GTK_ORIENTATION_HORIZONTAL), FALSE,
                     FALSE, 0);

  gtk_box_pack_start(GTK_BOX(box), create_menu_item("Print...", "print", ctx),
                     FALSE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(box),
                     create_menu_item("Find in Page", "find", ctx), FALSE,
                     FALSE, 0);

  // Zoom Controls
  GtkWidget *zoom_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
  gtk_box_pack_start(GTK_BOX(box), zoom_box, FALSE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(zoom_box), gtk_label_new("Zoom"), FALSE, FALSE,
                     10);
  GtkWidget *btn_minus = gtk_button_new_with_label("-");
  g_object_set_data(G_OBJECT(btn_minus), "action", "zoom_out");
  g_signal_connect(btn_minus, "clicked", G_CALLBACK(on_action_clicked), ctx);
  gtk_box_pack_end(GTK_BOX(zoom_box), btn_minus, FALSE, FALSE, 0);
  GtkWidget *btn_plus = gtk_button_new_with_label("+");
  g_object_set_data(G_OBJECT(btn_plus), "action", "zoom_in");
  g_signal_connect(btn_plus, "clicked", G_CALLBACK(on_action_clicked), ctx);
  gtk_box_pack_end(GTK_BOX(zoom_box), btn_plus, FALSE, FALSE, 0);

  gtk_box_pack_start(GTK_BOX(box),
                     gtk_separator_new(GTK_ORIENTATION_HORIZONTAL), FALSE,
                     FALSE, 0);

  gtk_box_pack_start(GTK_BOX(box),
                     create_menu_item("Settings", "settings", ctx), FALSE,
                     FALSE, 0);
  gtk_box_pack_start(GTK_BOX(box),
                     create_menu_item("Inspect Element", "inspector", ctx),
                     FALSE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(box),
                     create_menu_item("Browser Console", "console", ctx), FALSE,
                     FALSE, 0);
  gtk_box_pack_start(GTK_BOX(box), create_menu_item("About", "about", ctx),
                     FALSE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(box),
                     gtk_separator_new(GTK_ORIENTATION_HORIZONTAL), FALSE,
                     FALSE, 0);

  gtk_box_pack_start(GTK_BOX(box), create_menu_item("Exit", "exit", ctx), FALSE,
                     FALSE, 0);

  gtk_widget_show_all(box);
  gtk_menu_button_set_popover(GTK_MENU_BUTTON(button), popover);
}
