#ifndef __HELP__HXX
#define __HELP__HXX

#include <ui_help.hxx>
#include <commands.hxx>
#include <cassert>

class Help: public QDockWidget, protected Ui::Help {
    Q_OBJECT
  public:
    Help(QWidget* p = nullptr): QDockWidget(p) {
      setupUi(this);
      _search->hide();
      _help->setText(script.commands(Script::HTML));
    }
  public Q_SLOTS:
    void on__search_textEdited(const QString& txt) {
      _help->moveCursor(QTextCursor::Start);
      if (script.prototypes().count(txt))
        _help->scrollToAnchor(txt);
      else
        _help->find(txt);
    }
    void on__search_returnPressed() {
      next();
    }
    void next() {
      _help->find(_search->text());
    }
    void previous() {
      _help->find(_search->text(), QTextDocument::FindBackward);
    }
    void startSearch() {
      _search->show();
      _search->setFocus();
      _search->selectAll();
    }
  protected:
    void keyPressEvent(QKeyEvent *e) {
      if (e->matches(QKeySequence::Find))
        startSearch();
      if (e->matches(QKeySequence::FindNext))
        next();
      if (e->matches(QKeySequence::FindPrevious))
        previous();
      if (e->matches(QKeySequence::Cancel))
        _search->hide();
      return QDockWidget::keyPressEvent(e);
    }
  private:
    const Script script;
};

#endif
