from UniTAP.libs.lib_tsi.tsi_io import PortIO
from UniTAP.libs.lib_tsi.tsi import *
from .pr_private_types import *
from .pr_status import PanelReplayStatus
from .pr_config import PanelReplayConfig


class PanelReplay:
    """
    Class `PanelReplay` contains information about Panel Replay feature.
    - Read Panel Replay `status`.
    - Configure Panel Replay `config`.
    - Disable Panel Replay `disable`.
    - Enable Active mode `active_mode`.
    - Enable Inactive mode `inactive_mode`.
    - Enable selective update `selective_update`.
    """

    def __init__(self, port_io: PortIO, pg_caps):
        self.__io = port_io
        self.__status = PanelReplayStatus(self.__io)
        self.__config = PanelReplayConfig(self.__io, pg_caps)
        self.__caps = self.__config.pr_caps

    @property
    def status(self) -> PanelReplayStatus:
        """
        Returns object of class `PanelReplayStatus` for working with Panel Replay Status.

        Returns:
            object of `PanelReplayStatus` type
        """
        return self.__status

    @property
    def config(self) -> PanelReplayConfig:
        """
        Returns object of class `PanelReplayConfig` for configuration PR.

        Returns:
            object of `PanelReplayStatus` type
        """
        return self.__config

    def disable(self):
        """
        Disable Panel Replay
        """
        self.__write_pr_control(PrControl.Disable)

    def active_mode(self):
        """
        Enable active mode
        """
        self.__write_pr_control(PrControl.EnableActiveMode)

    def inactive_mode(self):
        """
        Enable inactive mode
        """
        self.__write_pr_control(PrControl.EnableInactiveMode)

    def selective_update(self):
        """
        Enable selective update mode (if device supports this feature)
        """
        if self.__caps.pr_flags().selective_update:
            self.__write_pr_control(PrControl.EnableSelectiveUpdate)

    def __write_pr_control(self, command: PrControl):
        self.__io.set(TSI_PG_STREAM_SELECT, 0)
        self.__io.set(TSI_PG_PR_CTRL_W, command.value)

