# -*- coding: utf-8 -*-

__author__ = 'wanger'
__date__ = '2024-08-21'
__copyright__ = '(C) 2024 by siwei'
__revision__ = '1.0'

from typing import Optional

from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QIcon
from PyQt5.QtNetwork import QNetworkCookie
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineView
import sys


class CustomWebView:
    def __init__(
            self,
            weburi: str,  # webview uri
            title: Optional[str] = "自定义窗口",  # window title
            cookieName: Optional[str] = None,  # cookie name
            cookieValue: Optional[str] = None,  # cookie value
            windowW: Optional[int] = 1200,  # window min width default 1200
            windowH: Optional[int] = 700,  # window min height default 1200
            icon: Optional[str] = "E:\\projects\\sanya\\real3d-portalsite\\src\\assets\\logo.png",
            # window icon type png
    ):
        self.uri = weburi
        app = QApplication(sys.argv)
        web_view = QWebEngineView()
        web_view.setWindowTitle(title)
        web_view.setMinimumSize(windowW, windowH)
        web_view.setWindowIcon(QIcon(icon))
        page = QWebEnginePage()
        page.load(QUrl(self.uri))
        web_view.setPage(page)
        if cookieName is not None and cookieValue is not None:
            # Cookie
            netcookie = QNetworkCookie()
            netcookie.setName(bytes(cookieName, encoding='utf-8'))
            netcookie.setValue(bytes(cookieValue, encoding='utf-8'))
            page.profile().cookieStore().setCookie(netcookie, QUrl(self.uri))
        web_view.show()
        sys.exit(app.exec_())