from UniTAP.common import ColorInfo


def get_vm_color_format(vsc_sdp_db16: int) -> ColorInfo.ColorFormat:
    color_format_value = vsc_sdp_db16 >> 4 & 0xf

    if color_format_value == 0:
        return ColorInfo.ColorFormat.CF_RGB
    elif color_format_value == 1:
        return ColorInfo.ColorFormat.CF_YCbCr_444
    elif color_format_value == 2:
        return ColorInfo.ColorFormat.CF_YCbCr_422
    elif color_format_value == 3:
        return ColorInfo.ColorFormat.CF_YCbCr_420
    elif color_format_value == 4:
        return ColorInfo.ColorFormat.CF_Y_ONLY
    elif color_format_value == 5:
        return ColorInfo.ColorFormat.CF_RAW
    else:
        return ColorInfo.ColorFormat.CF_UNKNOWN


def get_vm_colorimetry(vsc_sdp_db16: int) -> ColorInfo.Colorimetry:
    color_format_value = vsc_sdp_db16 >> 4 & 0xf
    colorimetry_value = vsc_sdp_db16 & 0xf

    if color_format_value == 0:
        if colorimetry_value == 0:
            return ColorInfo.Colorimetry.CM_sRGB
        elif colorimetry_value == 1:
            return ColorInfo.Colorimetry.CM_RGB_WIDE_GAMUT_FIX
        elif colorimetry_value == 2:
            return ColorInfo.Colorimetry.CM_RGB_WIDE_GAMUT_FLT
        elif colorimetry_value == 3:
            return ColorInfo.Colorimetry.CM_AdobeRGB
        elif colorimetry_value == 4:
            return ColorInfo.Colorimetry.CM_DCI_P3
        elif colorimetry_value == 5:
            return ColorInfo.Colorimetry.CM_CUSTOM_COLOR_PROFILE
        elif colorimetry_value == 6:
            return ColorInfo.Colorimetry.CM_ITUR_BT2020_RGB
        else:
            return ColorInfo.Colorimetry.CM_RESERVED

    elif color_format_value in [1, 2, 3]:
        if colorimetry_value == 0:
            return ColorInfo.Colorimetry.CM_ITUR_BT601
        elif colorimetry_value == 1:
            return ColorInfo.Colorimetry.CM_ITUR_BT709
        elif colorimetry_value == 2:
            return ColorInfo.Colorimetry.CM_xvYCC601
        elif colorimetry_value == 3:
            return ColorInfo.Colorimetry.CM_xvYCC709
        elif colorimetry_value == 4:
            return ColorInfo.Colorimetry.CM_sYCC601
        elif colorimetry_value == 5:
            return ColorInfo.Colorimetry.CM_opYCC601
        elif colorimetry_value == 6:
            return ColorInfo.Colorimetry.CM_ITUR_BT2020_YcCbcCrc
        elif colorimetry_value == 7:
            return ColorInfo.Colorimetry.CM_ITUR_BT2020_YCbCr
        else:
            return ColorInfo.Colorimetry.CM_RESERVED

    elif color_format_value == 4:
        if colorimetry_value == 0:
            return ColorInfo.Colorimetry.CM_DCI_P3
        else:
            return ColorInfo.Colorimetry.CM_RESERVED

    elif color_format_value == 5:
        if colorimetry_value == 0:
            return ColorInfo.Colorimetry.CM_CUSTOM_COLOR_PROFILE
        else:
            return ColorInfo.Colorimetry.CM_RESERVED

    else:
        return ColorInfo.Colorimetry.CM_RESERVED


def get_vm_bpc(vsc_sdp_db16: int, vsc_sdp_db17: int) -> int:
    color_format_value = vsc_sdp_db16 >> 4 & 0xf
    bpc_value = vsc_sdp_db17 & 0x7

    if color_format_value == 0:
        if bpc_value == 0:
            return 6
        elif bpc_value == 1:
            return 8
        elif bpc_value == 2:
            return 10
        elif bpc_value == 3:
            return 12
        elif bpc_value == 4:
            return 16
        else:
            return 0

    elif color_format_value in [1, 2, 3, 4]:
        if bpc_value == 1:
            return 8
        elif bpc_value == 2:
            return 10
        elif bpc_value == 3:
            return 12
        elif bpc_value == 4:
            return 16
        else:
            return 0

    elif color_format_value == 5:
        if bpc_value == 1:
            return 6
        elif bpc_value == 2:
            return 7
        elif bpc_value == 3:
            return 8
        elif bpc_value == 4:
            return 10
        elif bpc_value == 5:
            return 12
        elif bpc_value == 6:
            return 14
        elif bpc_value == 7:
            return 16
        else:
            return 0

    else:
        return 0


def get_vm_dynamic_range(vsc_sdp_db17: int) -> ColorInfo.DynamicRange:
    if vsc_sdp_db17 >> 7 & 1:
        return ColorInfo.DynamicRange.DR_CTA
    else:
        return ColorInfo.DynamicRange.DR_VESA
