"""测试结果图表 PNG 导出。"""
import os


# (item_key, fig_attr, filename, allowed_test_types_or_None, default_bbox_inches_auto)
IMAGE_SPECS = [
    ("gamut", "gamut_fig", "色域测试结果.png", None, True),
    ("gamma", "gamma_fig", "Gamma曲线测试结果.png",
     {"screen_module", "sdr_movie"}, True),
    ("eotf", "eotf_fig", "EOTF曲线测试结果.png", {"hdr_movie"}, True),
    ("cct", "cct_fig", "色度一致性测试结果.png", None, True),
    ("contrast", "contrast_fig", "对比度测试结果.png", None, False),
    ("accuracy", "accuracy_fig", "色准测试结果.png",
     {"sdr_movie", "hdr_movie"}, True),
]


def save_result_images(result_dir, current_test_type, selected_items,
                       figure_provider, log):
    """根据测试类型和已选项将各测试图表保存为 PNG。

    figure_provider: callable(attr_name) -> matplotlib Figure 或 None
    log: callable(msg)
    """
    for item_key, fig_attr, filename, allowed_types, default_bbox in IMAGE_SPECS:
        if item_key not in selected_items:
            continue
        if allowed_types is not None and current_test_type not in allowed_types:
            continue
        fig = figure_provider(fig_attr)
        if fig is None:
            continue
        path = os.path.join(result_dir, filename)
        if default_bbox:
            fig.savefig(path, dpi=300)
        else:
            fig.savefig(path, dpi=300, bbox_inches="tight")
        log(f"已保存: {filename}")
