"""Functions for helping manage the B2RRemote and RizomInteraface connections."""

from typing import Tuple

from rizomuvbridge_ultimate.rizom.live_link.rizom_link import RizomInterface
from rizomuvbridge_ultimate.rizom.script_link.b2r_remote import B2RRemote
from rizomuvbridge_ultimate.rizom.script_link.rizom_cmds import RizomModel


def connect_b2r(interface: "B2RRemote") -> tuple[bool, str]:
    """Attempt to connect to a running Rizom instance through B2RRemote.

    Args:
    ----
        interface (B2RRemote): B2RRemote instance.


    Returns:
    -------
        Tuple[bool, str]: True if connection success, info message

    """
    connected = interface.check_process()
    if not connected:
        return False, "B2RRemote: Could not connect to a remote Rizom instance"
    return True, "B2RRemote: Connected to remote Rizom instance"


def connect_rizomuv_link(interface: "RizomInterface") -> tuple[bool, str]:
    """Attempt to connect to a running Rizom instance through RizomUVLink.

    Args:
    ----
        interface (RizomInterface): RizomInterface instance.

    Returns:
    -------
        Tuple[bool, str]: True if connection success, info message

    """
    running = interface.connect_to_running()
    if not running:
        return False, "RizomUVLink: Could not connect to a live Rizom instance"
    return True, "RizomUVLink: Connected to live Rizom instance"


def connect_to_rizom(interface: RizomInterface | B2RRemote) -> Tuple[bool, str]:
    """Contextually connect to RizomUV using B2RRemote or RizomUVLink.

    Args:
    ----
        interface (RizomInterface | B2RRemote): Connection object for communication with Rizom.

    Returns:
    -------
        Tuple[bool, str]: True if connected, info message

    """
    if isinstance(interface, B2RRemote):
        return connect_b2r(interface)
    return connect_rizomuv_link(interface)


def request_import_with_link(interface: RizomInterface):
    """Signal RizomUV to save the file and send information to Blender.

    Args:
    ----
        interface (RizomInterface): Instaance of RizomInterface class.

    Returns:
    -------
        RizomInterface: Rizom data model containing import data.

    """
    interface.save_file()
    return RizomModel(
        file_path=interface.get_file(),
        loaded_objects=interface.get_objects(),
        uvmap=interface.get_current_uvmap(),
    )


def request_import_with_b2r(interface: B2RRemote):
    """Signal RizomUV to save the file and send information to Blender.

    Args:
    ----
        interface (B2RRemote): Instaance of B2RRemote class.

    Returns:
    -------
        RizomModel: Rizom data model containing import data.

    """
    return interface.request_import()


def request_import_from_rizom(ruv_interface: RizomInterface | B2RRemote):
    """Request import from Rizom.

    Signals RizomUV to save the current file and transfer data to Blender
    which can be used to prepare the scene for import.

    Args:
    ----
        ruv_interface (RizomInterface | B2RRemote): Connection object for communication with Rizom.

    """
    if isinstance(ruv_interface, B2RRemote):
        return request_import_with_b2r(ruv_interface)
    return request_import_with_link(ruv_interface)
