from PySide6.QtGui import QIcon, QShowEvent
from qfluentwidgets import FluentIcon, MSFluentWindow, NavigationItemPosition, MessageBox, setThemeColor

from ui import MAIN_THEME_COLOR, BLUE_BACKGROUND_COLOR
from ui.pyui.about_ui import AboutWidget
from ui.pyui.achievement_ui import AchievementWidget
from ui.pyui.defense_ui import DefenseWidget
from ui.pyui.picker_ui import PickerWidget
from ui.pyui.test_ui import TestWidget
from utils.function import DEVELOPMENT_ENV


class MainWindow(MSFluentWindow):
    def __init__(self):
        super().__init__()

        setThemeColor(MAIN_THEME_COLOR)
        self.setCustomBackgroundColor(BLUE_BACKGROUND_COLOR, BLUE_BACKGROUND_COLOR)

        self.achievementInterface = AchievementWidget('Achievement Interface', self)
        self.defenseInterface = DefenseWidget('Defense Interface', self)
        self.aboutInterface = AboutWidget('About Interface', self)
        self.pickerInterface = PickerWidget('Picker Interface', self)
        if not DEVELOPMENT_ENV:
            self.testInterface = TestWidget('Test Interface', self)

        self.achievementInterface.error.connect(self.showError)
        self.defenseInterface.errorSignal.connect(self.showError)
        self.pickerInterface.errorSignal.connect(self.showError)

        self.initNavigation()
        self.initWindow()

    def initNavigation(self):
        self.addSubInterface(self.achievementInterface, FluentIcon.SPEED_HIGH, '达成度')
        self.addSubInterface(self.defenseInterface, FluentIcon.FEEDBACK, '答辩题目')
        self.addSubInterface(self.pickerInterface, FluentIcon.PEOPLE, '提问')
        if not DEVELOPMENT_ENV:
            self.addSubInterface(self.testInterface, FluentIcon.VIEW, '测试')

        self.addSubInterface(self.aboutInterface, FluentIcon.INFO, '关于', position=NavigationItemPosition.BOTTOM)

    def initWindow(self):
        self.resize(900, 700)
        self.setWindowTitle('教学工具箱')
        self.setWindowIcon(QIcon(':/images/logo.png'))

    def showError(self, title: str, message: str):
        box = MessageBox(title, message, self)
        box.yesButton.setText("关闭")
        box.cancelButton.hide()
        box.exec()

    def showEvent(self, event: QShowEvent):
        super().showEvent(event)
        if DEVELOPMENT_ENV:
            import pyi_splash
            pyi_splash.update_text('正在加载...')
            pyi_splash.close()
