"""通用 UCD323/UCDController 辅助函数。

保留为兼容层和薄代理，避免业务模块直接依赖控制器内部实现细节。
"""


def get_tx_modules(ucd):
    """根据当前接口返回 (pg, ag) 模块。"""
    return ucd.get_tx_modules()


def get_current_resolution(ucd, default=(3840, 2160)):
    """从 UCD 当前 timing 获取 (width, height)，失败时返回 default。"""
    return ucd.get_current_resolution(default)


def send_image_pattern(ucd, image_path):
    """通过 UCDController 发送一张本地图片作为显示 Pattern。"""
    if not getattr(ucd, "status", False):
        return False

    send_via_controller = getattr(ucd, "send_image_pattern", None)
    if not callable(send_via_controller):
        return False
    return bool(send_via_controller(image_path))


def send_solid_rgb_pattern(ucd, rgb, *, raise_on_error=False):
    """通过 UCDController 当前状态发送一组纯色 RGB Pattern。"""
    if not getattr(ucd, "status", False):
        if raise_on_error:
            raise RuntimeError("UCD 未连接，无法发送纯色 Pattern")
        return False

    send_via_controller = getattr(ucd, "send_solid_rgb_pattern", None)
    if not callable(send_via_controller):
        if raise_on_error:
            raise RuntimeError("UCDController 未实现 send_solid_rgb_pattern")
        return False

    ok = bool(send_via_controller(list(rgb)))
    if not ok and raise_on_error:
        raise RuntimeError(f"发送纯色 Pattern 失败: {rgb}")
    return ok


