#pragma once
#include "actions/browser-tab-actions.hpp"
#include "builtin_icon.hpp"
#include "common.hpp"
#include "navigation-controller.hpp"
#include "services/browser-extension-service.hpp"
#include "services/root-item-manager/root-item-manager.hpp"
#include "ui/list-accessory/list-accessory.hpp"
#include <QCoreApplication>
#include <qjsonobject.h>
#include <qstringliteral.h>
#include <ranges>

class BrowserTabRootItem : public RootItem {
  Q_DECLARE_TR_FUNCTIONS(BrowserTabRootItem)

  double baseScoreWeight() const override { return 1.1; }

  QString typeDisplayName() const override { return tr("Browser Tab"); }

  QString title() const override { return m_tab.host(); }

  QString subtitle() const override { return m_tab.title.c_str(); }

  std::vector<QString> keywords() const override { return {m_tab.url.c_str()}; }

  std::unique_ptr<ActionPanelState> newActionPanel(ApplicationContext *ctx,
                                                   const RootItemMetadata &metadata) const override {
    return BrowserTabActionGenerator::generate(ctx, m_tab);
  }

  AccessoryList accessories() const override {
    ListAccessory accessory{.text = tr("Tab")};

    if (m_tab.audible) { accessory.icon = ImageURL::emoji(m_tab.muted ? "🔇" : "🔊"); }

    return {accessory};
  }

  EntrypointId uniqueId() const override { return EntrypointId("browser-tabs", m_tab.uniqueId()); }

  ImageURL iconUrl() const override { return m_tab.icon(); }

  bool isActive() const override { return m_tab.active; }

public:
  BrowserTabRootItem(const BrowserExtensionService::BrowserTab &tab) : m_tab(tab) {}

private:
  BrowserExtensionService::BrowserTab m_tab;
};

class BrowserTabProvider : public RootProvider {
public:
  std::vector<std::shared_ptr<RootItem>> loadItems() const override {
    auto items = m_service.tabs() |
                 std::views::transform(
                     [](const BrowserExtensionService::BrowserTab &tab) -> std::shared_ptr<RootItem> {
                       return std::make_shared<BrowserTabRootItem>(tab);
                     });

    return items | std::ranges::to<std::vector>();
  }

  Type type() const override { return GroupProvider; }

  ImageURL icon() const override { return BuiltinIcon::AppWindowSidebarLeft; }

  QString displayName() const override {
    return QCoreApplication::translate("BrowserTabProvider", "Browser Tabs");
  }

  bool isTransient() const override { return true; }

  QString uniqueId() const override { return "browser-tabs"; }

  PreferenceList preferences() const override { return {}; }

  void preferencesChanged(const QJsonObject &preferences) override {}

public:
  BrowserTabProvider(BrowserExtensionService &service) : m_service(service) {
    connect(&m_service, &BrowserExtensionService::tabsChanged, this, [this]() { emit itemsChanged(); });
  }

private:
  BrowserExtensionService &m_service;
};
