#include "edit-keywords-view-host.hpp"
#include "navigation-controller.hpp"
#include "service-registry.hpp"
#include "services/toast/toast-service.hpp"
#include "ui/action-pannel/action.hpp"

EditKeywordsViewHost::EditKeywordsViewHost(LoadFn loadKeywords, SaveFn saveKeywords, const QString &infoText)
    : FormViewBase(), m_loadKeywords(std::move(loadKeywords)), m_saveKeywords(std::move(saveKeywords)),
      m_infoText(infoText) {}

QUrl EditKeywordsViewHost::qmlComponentUrl() const {
  return QUrl(QStringLiteral("qrc:/Vicinae/EditKeywordsFormView.qml"));
}

QVariantMap EditKeywordsViewHost::qmlProperties() {
  return {{QStringLiteral("host"), QVariant::fromValue(this)}};
}

void EditKeywordsViewHost::initialize() {
  BaseView::initialize();

  m_keywords = m_loadKeywords();

  auto panel = std::make_unique<FormActionPanelState>();
  auto section = panel->createSection();
  auto submitAction =
      new StaticAction(tr("Submit"), ImageURL::builtin(BuiltinIcon::EnterKey), [this]() { submit(); });
  section->addAction(submitAction);
  setActions(std::move(panel));

  emit formChanged();
}

void EditKeywordsViewHost::submit() {
  auto toast = context()->services->toastService();

  if (m_saveKeywords(m_keywords)) {
    toast->setToast(tr("Keywords edited"), ToastStyle::Success);
    popSelf();
  } else {
    toast->setToast(tr("Failed to edit keywords"), ToastStyle::Danger);
  }
}
