from PySide6.QtGui import QDesktopServices, Qt
from PySide6.QtWidgets import QVBoxLayout, QHBoxLayout
from qfluentwidgets import PrimaryPushSettingCard, FluentIcon, GroupHeaderCardWidget, PushButton, ImageLabel, TitleLabel

from ui.components.widget import Widget
from utils.function import DEVELOPMENT_ENV


class AboutWidget(Widget):
    def __init__(self, key: str, parent=None):
        super().__init__(key, parent)

        self.logoImage = ImageLabel(':/images/logo.png')
        self.logoImage.scaledToHeight(100)
        self.appNameLabel = TitleLabel('教学工具箱 🛠️')

        self.hBox = QHBoxLayout()
        self.hBox.addWidget(self.logoImage, 0, Qt.AlignLeft)
        self.hBox.addWidget(self.appNameLabel, 1, Qt.AlignLeft)

        build_time_str = ''
        if DEVELOPMENT_ENV:
            from build_info import BUILD_TIME
            build_time_str = f"，构建时间：{BUILD_TIME}"

        self.version_card = PrimaryPushSettingCard(
            text="获取源码",
            icon=FluentIcon.INFO,
            title="关于",
            content=f"作者：许方杰。当前版本：1.0.0{build_time_str}\n"
                    f"本软件使用 GPLv3 开源协议进行分发，作者不对使用本软件造成的任何损失负责。"
        )
        self.button_list = [
            PushButton("访问网站"),
            PushButton("访问网站"),
            PushButton("访问网站"),
            PushButton("访问网站"),
        ]
        self.url_list = [
            "https://qt.io",
            "https://qfluentwidgets.com",
            "https://openpyxl.readthedocs.io/en/stable",
            "https://github.com/python-openxml/python-docx"
        ]
        self.group_card = GroupHeaderCardWidget(self)
        self.group_card.setTitle("第三方框架")
        self.vbox = QVBoxLayout(self)

        self.vbox.addLayout(self.hBox)
        self.vbox.addWidget(self.version_card)
        self.vbox.addWidget(self.group_card)
        self.vbox.addStretch(1)

        self.group_card.addGroup(":/images/3rd/qt.png", "PySide6", self.url_list[0], self.button_list[0])
        self.group_card.addGroup(":/images/3rd/qfluentwidgets", "QFluentWidgets", self.url_list[1], self.button_list[1])
        self.group_card.addGroup(FluentIcon.LAYOUT, "openpyxl", self.url_list[2], self.button_list[2])
        self.group_card.addGroup(FluentIcon.LAYOUT, "python-docx", self.url_list[3], self.button_list[3])

        self.version_card.clicked.connect(
            lambda: QDesktopServices.openUrl("https://cantyonion.site/git/cantyonion/DefenseTopicGenerator")
        )
        self.button_list[0].clicked.connect(lambda: QDesktopServices.openUrl(self.url_list[0]))
        self.button_list[1].clicked.connect(lambda: QDesktopServices.openUrl(self.url_list[1]))
        self.button_list[2].clicked.connect(lambda: QDesktopServices.openUrl(self.url_list[2]))
        self.button_list[3].clicked.connect(lambda: QDesktopServices.openUrl(self.url_list[3]))
