#pragma once
#include "fuzzy-section.hpp"
#include "services/app-service/abstract-app-db.hpp"
#include "services/window-manager/abstract-window-manager.hpp"
#include <QCoreApplication>

struct WindowEntry {
  AbstractWindowManager::WindowPtr window;
  std::shared_ptr<AbstractApplication> app;
  QString workspaceName;
};

template <> struct fuzzy::FuzzySearchable<WindowEntry> {
  static fuzzy::Match score(const WindowEntry &e, const fuzzy::Query &query) {
    auto title = e.window->title().toStdString();
    auto wmClass = e.window->wmClass().toStdString();
    if (e.app) {
      auto appName = e.app->displayName().toStdString();
      return fuzzy::scoreWeighted({{title, 1.0}, {appName, 0.5}, {wmClass, 0.3}}, query);
    }
    return fuzzy::scoreWeighted({{title, 1.0}, {wmClass, 0.3}}, query);
  }
};

class SwitchWindowsSection : public FuzzySection<WindowEntry> {
  Q_DECLARE_TR_FUNCTIONS(SwitchWindowsSection)
public:
  QString sectionName() const override { return tr("Open Windows"); }

protected:
  QString displayTitle(const WindowEntry &e) const override;
  QString displaySubtitle(const WindowEntry &e) const override;
  std::optional<ImageURL> displayIcon(const WindowEntry &e) const override;
  AccessoryList displayAccessories(const WindowEntry &e) const override;
  std::unique_ptr<ActionPanelState> buildActionPanel(const WindowEntry &e) const override;
};
