from PyQt5.QtWidgets import QWidget, QVBoxLayout, QCheckBox, QComboBox, QColorDialog, QPushButton, QLabel, QHBoxLayout, \
    QLineEdit, QMessageBox, QSpacerItem, QSizePolicy
from PyQt5.QtCore import Qt, pyqtSignal
import config


class SettingsPage(QWidget):
    floatingSignal = pyqtSignal(int)
    colorSignal = pyqtSignal(str)

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

        # 第1行：客户端模式选择
        self.modeLayout = QHBoxLayout()
        self.modeLabel = QLabel("客户端模式:")
        self.modeLayout.addWidget(self.modeLabel)

        self.modeComboBox = QComboBox()  # 创建下拉框
        self.modeComboBox.addItems(["演示笔", "电脑麦克风"])  # 添加选项
        self.modeLayout.addWidget(self.modeComboBox)
        self.modeLayout.addStretch(1)

        self.mode_type = config.config["MODE_TYPE"]
        if self.mode_type == "0":
            self.modeComboBox.setCurrentIndex(0)
        else:
            self.modeComboBox.setCurrentIndex(1)

        self.modeComboBox.currentIndexChanged.connect(self.modeChanged)  # 连接信号和槽

        # 添加空间，控制间隔
        spacerItem = QSpacerItem(40, 40, QSizePolicy.Expanding, QSizePolicy.Minimum)
        self.modeLayout.addItem(spacerItem)
        layout.addLayout(self.modeLayout)

        # 第2行：悬浮窗启用开关
        self.floatingWindowLayout = QHBoxLayout()
        self.floatingWindowLabel = QLabel("悬浮窗启用:")
        self.floatingWindowLayout.addWidget(self.floatingWindowLabel)

        self.floatingWindowEnableCheckBox = QCheckBox("")
        self.floatingWindowLayout.addWidget(self.floatingWindowEnableCheckBox)
        self.floatingWindowLayout.addStretch(1)  # 添加弹性空间使得"开启"文本左对齐

        asr_show = config.config["ASR_TXT_SHOW"]
        if asr_show == "0":
            self.floatingWindowEnableCheckBox.setChecked(False)
        else:
            self.floatingWindowEnableCheckBox.setChecked(True)

        # 连接信号和槽
        self.floatingWindowEnableCheckBox.stateChanged.connect(self.floatingWindowStateChanged)

        spacerItem2 = QSpacerItem(40, 40, QSizePolicy.Expanding, QSizePolicy.Minimum)
        self.floatingWindowLayout.addItem(spacerItem2)
        layout.addLayout(self.floatingWindowLayout)

        # 第3行：字体颜色选择器
        self.colorPickerLayout = QHBoxLayout()
        self.colorLabel = QLabel("字体颜色:")
        self.colorPickerLayout.addWidget(self.colorLabel)

        self.colorLineEdit = QLineEdit()
        self.colorLineEdit.setFixedWidth(100)  # 设置文本框宽度更小
        self.colorLineEdit.setText(config.config["ASR_TXT_COLOR"])
        self.colorPickerLayout.addWidget(self.colorLineEdit)

        self.colorPickerButton = QPushButton("选择颜色")
        self.colorPickerButton.clicked.connect(self.openColorPicker)
        self.colorPickerLayout.addWidget(self.colorPickerButton)
        self.colorPickerLayout.addStretch(1)  # 添加弹性空间使得"开启"文本左对齐

        layout.addLayout(self.colorPickerLayout)

        # 添加弹性空间使得两个水平布局在页面上方并且不占据整个页面的宽度
        layout.addStretch(1)
        self.setLayout(layout)

    # 新增的槽函数：处理模式选择改变时的操作
    def modeChanged(self, index):
        config.save_config("APP/MODE_TYPE", index)
        # 弹出提醒框
        if index == 0:
            QMessageBox.information(self, "提示", "客户端模式修改成功！\n请重启客户端以使修改生效。", QMessageBox.Ok)
        else:
            QMessageBox.information(self, "提示",
                                    "客户端模式修改成功！\n请重启客户端以使修改生效。\n电脑麦克风模式请长按键盘右侧Ctrl键说话",
                                    QMessageBox.Ok)

    def floatingWindowStateChanged(self, state):
        self.floatingSignal.emit(state)
        if state == 0:  # 选中关闭
            # print("悬浮窗已关闭")
            config.save_config("APP/ASR_TXT_SHOW", 0)
        else:
            # print("悬浮窗已开启")
            config.save_config("APP/ASR_TXT_SHOW", 1)

    def openColorPicker(self):
        color = QColorDialog.getColor()
        if color.isValid():
            self.colorSignal.emit(color.name())
            self.colorLineEdit.setText(color.name())
            # 这里添加设置字体颜色的代码
            config.save_config("APP/ASR_TXT_COLOR", color.name())
