from PyQt6.QtWidgets import QDialog, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QWidget
from PyQt6.QtCore import Qt, QSize
from PyQt6.QtGui import QPixmap
from .get_bootstrap_icon import get_colored_pixmap, get_colored_icon


class ModernConfirmDialog(QDialog):
    """符合项目深色圆角主题的现代确认对话框

    用法：dlg = ModernConfirmDialog(parent, title, message, icon_name)
    if dlg.exec() == QDialog.DialogCode.Accepted: ...
    """

    def __init__(self, parent: QWidget, title: str, message: str, icon_name: str = "question-circle.svg",
                 confirm_text: str = "确定", cancel_text: str = "取消"):
        super().__init__(parent)
        self.setWindowTitle(title)
        self.setModal(True)
        self.setWindowFlag(Qt.WindowType.WindowContextHelpButtonHint, False)
        self.setFixedWidth(560)

        # 样式
        self.setStyleSheet(
            """
            QDialog { background-color: #3f3f3f; border: 2px solid #646464; border-radius: 12px; }
            QLabel#title { color: #f2f2f2; font-size: 22px; font-weight: 700; background-color: transparent;}
            QLabel#message { color: #dcdcdc; font-size: 18px; background-color: transparent;}
            QPushButton#confirm { min-width: 140px; min-height: 48px; font-size: 18px; font-weight: 700; color: #ffffff; background-color: #2f6f91; border: 2px solid #4a9fc8; border-radius: 10px; }
            QPushButton#confirm:hover { background-color: #3a85b3; }
            QPushButton#cancel { min-width: 120px; min-height: 48px; font-size: 18px; font-weight: 600; color: #f2f2f2; background-color: #555555; border: 2px solid #707070; border-radius: 10px; }
            QPushButton#cancel:hover { background-color: #636363; }
            """
        )

        root = QVBoxLayout(self)
        root.setContentsMargins(20, 20, 20, 20)
        root.setSpacing(16)

        top_row = QHBoxLayout()
        top_row.setSpacing(16)

        # 图标
        icon_label = QLabel()
        icon_label.setStyleSheet("background-color: transparent;")
        icon_pix = get_colored_pixmap(icon_name, "#4a9fc8", 64, 64)
        if isinstance(icon_pix, QPixmap):
            icon_label.setPixmap(icon_pix.scaled(64, 64, Qt.AspectRatioMode.KeepAspectRatio, Qt.TransformationMode.SmoothTransformation))
        icon_label.setFixedSize(72, 72)
        top_row.addWidget(icon_label, alignment=Qt.AlignmentFlag.AlignTop)

        # 文本列
        text_col = QVBoxLayout()
        title_lbl = QLabel(title)
        title_lbl.setObjectName("title")
        text_col.addWidget(title_lbl)

        msg_lbl = QLabel(message)
        msg_lbl.setObjectName("message")
        msg_lbl.setWordWrap(True)
        text_col.addWidget(msg_lbl)

        top_row.addLayout(text_col)
        root.addLayout(top_row)

        # 按钮行
        btn_row = QHBoxLayout()
        btn_row.addStretch()

        cancel_btn = QPushButton(cancel_text)
        cancel_btn.setObjectName("cancel")
        cancel_icon = get_colored_icon("x-circle.svg", "#f2f2f2", 20, 20)
        if cancel_icon:
            cancel_btn.setIcon(cancel_icon)
            cancel_btn.setIconSize(QSize(18, 18))
        cancel_btn.clicked.connect(self.reject)
        btn_row.addWidget(cancel_btn)

        confirm_btn = QPushButton(confirm_text)
        confirm_btn.setObjectName("confirm")
        confirm_icon = get_colored_icon("check-circle.svg", "#ffffff", 20, 20)
        if confirm_icon:
            confirm_btn.setIcon(confirm_icon)
            confirm_btn.setIconSize(QSize(18, 18))
        confirm_btn.clicked.connect(self.accept)
        btn_row.addWidget(confirm_btn)

        root.addLayout(btn_row)
