// Copyright (C) Oleg Shparber, et al. <https://zealdocs.org>
// Copyright (C) 2013-2014 Jerzy Kozera
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef ZEAL_BROWSER_WEBCONTROL_H
#define ZEAL_BROWSER_WEBCONTROL_H

#include <QWidget>

class QWebEngineHistory;

namespace Zeal::Browser {

class SearchToolBar;
class WebView;

class WebControl final : public QWidget
{
    Q_OBJECT
    Q_DISABLE_COPY_MOVE(WebControl)
public:
    explicit WebControl(QWidget *parent = nullptr);
    ~WebControl() override = default;

    void focus();
    void load(const QUrl &url);
    bool canGoBack() const;
    bool canGoForward() const;

    QString title() const;
    QUrl url() const;

    QWebEngineHistory *history() const;
    void restoreHistory(const QByteArray &array);
    QByteArray saveHistory() const;

    int zoomLevel() const;
    void setZoomLevel(int level);

    int zoomLevelPercentage() const;

    void setJavaScriptEnabled(bool enabled);
    void setWebBridgeObject(const QString &name, QObject *object);

    void activateSearchBar();
    void back();
    void forward();

    void zoomIn();
    void zoomOut();
    void resetZoom();

signals:
    void titleChanged(const QString &title);
    void urlChanged(const QUrl &url);
    void zoomLevelChanged();

protected:
    void keyPressEvent(QKeyEvent *event) override;

private:
    friend class WebView;

    void updateWebBridge(const QUrl &url);
    void installWebBridge();
    void removeWebBridge();

    WebView *m_webView = nullptr;
    SearchToolBar *m_searchToolBar = nullptr;
    QString m_webBridgeName;
    QObject *m_webBridgeObject = nullptr;
    int m_renderProcessReloadAttempts = 0;
};

} // namespace Zeal::Browser

#endif // ZEAL_BROWSER_WEBCONTROL_H
