#pragma once
#include "extend/dropdown-model.hpp"
#include "extend/metadata-model.hpp"
#include "image-url.hpp"
#include "ui/image/url.hpp"
#include "ui/list-accessory/list-accessory.hpp"
#include <QMimeDatabase>
#include <QString>
#include <QVariantList>
#include <cstdint>
#include <filesystem>
#include <vector>

namespace qml {

inline constexpr int64_t MAX_PREVIEW_SIZE = 2 * 1024 * 1024;

struct FilePreviewContent {
  QString mimeType;
  QString imageSource;
  QString textContent;
};

FilePreviewContent resolveFilePreview(const std::filesystem::path &path, QMimeDatabase &mimeDb);

QVariantList metadataToVariantList(const MetadataModel &metadata);
QVariantList accessoriesToVariantList(const AccessoryList &accessories);
QVariantMap accessoryToVariant(const ListAccessory &acc);

inline QString imageSourceFor(const ImageURL &url) { return url.toString(); }

inline QVariantMap makeDropdownItem(const QString &id, const QString &displayName,
                                    const QString &iconSource = {}) {
  QVariantMap m;
  m[QStringLiteral("id")] = id;
  m[QStringLiteral("displayName")] = displayName;
  if (!iconSource.isEmpty()) m[QStringLiteral("iconSource")] = iconSource;
  return m;
}

inline std::optional<QString> firstDropdownItemValue(const std::vector<DropdownModel::Child> &children) {
  for (const auto &child : children) {
    if (auto *item = std::get_if<DropdownModel::Item>(&child)) {
      return QString::fromStdString(item->value);
    } else if (auto *section = std::get_if<DropdownModel::Section>(&child)) {
      if (!section->items.empty()) return QString::fromStdString(section->items[0].value);
    }
  }
  return std::nullopt;
}

inline QVariantList convertDropdownChildren(const std::vector<DropdownModel::Child> &children) {
  QVariantList result;
  QVariantList unsectioned;

  for (const auto &child : children) {
    if (auto *item = std::get_if<DropdownModel::Item>(&child)) {
      QVariantMap m;
      m["id"] = QString::fromStdString(item->value);
      m["displayName"] = QString::fromStdString(item->title);
      m["iconSource"] = item->icon ? imageSourceFor(ImageURL(*item->icon)) : QString();
      unsectioned.append(m);
    } else if (auto *section = std::get_if<DropdownModel::Section>(&child)) {
      if (!unsectioned.isEmpty()) {
        QVariantMap defaultSection;
        defaultSection["title"] = QString();
        defaultSection["items"] = unsectioned;
        result.append(defaultSection);
        unsectioned.clear();
      }
      QVariantMap sectionMap;
      sectionMap["title"] = QString::fromStdString(section->title);
      QVariantList sectionItems;
      for (const auto &si : section->items) {
        QVariantMap m;
        m["id"] = QString::fromStdString(si.value);
        m["displayName"] = QString::fromStdString(si.title);
        m["iconSource"] = si.icon ? imageSourceFor(ImageURL(*si.icon)) : QString();
        sectionItems.append(m);
      }
      sectionMap["items"] = sectionItems;
      result.append(sectionMap);
    }
  }

  if (!unsectioned.isEmpty()) {
    QVariantMap defaultSection;
    defaultSection["title"] = QString();
    defaultSection["items"] = unsectioned;
    result.append(defaultSection);
  }

  return result;
}

} // namespace qml
