"""Gamma 曲线绘制。

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

import numpy as np

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from pqAutomationApp import PQAutomationApp



def plot_gamma(self: "PQAutomationApp", L_bar, results_with_gamma_list, target_gamma, test_type):
    """绘制Gamma曲线 + 数据表格（包含实测亮度）"""

    # ========== 1. 清空并重置左侧曲线 ==========
    self.gamma_ax.clear()
    self.gamma_ax.set_xlim(0, 105)
    self.gamma_ax.set_ylim(0, 1.1)
    self.gamma_ax.set_xlabel("灰阶 (%)", fontsize=10)
    self.gamma_ax.set_ylabel("L_bar", fontsize=10)
    self.gamma_ax.grid(True, linestyle="--", alpha=0.3)
    self.gamma_ax.tick_params(labelsize=9)

    # 生成横坐标（灰阶百分比）
    x_values = np.linspace(0, 100, len(L_bar))

    # 反转 L_bar（确保从左到右是 0% → 100%）
    if len(L_bar) > 1 and L_bar[0] > L_bar[-1]:
        L_bar = L_bar[::-1]
        results_with_gamma_list = results_with_gamma_list[::-1]

    # 计算平均Gamma
    gamma_values = []
    for item in results_with_gamma_list:
        if isinstance(item, (list, tuple)) and len(item) >= 4:
            gamma = item[3]
            if 0.5 < gamma < 5.0:
                gamma_values.append(gamma)

    avg_gamma = np.mean(gamma_values) if gamma_values else target_gamma

    # 绘制实测曲线
    self.gamma_ax.plot(
        x_values,
        L_bar,
        "b-o",
        label=f"实测 (平均γ={avg_gamma:.2f})",
        linewidth=2,
        markersize=4,
        zorder=5,
    )

    # 绘制理想曲线（使用 target_gamma）
    ideal_L_bar = [(x / 100) ** target_gamma for x in x_values]
    self.gamma_ax.plot(
        x_values,
        ideal_L_bar,
        "r--",
        label=f"理想 (γ={target_gamma})",
        linewidth=2,
        alpha=0.7,
        zorder=3,
    )

    # 图例
    self.gamma_ax.legend(fontsize=9, loc="upper left", framealpha=0.95)

    # ========== 2. 清空并绘制右侧表格 ==========
    self.gamma_table_ax.clear()
    self.gamma_table_ax.axis("off")

    # 构建表格数据（4列）
    table_data = [["灰阶", "实测亮度\n(cd/m²)", "L_bar", "Gamma"]]

    for i, (x_val, L_val, result) in enumerate(
        zip(x_values, L_bar, results_with_gamma_list)
    ):
        # 提取实测亮度
        if isinstance(result, (list, tuple)) and len(result) >= 3:
            measured_lv = result[2]
            measured_lv_str = f"{measured_lv:.2f}"
        else:
            measured_lv_str = "--"

        # 提取 Gamma
        if isinstance(result, (list, tuple)) and len(result) >= 4:
            gamma = result[3]
            if gamma < 0.5 or gamma > 5.0:
                gamma_str = "--"
            else:
                gamma_str = f"{gamma:.2f}"
        else:
            gamma_str = "--"

        table_data.append(
            [
                f"{x_val:.0f}%",
                measured_lv_str,
                f"{L_val:.3f}",
                gamma_str,
            ]
        )

    # 绘制表格（4列）
    table = self.gamma_table_ax.table(
        cellText=table_data,
        cellLoc="center",
        loc="center",
        colWidths=[0.18, 0.28, 0.27, 0.27],
    )

    # 美化表格
    table.auto_set_font_size(False)
    table.set_fontsize(7.5)
    table.scale(1, 1.5)

    # 表头样式
    for i in range(4):
        cell = table[(0, i)]
        cell.set_facecolor("#4472C4")
        cell.set_text_props(weight="bold", color="white")

    # 数据行交替颜色
    for i in range(1, len(table_data)):
        for j in range(4):
            cell = table[(i, j)]
            if i % 2 == 0:
                cell.set_facecolor("#E7E6E6")
            else:
                cell.set_facecolor("#FFFFFF")

    # ========== 3. 总标题 ==========
    test_type_name = self.get_test_type_name(test_type)
    self.gamma_fig.suptitle(
        f"{test_type_name} - Gamma曲线",
        fontsize=12,
        y=0.98,
        fontweight="bold",
    )

    # ========== 4. 绘制到画布 ==========
    self.gamma_canvas.draw()
    self.chart_notebook.select(self.gamma_chart_frame)

    self.log_gui.log("Gamma曲线 + 数据表格绘制完成", level="success")


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