#pragma once

#include "WidgetPage.h"

#include <QWebChannel>
#include <QtWebEngineWidgets/QWebEngineView>


class JsHandler : public QObject
{
    Q_OBJECT
public:
    //Q_PROPERTY(QString nativeText READ nativeText MEMBER m_nativeText NOTIFY signalNativeTextChanged FINAL)
        explicit JsHandler(QObject* parent = nullptr)
    {
    }

    QString nativeText() const { return m_nativeText; }

signals:
    //在C++中定义的信号，可以在JS端监听此信号接收消息
    void signalNativeTextChanged(const QString& text);
    void signalShowWebView();

public slots:
    void showWebView()
    {
        emit signalShowWebView();
    }

    //C++ 端的公共槽函数，可以在JS端调用。
    void setNativeText(const QString& text)
    {
        m_nativeText = text;
        qDebug() << QString("run JS call CPP: setNativeText:") << text;
        emit signalNativeTextChanged("HelloWorld");
    }

private:
    QString m_nativeText;
};


class WidgetPageWeb : public WidgetPage
{
    Q_OBJECT
public:
    WidgetPageWeb(QWidget* parent, std::string name);

    void slotLoadFinished(bool isOk);
    void slotShowWebView();

public:
    std::shared_ptr<QWebEngineView> wWebView = nullptr;
    QWebChannel* webChannel = nullptr;
    JsHandler* jsHandler = nullptr;
};