#!/usr/bin/env python
# cardinal_pythonlib/typing_helpers.py

"""
===============================================================================

    Original code copyright (C) 2009-2021 Rudolf Cardinal (rudolf@pobox.com).

    This file is part of cardinal_pythonlib.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        https://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

===============================================================================

**Methods and unusual types for type hints.**

"""

from abc import abstractmethod
import csv
from typing import (
    Any,
    Iterator,
    List,
    Optional,
    Sequence,
    Tuple,
    Type,
    TYPE_CHECKING,
    TypeVar,
    Union,
)


# =============================================================================
# Constants
# =============================================================================

T = TypeVar('T')


# =============================================================================
# with_typehint
# =============================================================================

def with_typehint(baseclass: Type[T]) -> Type[T]:
    """
    Useful function to make mixins with type hints from a base class.

    From
    https://stackoverflow.com/questions/51930339/how-do-i-correctly-add-type-hints-to-mixin-classes.

    Specimen usage:

    .. code-block:: python

        class MyMixin1(with_typehint(SomeBaseClass))):
            # ...

    """  # noqa
    if TYPE_CHECKING:
        return baseclass
    return object


def with_typehints(*baseclasses: Type[T]) -> Type[T]:
    """
    Useful function to make mixins with type hints from multiple base classes.

    From
    https://stackoverflow.com/questions/51930339/how-do-i-correctly-add-type-hints-to-mixin-classes.

    Specimen usage:

    .. code-block:: python

        class MyMixin2(*with_typehints(SomeBaseClass, AnotherBaseClass))):
            # ...

    """  # noqa
    if TYPE_CHECKING:
        return baseclasses
    return object


# =============================================================================
# csv.writer
# =============================================================================

class CSVWriterType(object):
    """
    Type hint for the result of ``csv.writer()``

    See https://stackoverflow.com/questions/51264355/how-to-type-annotate-object-returned-by-csv-writer
    """  # noqa

    @abstractmethod
    def writerow(self, row: List[str]) -> None:
        pass

    @abstractmethod
    def writerows(self, rows: List[List[str]]) -> None:
        pass

    @property
    @abstractmethod
    def dialect(self) -> csv.Dialect:
        pass


# =============================================================================
# Pep249DatabaseConnectionType
# =============================================================================

class Pep249DatabaseConnectionType(object):
    """
    Type hint for a database connection compliant with PEP 249. See
    https://www.python.org/dev/peps/pep-0249/.

    Not supported:

    - https://www.python.org/dev/peps/pep-0249/#optional-error-handling-extensions
    - https://www.python.org/dev/peps/pep-0249/#optional-two-phase-commit-extensions
    """  # noqa

    @abstractmethod
    def close(self) -> None:
        """
        See https://www.python.org/dev/peps/pep-0249/#connection-objects
        """
        pass

    @abstractmethod
    def commit(self) -> None:
        """
        See https://www.python.org/dev/peps/pep-0249/#connection-objects
        """
        pass

    @abstractmethod
    def rollback(self) -> None:
        """
        See https://www.python.org/dev/peps/pep-0249/#connection-objects
        """
        pass

    @abstractmethod
    def cursor(self) -> "Pep249DatabaseCursorType":
        """
        See https://www.python.org/dev/peps/pep-0249/#connection-objects
        """
        pass

    @property
    @abstractmethod
    def messages(self) -> List[Tuple[Type, Any]]:
        """
        See
        https://www.python.org/dev/peps/pep-0249/#optional-db-api-extensions
        """
        pass


# =============================================================================
# Pep249DatabaseConnectionType
# =============================================================================

_DATABASE_ROW_TYPE = Sequence[Any]