#  Copyright (c) 2025  Jeffrey Hsu - JITToolBox
#  #
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#  #
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  #
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <https://www.gnu.org/licenses/>.

import sys

from PySide6.QtCore import Signal
from PySide6.QtWidgets import QWidget, QHBoxLayout, QVBoxLayout, QApplication, QGridLayout
from qfluentwidgets import PushButton, SpinBox, PrimaryPushButton, \
    BodyLabel, SimpleCardWidget
from qfluentwidgets.components.widgets.card_widget import CardSeparator

from ui.components.widget import RollingTextWidget


class QuickScoringKeyBoard(QWidget):
    scoringSignal = Signal(int)

    def __init__(self, parent=None):
        super().__init__(parent)
        self.vBoxLayout = QVBoxLayout(self)
        self.keyBoardLayout = QGridLayout()
        buttons = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 95, 100]

        for i, num in enumerate(buttons):
            row = i // 3
            col = i % 3
            btn = PushButton(str(num))
            btn.setFixedWidth(120)
            btn.clicked.connect(lambda _, n=num: self.scoringSignal.emit(n))
            self.keyBoardLayout.addWidget(btn, row, col)

        self.vBoxLayout.addLayout(self.keyBoardLayout)


class QuickScoring(QWidget):
    submitSignal = Signal(int)

    def __init__(self, parent=None):
        super().__init__(parent)

        self.hBoxLayout = QHBoxLayout(self)
        self.btnVBoxLayout = QVBoxLayout(self)
        self.spinboxLayout = QHBoxLayout(self)
        self.spinboxLabel = BodyLabel("得分", self)
        self.gradeSpinBox = SpinBox(self)
        self.submitButton = PrimaryPushButton("确定", self)
        self.resetButton = PushButton("重置", self)
        self.keyboard = QuickScoringKeyBoard(self)

        self.gradeSpinBox.setRange(0, 100)
        self.gradeSpinBox.setFixedWidth(150)
        self.submitButton.setFixedWidth(120)
        self.resetButton.setFixedWidth(120)

        self.spinboxLayout.addWidget(self.spinboxLabel)
        self.spinboxLayout.addWidget(self.gradeSpinBox)

        self.btnVBoxLayout.addStretch()
        self.btnVBoxLayout.addWidget(self.submitButton)
        self.btnVBoxLayout.addWidget(self.resetButton)
        self.btnVBoxLayout.addStretch()

        self.hBoxLayout.addStretch()
        self.hBoxLayout.addLayout(self.spinboxLayout)
        self.hBoxLayout.addLayout(self.btnVBoxLayout)
        self.hBoxLayout.addWidget(self.keyboard)
        self.hBoxLayout.addStretch()

        self.keyboard.scoringSignal.connect(lambda x: self.gradeSpinBox.setValue(x))
        self.resetButton.clicked.connect(lambda: self.gradeSpinBox.clear())
        self.submitButton.clicked.connect(lambda: self.submitSignal.emit(self.gradeSpinBox.value()))


class PickStudentLabelUi(SimpleCardWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.vBoxLayout = QVBoxLayout(self)
        self.vBoxLayout.setContentsMargins(0, 0, 0, 0)

        self.rollingText = RollingTextWidget(self)
        self.scoring = QuickScoring(self)
        self.separator = CardSeparator(self)

        self.scoring.hide()
        self.separator.hide()

        self.vBoxLayout.addWidget(self.rollingText)
        self.vBoxLayout.addWidget(self.separator)
        self.vBoxLayout.addWidget(self.scoring)
        self.vBoxLayout.addStretch()

    def show_scoring(self):
        self.scoring.show()
        self.separator.show()

    def hideEvent(self, event, /):
        super().hideEvent(event)
        self.scoring.gradeSpinBox.clear()
        self.scoring.hide()
        self.separator.hide()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = PickStudentLabelUi()
    window.show()
    sys.exit(app.exec())
