from abc import ABC, abstractmethod
from typing import Any, Dict, Tuple


class AbstractAsyncSession(ABC):
    started: bool = False  # Changes according to implementation

    @abstractmethod
    async def start(self, instructions: Dict[str, Tuple[Tuple, Dict[str, Any]]]):
        """
        :param instructions:
        Settings from TelegramClient. Example:
            {
                <function> SessionMethod: <tuple[2]> (
                    <tuple...> (...*_params_args),
                    <dict> {...*_params_kwargs}
                )
            }
        """
        raise NotImplementedError

    @abstractmethod
    async def set_dc(self, dc_id, server_address, port):
        """
        Sets the information of the data center address and port that
        the library should connect to, as well as the data center ID,
        which is currently unused.
        """
        raise NotImplementedError

    @abstractmethod
    async def dc_id(self):
        """
        Returns the currently-used data center ID.
        """
        raise NotImplementedError

    @abstractmethod
    async def server_address(self):
        """
        Returns the server address where the library should connect to.
        """
        raise NotImplementedError

    @abstractmethod
    async def port(self):
        """
        Returns the port to which the library should connect to.
        """
        raise NotImplementedError

    @abstractmethod
    async def auth_key(self):
        """
        Returns an ``AuthKey`` instance associated with the saved
        data center, or `None` if a new one should be generated.
        """
        raise NotImplementedError

    @abstractmethod
    async def set_auth_key(self, auth_key: str):
        """
        Sets the ``AuthKey`` to be used for the saved data center.
        """
        raise NotImplementedError

    @abstractmethod
    async def takeout_id(self):
        """
        Returns an ID of the takeout process initialized for this session,
        or `None` if there's no were any unfinished takeout requests.
        """
        raise NotImplementedError

    @abstractmethod
    async def set_takeout_id(self, takeout_id: int):
        """
        Sets the ID of the unfinished takeout process for this session.
        """
        raise NotImplementedError

    @abstractmethod
    async def get_update_state(self, entity_id):
        """
        Returns the ``UpdateState`` associated with the given `entity_id`.
        If the `entity_id` is 0, it should return the ``UpdateState`` for
        no specific channel (the "general" state). If no state is known
        it should ``return None``.
        """
        raise NotImplementedError

    @abstractmethod
    async def set_update_state(self, entity_id, state):
        """
        Sets the given ``UpdateState`` for the specified `entity_id`, which
        should be 0 if the ``UpdateState`` is the "general" state (and not
        for any specific channel).
        """
        raise NotImplementedError

    @abstractmethod
    async def close(self):
        """
        Called on client disconnection. Should be used to
        free any used resources. Can be left empty if none.
        """

    @abstractmethod
    async def save(self):
        """
        Called whenever important properties change. It should
        make persist the relevant session information to disk.
        """
        raise NotImplementedError

    @abstractmethod
    async def delete(self):
        """
        Called upon client.log_out(). Should delete the stored
        information from disk since it's not valid anymore.
        """
        raise NotImplementedError

    @classmethod
    async def list_sessions(cls):
        """
        Lists available sessions. Not used by the library itself.
        """
        return []

    @abstractmethod
    async def process_entities(self, tlo):
        """
        Processes the input ``TLObject`` or ``list`` and saves
        whatever information is relevant (e.g., ID or access hash).
        """
        raise NotImplementedError

    @abstractmethod
    async def get_input_entity(self, key):
        """
        Turns the given key into an ``InputPeer`` (e.g. ``InputPeerUser``).
        The library uses this method whenever an ``InputPeer`` is needed
        to suit several purposes (e.g. user only provided its ID or wishes
        to use a cached username to avoid extra RPC).
        """
        raise NotImplementedError

    @abstractmethod
    async def cache_file(self, md5_digest, file_size, instance):
        """
        Caches the given file information persistently, so that it
        doesn't need to be re-uploaded in case the file is used again.

        The ``instance`` will be either an ``InputPhoto`` or ``InputDocument``,
        both with an ``.id`` and ``.access_hash`` attributes.
        """
        raise NotImplementedError

    @abstractmethod
    async def get_file(self, md5_digest, file_size, cls):
        """
        Returns an instance of ``cls`` if the ``md5_digest`` and ``file_size``
        match an existing saved record. The class will either be an
        ``InputPhoto`` or ``InputDocument``, both with two parameters
        ``id`` and ``access_hash`` in that order.
        """
        raise NotImplementedError
