"""对比度测试结果绘制。

Step 2 重构：从 pqAutomationApp.PQAutomationApp.plot_contrast 原样搬迁。
"""

from matplotlib.patches import Rectangle
from app.views.modern_styles import get_theme_palette

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from pqAutomationApp import PQAutomationApp



def plot_contrast(self: "PQAutomationApp", contrast_data, test_type):
    """绘制对比度测试结果 - 固定布局版本"""
    palette = get_theme_palette()

    # 清空并重置
    self.contrast_ax.clear()
    self.contrast_fig.patch.set_facecolor(palette["bg"])
    self.contrast_ax.set_facecolor(palette["card_bg"])
    self.contrast_ax.set_xlim(0, 1)
    self.contrast_ax.set_ylim(0, 1)
    self.contrast_ax.axis("off")

    # 强制重置布局
    self.contrast_fig.subplots_adjust(
        left=0.02,
        right=0.98,
        top=0.90,
        bottom=0.02,
    )

    max_lum = contrast_data["max_luminance"]
    min_lum = contrast_data["min_luminance"]
    contrast = contrast_data["contrast_ratio"]

    # 确定等级和颜色
    if contrast >= 5000:
        grade, grade_color = "优秀", "#4CAF50"
    elif contrast >= 3000:
        grade, grade_color = "良好", "#8BC34A"
    elif contrast >= 1000:
        grade, grade_color = "合格", "#FFC107"
    else:
        grade, grade_color = "不合格", "#F44336"

    test_type_name = self.get_test_type_name(test_type)

    # ========== 顶部标题 - 统一格式 ==========
    self.contrast_fig.suptitle(
        f"{test_type_name} - 对比度测试",
        fontsize=12,
        y=0.98,
        fontweight="bold",
        color=palette["fg"],
    )

    # ========== 中央大对比度卡片 ==========
    center_card = Rectangle(
        (0.15, 0.48),
        0.70,
        0.32,
        transform=self.contrast_ax.transAxes,
        facecolor=grade_color,
        edgecolor="black",
        linewidth=2.5,
        alpha=0.15,
    )
    self.contrast_ax.add_patch(center_card)

    # 对比度数值
    self.contrast_ax.text(
        0.5,
        0.65,
        f"{contrast:.0f} : 1",
        ha="center",
        va="center",
        fontsize=36,
        fontweight="bold",
        color=grade_color,
        transform=self.contrast_ax.transAxes,
    )

    # 等级标签
    self.contrast_ax.text(
        0.5,
        0.51,
        f"等级: {grade}",
        ha="center",
        va="center",
        fontsize=12,
        fontweight="bold",
        color=grade_color,
        transform=self.contrast_ax.transAxes,
    )

    # ========== 两个信息卡片（缩小）==========
    card_width = 0.32
    card_height = 0.22
    card_y = 0.12

    gap = 0.05
    total_width = card_width * 2 + gap
    start_x = (1 - total_width) / 2

    cards_data = [
        {
            "x": start_x,
            "title": "白场亮度",
            "value": f"{max_lum:.2f}",
            "unit": "cd/m²",
            "color": palette["surface_alt_bg"],
            "edge_color": palette["primary"],
        },
        {
            "x": start_x + card_width + gap,
            "title": "黑场亮度",
            "value": f"{min_lum:.4f}",
            "unit": "cd/m²",
            "color": palette["card_bg"],
            "edge_color": palette["secondary"],
        },
    ]

    for card in cards_data:
        # 绘制卡片背景
        rect = Rectangle(
            (card["x"], card_y),
            card_width,
            card_height,
            transform=self.contrast_ax.transAxes,
            facecolor=card["color"],
            edgecolor=card["edge_color"],
            linewidth=2,
        )
        self.contrast_ax.add_patch(rect)

        # 标题
        self.contrast_ax.text(
            card["x"] + card_width / 2,
            card_y + card_height - 0.03,
            card["title"],
            ha="center",
            va="top",
            fontsize=10,
            fontweight="bold",
            color=palette["fg"],
            transform=self.contrast_ax.transAxes,
        )

        # 数值
        self.contrast_ax.text(
            card["x"] + card_width / 2,
            card_y + card_height / 2,
            card["value"],
            ha="center",
            va="center",
            fontsize=16,
            fontweight="bold",
            color=palette["fg"],
            transform=self.contrast_ax.transAxes,
        )

        # 单位
        self.contrast_ax.text(
            card["x"] + card_width / 2,
            card_y + 0.03,
            card["unit"],
            ha="center",
            va="bottom",
            fontsize=9,
            color=palette["muted_fg"],
            transform=self.contrast_ax.transAxes,
        )

    self.contrast_canvas.draw()
    self.chart_notebook.select(self.contrast_chart_frame)


class PlotContrastMixin:
    """由 tools/refactor_to_mixins.py 自动生成。
    把本模块的自由函数挂到 PQAutomationApp 上，便于 F12 跳转与类型推断。
    """
    plot_contrast = plot_contrast
