import random
from time import sleep
from typing import Any, Dict

import httpx

from ...client import AuthenticatedClient
from ...models.application_in import ApplicationIn
from ...models.application_out import ApplicationOut
from ...models.http_error import HttpError
from ...models.http_validation_error import HTTPValidationError


def _get_kwargs(
    app_id: str,
    *,
    client: AuthenticatedClient,
    json_body: ApplicationIn,
) -> Dict[str, Any]:
    url = "{}/api/v1/app/{app_id}/".format(client.base_url, app_id=app_id)

    headers: Dict[str, str] = client.get_headers()
    cookies: Dict[str, Any] = client.get_cookies()

    json_json_body = json_body.to_dict()

    return {
        "method": "put",
        "url": url,
        "headers": headers,
        "cookies": cookies,
        "timeout": client.get_timeout(),
        "json": json_json_body,
    }


def _parse_response(*, response: httpx.Response) -> ApplicationOut:
    if response.status_code == 401:
        raise HttpError(response.json(), response.status_code)
    if response.status_code == 403:
        raise HttpError(response.json(), response.status_code)
    if response.status_code == 404:
        raise HttpError(response.json(), response.status_code)
    if response.status_code == 409:
        raise HttpError(response.json(), response.status_code)
    if response.status_code == 422:
        raise HTTPValidationError(response.json(), response.status_code)
    if response.status_code == 429:
        raise HttpError(response.json(), response.status_code)
    response_200 = ApplicationOut.from_dict(response.json())

    return response_200


sleep_time = 0.05
num_retries = 3


def sync_detailed(
    app_id: str,
    *,
    client: AuthenticatedClient,
    json_body: ApplicationIn,
) -> ApplicationOut:
    """Update Application

     Update an application.

    Args:
        app_id (str): The app's ID or UID Example: unique-app-identifier.
        json_body (ApplicationIn):

    Returns:
        ApplicationOut
    """

    kwargs = _get_kwargs(
        app_id=app_id,
        client=client,
        json_body=json_body,
    )

    kwargs["headers"] = {"svix-req-id": f"{random.getrandbits(32)}", **kwargs["headers"]}

    retry_count = 0
    for retry in range(num_retries):
        response = httpx.request(
            verify=client.verify_ssl,
            **kwargs,
        )
        if response.status_code >= 500 and retry < num_retries:
            retry_count += 1
            kwargs["headers"]["svix-retry-count"] = str(retry_count)
            sleep(sleep_time)
            sleep_time * 2
        else:
            break

    return _parse_response(response=response)


def sync(
    app_id: str,
    *,
    client: AuthenticatedClient,
    json_body: ApplicationIn,
) -> ApplicationOut:
    """Update Application

     Update an application.

    Args:
        app_id (str): The app's ID or UID Example: unique-app-identifier.
        json_body (ApplicationIn):

    Returns:
        ApplicationOut
    """

    return sync_detailed(
        app_id=app_id,
        client=client,
        json_body=json_body,
    )


async def asyncio_detailed(
    app_id: str,
    *,
    client: AuthenticatedClient,
    json_body: ApplicationIn,
) -> ApplicationOut:
    """Update Application

     Update an application.

    Args:
        app_id (str): The app's ID or UID Example: unique-app-identifier.
        json_body (ApplicationIn):

    Returns:
        ApplicationOut
    """

    kwargs = _get_kwargs(
        app_id=app_id,
        client=client,
        json_body=json_body,
    )

    kwargs["headers"] = {"svix-req-id": f"{random.getrandbits(32)}", **kwargs["headers"]}

    async with httpx.AsyncClient(verify=client.verify_ssl) as _client:
        retry_count = 0
        for retry in range(num_retries):
            response = await _client.request(**kwargs)
            if response.status_code >= 500 and retry < num_retries:
                retry_count += 1
                kwargs["headers"]["svix-retry-count"] = str(retry_count)
                sleep(sleep_time)
                sleep_time * 2
            else:
                break

    return _parse_response(response=response)


async def asyncio(
    app_id: str,
    *,
    client: AuthenticatedClient,
    json_body: ApplicationIn,
) -> ApplicationOut:
    """Update Application

     Update an application.

    Args:
        app_id (str): The app's ID or UID Example: unique-app-identifier.
        json_body (ApplicationIn):

    Returns:
        ApplicationOut
    """

    return await asyncio_detailed(
        app_id=app_id,
        client=client,
        json_body=json_body,
    )
