/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: https://neueland.iserlohn-fortress.net/smolbote.hg
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "navigationbar.h"
#include "mainwindow/mainwindow.h"
#include <QHBoxLayout>
#include <QMenu>
#include <QShortcut>
#include <QStyle>
#include <QToolBar>
#include <QToolButton>
#include <QWebEngineHistory>

NavigationBar::NavigationBar(MainWindow *parent)
    : QObject(parent)
{
    qStyle = parent->style();

    // Back button
    backButton = new QToolButton(parent);
    backButton->setIcon(qStyle->standardIcon(QStyle::SP_ArrowBack));
    backButton->setShortcut(QString::fromStdString(parent->m_config->value<std::string>("browser.shortcuts.back").value()));
    connect(backButton, &QToolButton::clicked, this, [this]() {
        m_view->history()->back();
    });

    auto *backMenu = new QMenu(backButton);
    backButton->setMenu(backMenu);
    connect(backMenu, &QMenu::aboutToShow, this, [this, backMenu]() {
        backMenu->clear();
        const QList<QWebEngineHistoryItem> items = m_view->history()->backItems(10);
        for(const QWebEngineHistoryItem &i : items) {
            QAction *a = backMenu->addAction(i.title());
            connect(a, &QAction::triggered, this, [i, this]() {
                m_view->history()->goToItem(i);
            });
        }
    });

    // Forward button
    forwardButton = new QToolButton(parent);
    forwardButton->setIcon(qStyle->standardIcon(QStyle::SP_ArrowForward));
    forwardButton->setShortcut(QString::fromStdString(parent->m_config->value<std::string>("browser.shortcuts.forward").value()));
    connect(forwardButton, &QToolButton::clicked, this, [this]() {
        m_view->history()->forward();
    });

    auto *forwardMenu = new QMenu(forwardButton);
    forwardButton->setMenu(forwardMenu);
    connect(forwardMenu, &QMenu::aboutToShow, this, [this, forwardMenu]() {
        forwardMenu->clear();
        const QList<QWebEngineHistoryItem> items = m_view->history()->forwardItems(10);
        for(const QWebEngineHistoryItem &i : items) {
            QAction *a = forwardMenu->addAction(i.title());
            connect(a, &QAction::triggered, this, [i, this]() {
                m_view->history()->goToItem(i);
            });
        }
    });

    // Stop/Refresh button
    stopReloadButton = new QToolButton(parent);
    stopReloadButton->setIcon(qStyle->standardIcon(QStyle::SP_BrowserReload));
    stopReloadButton->setShortcut(QString::fromStdString(parent->m_config->value<std::string>("browser.shortcuts.refresh").value()));
    connect(stopReloadButton, &QToolButton::clicked, this, [this]() {
        if(m_view->isLoaded())
            m_view->reload();
        else
            m_view->stop();
    });

    auto *reloadShortcut = new QShortcut(
        QString::fromStdString(parent->m_config->value<std::string>("browser.shortcuts.reload").value()),
        parent);
    connect(reloadShortcut, &QShortcut::activated, this, [this]() {
        m_view->reload();
    });

    // Home button
    homeButton = new QToolButton(parent);
    homeButton->setIcon(qStyle->standardIcon(QStyle::SP_DirHomeIcon));
    homeButton->setShortcut(QString::fromStdString(parent->m_config->value<std::string>("browser.shortcuts.home").value()));
    connect(homeButton, &QToolButton::clicked, this, [this, parent]() {
        m_view->load(parent->m_profile->homepage());
    });
}

void NavigationBar::addWidgetsTo(QToolBar *toolBar)
{
    toolBar->addWidget(backButton);
    toolBar->addWidget(forwardButton);
    toolBar->addWidget(stopReloadButton);
    toolBar->addWidget(homeButton);
}

void NavigationBar::connectWebView(WebView *view)
{
    Q_CHECK_PTR(view);
    m_view = view;

    disconnect(loadStartedConnection);
    disconnect(loadFinishedConnection);

    if(view->isLoaded()) {
        update_loadFinished();
    } else {
        update_loadStarted();
    }

    loadStartedConnection = connect(view, &QWebEngineView::loadStarted, this, &NavigationBar::update_loadStarted);
    loadFinishedConnection = connect(view, &WebView::loaded, this, &NavigationBar::update_loadFinished);
}

void NavigationBar::update_loadStarted()
{
    backButton->setEnabled(m_view->history()->canGoForward());
    forwardButton->setEnabled(m_view->history()->canGoForward());
    stopReloadButton->setIcon(qStyle->standardIcon(QStyle::SP_BrowserStop));
}

void NavigationBar::update_loadFinished()
{
    backButton->setEnabled(m_view->history()->canGoBack());
    forwardButton->setEnabled(m_view->history()->canGoForward());
    stopReloadButton->setIcon(qStyle->standardIcon(QStyle::SP_BrowserReload));
}