from .rx import *
from .modules.dpcd.dpcd import DPCDRegisters
from .modules.link.dp.link_rx import LinkDisplayPortRx
from .modules.edid.edid import EdidSink
from .modules.hdcp import HdcpSink
from .modules.capturer.event.event_capturer import EventCapturer, EventFilterDpRx, EventFilterUsbc
from .modules.link.dp.private_link_rx_types import DPRXHWCaps
from UniTAP.libs.lib_tsi.tsi_types import TSI_DPRX_HDCP_CAPS_R, TSI_DPRX_HDCP_STATUS_R
from UniTAP.libs.lib_tsi.tsi_types import TSI_DPRX_DPCD_BASE_W, TSI_DPRX_DPCD_DATA
from UniTAP.libs.lib_tsi.tsi_types import TSI_PDC_LOG_CONTROL, TSI_DPRX_HW_CAPS_R
from UniTAP.libs.lib_tsi.tsi_io import PortProtocol


class DPRX(RX):

    """

    Main class of `DPRX` object.
    Inherited from class `RX`.
    Class describes capabilities of 300th (3XX) series of DP and USB-C devices in Sink (RX - receiver) role.

    Attributes:
        __link (LinkDisplayPortRx): object of `LinkDisplayPortRx`.
        __dpcd (DPCDRegisters): object of `DPCDRegisters`.
        __edid (EdidSink): object of `EdidSink`.
        __hdcp (HdcpSink): object of `HdcpSink`.
        __event_capturer (EventCapturer): object of `EventCapturer`.
        __video_capturer (VideoCapturer): object of `VideoCapturerDP`.
    """

    __CHECK_EVENT_FILTER = {PortProtocol.DisplayPort: [EventFilterDpRx],
                            PortProtocol.DisplayPortThrowUSBC: [EventFilterDpRx, EventFilterUsbc]}

    def __init__(self, port_io: PortIO, memory_manager: MemoryManager, capturer: Capturer):
        super().__init__(port_io, memory_manager, capturer)

        hw_caps = port_io.get(TSI_DPRX_HW_CAPS_R, DPRXHWCaps)[1]
        self.__dpcd = DPCDRegisters(port_io, TSI_DPRX_DPCD_BASE_W, TSI_DPRX_DPCD_DATA)
        self.__link = LinkDisplayPortRx(port_io, hw_caps, self.__dpcd)
        self.__edid = EdidSink(port_io, self.__link.status.mst_stream_count)
        self.__hdcp = HdcpSink(port_io, TSI_DPRX_HDCP_CAPS_R, TSI_DPRX_HDCP_STATUS_R)
        self.__video_capturer = VideoCapturerDP(capturer, hw_caps.mst_stream_count)

        event_filters = []
        for item in self.__CHECK_EVENT_FILTER.get(port_io.protocol()):
            if item == EventFilterDpRx:
                event_filters.append(item(hw_caps))
            else:
                event_filters.append(item(None))

        self.__event_capturer = EventCapturer(capturer, port_io.index(), event_filters)

    @property
    def link(self) -> LinkDisplayPortRx:
        """

        Should be used to control link capabilities on Sink (RX - receiver) role.

        Returns:
            object of `LinkDisplayPortRx` type.
        """
        return self.__link

    @property
    def dpcd(self) -> DPCDRegisters:
        """

        Should be used to work with DPCD registers on Sink (RX - receiver) role.

        Returns:
            object of `DPCDRegisters` type.
        """
        return self.__dpcd

    @property
    def edid(self) -> EdidSink:
        """

        Should be used to work with EDID on Sink (RX - receiver) role.

        Returns:
            object of `EdidSink` type.
        """
        return self.__edid

    @property
    def hdcp(self) -> HdcpSink:
        """

        Should be used to work with HDCP on Sink (RX - receiver) role.

        Returns:
            object of `HdcpSink`.
        """
        return self.__hdcp

    @property
    def event_capturer(self) -> EventCapturer:
        """

        Should be used to control `EventCapturer` on Sink (RX - receiver) role.

        Returns:
            object of `EventCapturer` type.
        """
        return self.__event_capturer

    @property
    def video_capturer(self) -> VideoCapturerDP:
        """

        Should be used to control `VideoCapturerDP` on Sink (RX - receiver) role.

        Returns:
            object of `VideoCapturerDP` type.
        """
        return self.__video_capturer
