from __future__ import annotations

import copy
from dataclasses import dataclass

from app.data_range_converter import convert_pattern_params
from app.pq.pq_config import get_pattern


@dataclass
class PatternSession:
    mode: str
    test_type: str
    active_config: object
    pattern_params: list[list[int]]
    total_patterns: int
    display_names: list[str]


class PatternService:
    def __init__(self, app):
        self.app = app

    def _build_apply_config_error(self, test_type):
        timing = self.app.config.current_test_types.get(test_type, {}).get("timing", "-")
        detail = ""
        try:
            ctrl = getattr(self.app.signal_service.device, "raw_controller", None)
            if ctrl is not None:
                d = getattr(ctrl, "last_error", None)
                if d:
                    detail = f", detail={d}"
        except Exception:
            pass
        return f"UCD profile apply_config failed for {test_type}, timing={timing}{detail}"

    def prepare_session(self, mode, *, test_type=None, log_details=False):
        test_type = test_type or self.app.config.current_test_type
        if hasattr(self.app.config, "set_current_test_type"):
            self.app.config.set_current_test_type(test_type)
        if not self.app.config.set_current_pattern(mode):
            raise ValueError(f"未知的图案模式: {mode}")

        active_config = self.app.config
        source_params = self._get_source_pattern_params(mode)

        if test_type == "screen_module":
            screen_cfg = self.app.config.current_test_types.get("screen_module", {})
            color_space = (
                self.app.screen_module_color_space_var.get()
                if hasattr(self.app, "screen_module_color_space_var")
                else screen_cfg.get("colorimetry", "sRGB")
            )
            data_range = (
                self.app.screen_module_data_range_var.get()
                if hasattr(self.app, "screen_module_data_range_var")
                else screen_cfg.get("data_range", "Full")
            )
            bit_depth = (
                self.app.screen_module_bit_depth_var.get()
                if hasattr(self.app, "screen_module_bit_depth_var")
                else f"{int(screen_cfg.get('bpc', 8))}bit"
            )
            output_format = (
                self.app.screen_module_output_format_var.get()
                if hasattr(self.app, "screen_module_output_format_var")
                else screen_cfg.get("color_format", "RGB")
            )

            if log_details:
                self._log("=" * 50, "separator")
                self._log("设置屏模组信号格式:", "info")
                self._log("=" * 50, "separator")
                for label, value in [
                    ("色彩空间", color_space),
                    ("色彩格式", output_format),
                    ("数据范围", data_range),
                    ("编码位深", bit_depth),
                    ("Timing", self.app.config.current_test_types[test_type]["timing"]),
                ]:
                    self._log(f"  {label}: {value}", "info")
            if not self.app.signal_service.apply_config(active_config):
                raise RuntimeError(self._build_apply_config_error(test_type))
            success = self.app.signal_service.update_signal_format(
                color_space=color_space,
                data_range=data_range,
                bit_depth=bit_depth,
                output_format=output_format,
            )
            if log_details:
                self._log(
                    f"屏模组信号格式设置{'成功' if success else '失败'}",
                    "success" if success else "error",
                )

        elif test_type == "sdr_movie":
            data_range = self.app.sdr_data_range_var.get()
            if log_details:
                self._log("=" * 50, "separator")
                self._log("设置 SDR 信号格式:", "info")
                self._log("=" * 50, "separator")
                for label, value in [
                    ("色彩空间", self.app.sdr_color_space_var.get()),
                    ("色彩格式", self.app.sdr_output_format_var.get()),
                    ("Gamma",   self.app.sdr_gamma_type_var.get()),
                    ("数据范围", data_range),
                    ("编码位深", self.app.sdr_bit_depth_var.get()),
                ]:
                    self._log(f"  {label}: {value}", "info")
            converted_params = convert_pattern_params(
                source_params, data_range=data_range, verbose=False
            )
            active_config = self.app.config.get_temp_config_with_converted_params(
                mode=mode, converted_params=converted_params
            )
            if hasattr(active_config, "set_current_test_type"):
                active_config.set_current_test_type(test_type)
            if not self.app.signal_service.apply_config(active_config):
                raise RuntimeError(self._build_apply_config_error(test_type))
            success = self.app.signal_service.update_signal_format(
                color_space=self.app.sdr_color_space_var.get(),
                data_range=data_range,
                bit_depth=self.app.sdr_bit_depth_var.get(),
                output_format=self.app.sdr_output_format_var.get(),
            )
            if log_details:
                self._log(f"SDR 信号格式设置{'成功' if success else '失败'}", "success" if success else "error")
                self._log(f"图案参数已设置，共 {len(converted_params)} 个图案", "success")

        elif test_type == "hdr_movie":
            data_range = self.app.hdr_data_range_var.get()
            if log_details:
                self._log("=" * 50, "separator")
                self._log("设置 HDR 信号格式:", "info")
                self._log("=" * 50, "separator")
                for label, value in [
                    ("色彩空间", self.app.hdr_color_space_var.get()),
                    ("色彩格式", self.app.hdr_output_format_var.get()),
                    ("数据范围", data_range),
                    ("编码位深", self.app.hdr_bit_depth_var.get()),
                    ("MaxCLL",  self.app.hdr_maxcll_var.get()),
                    ("MaxFALL", self.app.hdr_maxfall_var.get()),
                ]:
                    self._log(f"  {label}: {value}", "info")
            converted_params = convert_pattern_params(
                source_params, data_range=data_range, verbose=False
            )
            active_config = self.app.config.get_temp_config_with_converted_params(
                mode=mode, converted_params=converted_params
            )
            if hasattr(active_config, "set_current_test_type"):
                active_config.set_current_test_type(test_type)
            if not self.app.signal_service.apply_config(active_config):
                raise RuntimeError(self._build_apply_config_error(test_type))
            success = self.app.signal_service.update_signal_format(
                color_space=self.app.hdr_color_space_var.get(),
                data_range=data_range,
                bit_depth=self.app.hdr_bit_depth_var.get(),
                output_format=self.app.hdr_output_format_var.get(),
                max_cll=self.app.hdr_maxcll_var.get(),
                max_fall=self.app.hdr_maxfall_var.get(),
            )
            if log_details:
                self._log(f"HDR 信号格式设置{'成功' if success else '失败'}", "success" if success else "error")
                self._log(f"图案参数已设置，共 {len(converted_params)} 个图案", "success")

        else:
            raise ValueError(f"不支持的测试类型: {test_type}")

        pattern_params = copy.deepcopy(active_config.current_pattern["pattern_params"])
        return PatternSession(
            mode=mode,
            test_type=test_type,
            active_config=active_config,
            pattern_params=pattern_params,
            total_patterns=len(pattern_params),
            display_names=self._get_display_names(mode, len(pattern_params)),
        )

    def send_session_pattern(self, session, index):
        if index < 0 or index >= session.total_patterns:
            raise IndexError(f"pattern 索引越界: {index}")

        pattern_param = session.pattern_params[index]
        if not self.app.signal_service.send_pattern_params(pattern_param):
            raise RuntimeError(f"发送 pattern 失败: {index}")
        return pattern_param

    def send_rgb(self, rgb, *, session=None, test_type=None):
        active_session = session or self.prepare_session(
            "rgb",
            test_type=test_type,
            log_details=False,
        )
        converted_rgb = self._convert_rgb_for_test_type(rgb, active_session.test_type)
        self.app.signal_service.send_solid_rgb(converted_rgb)
        return True

    def _get_source_pattern_params(self, mode):
        return copy.deepcopy(get_pattern(mode)["pattern_params"])

    def _get_display_names(self, mode, total_patterns):
        if mode == "accuracy":
            return self.app.config.get_accuracy_color_names()
        if mode == "custom" and hasattr(self.app.config, "get_temp_pattern_names"):
            return self.app.config.get_temp_pattern_names()
        return [f"P {index + 1}" for index in range(total_patterns)]

    def _convert_rgb_for_test_type(self, rgb, test_type):
        if test_type == "sdr_movie":
            data_range = self.app.sdr_data_range_var.get()
        elif test_type == "hdr_movie":
            data_range = self.app.hdr_data_range_var.get()
        else:
            data_range = "Full"

        return convert_pattern_params([list(rgb)], data_range=data_range, verbose=False)[0]

    def _log(self, message, level):
        if hasattr(self.app, "log_gui"):
            self.app.log_gui.log(message, level=level)