from .tx import *
from .modules import DPCDRegisters, LinkDisplayPortTx
from .modules.edid.edid import EdidSource
from .modules.vtg.pg import DpPatternGenerator
from .modules.hdcp import HdcpSource
from .modules.capturer.event.event_capturer import EventCapturer, EventFilterDpTx, EventFilterUsbc
from .modules.link.dp.private_link_tx_types import DPTXHWCaps
from UniTAP.libs.lib_tsi.tsi_types import TSI_DPTX_DPCD_BASE_W, TSI_DPTX_DPCD_DATA
from UniTAP.libs.lib_tsi.tsi_types import TSI_DPTX_HDCP_CAPS_R, TSI_DPTX_HDCP_STATUS_R
from UniTAP.libs.lib_tsi.tsi_types import TSI_DPTX_HW_CAPS_R
from UniTAP.libs.lib_tsi.tsi_io import PortProtocol


class DPTX(TX):

    """

    Main class of `DPTX` object.
    Inherited from class `TX`.
    Class describes capabilities of 300th (3XX) series of DP and USB-C devices in Source (TX - transmitter) role.

    Attributes:
        __link (LinkDisplayPortTx): object of `LinkDisplayPortTx`.
        __dpcd (DPCDRegisters): object of `DPCDRegisters`.
        __edid (EdidSource): object of `EdidSource`.
        __hdcp (HdcpSource): object of `HdcpSource`.
        __pg (DpPatternGenerator): object of `DpPatternGenerator`.
        __event_capturer (EventCapturer): object of `EventCapturer`.
    """

    __CHECK_EVENT_FILTER = {PortProtocol.DisplayPort: [EventFilterDpTx],
                            PortProtocol.DisplayPortThrowUSBC: [EventFilterDpTx, 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_DPTX_HW_CAPS_R, DPTXHWCaps)[1]
        self.__dpcd = DPCDRegisters(port_io, TSI_DPTX_DPCD_BASE_W, TSI_DPTX_DPCD_DATA)
        self.__link = LinkDisplayPortTx(port_io, self.__dpcd, hw_caps)
        self.__pg = DpPatternGenerator(port_io, memory_manager, 0)
        self.__edid = EdidSource(port_io, self.__link.max_stream_count)
        self.__hdcp = HdcpSource(port_io, TSI_DPTX_HDCP_CAPS_R, TSI_DPTX_HDCP_STATUS_R)
        # event_filters = [e(hw_caps) for e in self.__CHECK_EVENT_FILTER.get(port_io.protocol())]

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

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

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

        Should be used to work with DPCD registers on Source (TX - transmitter) side.

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

    @property
    def pg(self) -> DpPatternGenerator:
        """

        Should be used to control Pattern generator functionality on Source (TX - transmitter) side.

        Returns:
            object of `DpPatternGenerator` type.
        """
        return self.__pg

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

        Should be used to control link settings on Source (TX - transmitter) side.

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

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

        Should be used to work with EDID on Source (TX - transmitter) side.

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

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

        Should be used to work with HDCP on Source (TX - transmitter) side.

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

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

        Should be used to control `EventCapturer` on Source (TX - transmitter) role.

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