# -*- coding: utf-8 -*-

from typing import Optional, Union

from ..exceptions import (
    RequestError, TooManyRedirectsError,
    RequestTimeoutError, YaDiskConnectionError
)

from ..types import TimeoutParameter

import httpx

__all__ = ["convert_httpx_exception", "convert_timeout", "convert_args_for_httpx"]

def convert_httpx_exception(exc: httpx.HTTPError) -> Union[RequestError, httpx.HTTPError]:
    if isinstance(exc, httpx.TooManyRedirects):
        return TooManyRedirectsError(str(exc))
    elif isinstance(exc, httpx.TimeoutException):
        return RequestTimeoutError(str(exc))
    elif isinstance(exc, httpx.ConnectError):
        return YaDiskConnectionError(str(exc))
    elif isinstance(exc, httpx.HTTPError):
        return RequestError(str(exc))
    else:
        return exc

def convert_timeout(timeout: TimeoutParameter) -> Optional[httpx.Timeout]:
    if timeout is None:
        return None

    if isinstance(timeout, (int, float)):
        return httpx.Timeout(timeout)

    connect, read = timeout

    return httpx.Timeout(connect=connect, pool=connect, read=read, write=read)

def convert_args_for_httpx(session, kwargs):
    if "timeout" in kwargs:
        kwargs["timeout"] = convert_timeout(kwargs["timeout"])

    if "data" in kwargs:
        kwargs["content"] = kwargs.pop("data")

    if "httpx_args" in kwargs:
        kwargs.update(kwargs.pop("httpx_args"))

    request_kwargs = {
        "content": kwargs.pop("content", None),
        "params": kwargs.get("params"),
        "cookies": kwargs.get("cookies"),
        "headers": kwargs.get("headers"),
        "extensions": kwargs.get("extensions"),
        "timeout": kwargs.get("timeout", session.timeout)
    }

    for i in request_kwargs:
        kwargs.pop(i, None)

    send_kwargs = kwargs

    return request_kwargs, send_kwargs
