from http import HTTPStatus
from typing import Any
from urllib.parse import quote

import httpx

from ... import errors
from ...client import AuthenticatedClient, Client
from ...models.get_cp_form_form_response_400 import GetCpFormFormResponse400
from ...models.get_cp_form_form_response_500 import GetCpFormFormResponse500
from ...models.pkg_routes_cp_form_get_form_response import PkgRoutesCpFormGetFormResponse
from ...types import UNSET, Response, Unset


def _get_kwargs(
    form: str,
    *,
    team: str,
    task: str | Unset = UNSET,
) -> dict[str, Any]:

    params: dict[str, Any] = {}

    params["team"] = team

    params["task"] = task

    params = {k: v for k, v in params.items() if v is not UNSET and v is not None}

    _kwargs: dict[str, Any] = {
        "method": "get",
        "url": "/cp/form/{form}".format(
            form=quote(str(form), safe=""),
        ),
        "params": params,
    }

    return _kwargs


def _parse_response(
    *, client: AuthenticatedClient | Client, response: httpx.Response
) -> GetCpFormFormResponse400 | GetCpFormFormResponse500 | PkgRoutesCpFormGetFormResponse | None:
    if response.status_code == 200:
        response_200 = PkgRoutesCpFormGetFormResponse.from_dict(response.json())

        return response_200

    if response.status_code == 400:
        response_400 = GetCpFormFormResponse400.from_dict(response.json())

        return response_400

    if response.status_code == 500:
        response_500 = GetCpFormFormResponse500.from_dict(response.json())

        return response_500

    if client.raise_on_unexpected_status:
        raise errors.UnexpectedStatus(response.status_code, response.content)
    else:
        return None


def _build_response(
    *, client: AuthenticatedClient | Client, response: httpx.Response
) -> Response[GetCpFormFormResponse400 | GetCpFormFormResponse500 | PkgRoutesCpFormGetFormResponse]:
    return Response(
        status_code=HTTPStatus(response.status_code),
        content=response.content,
        headers=response.headers,
        parsed=_parse_response(client=client, response=response),
    )


def sync_detailed(
    form: str,
    *,
    client: AuthenticatedClient | Client,
    team: str,
    task: str | Unset = UNSET,
) -> Response[GetCpFormFormResponse400 | GetCpFormFormResponse500 | PkgRoutesCpFormGetFormResponse]:
    """Get form (public)

     Get form template with evaluated expressions and conditions. Public access without client
    authentication.

    Args:
        form (str):
        team (str):
        task (str | Unset):

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        Response[GetCpFormFormResponse400 | GetCpFormFormResponse500 | PkgRoutesCpFormGetFormResponse]
    """

    kwargs = _get_kwargs(
        form=form,
        team=team,
        task=task,
    )

    response = client.get_httpx_client().request(
        **kwargs,
    )

    return _build_response(client=client, response=response)


def sync(
    form: str,
    *,
    client: AuthenticatedClient | Client,
    team: str,
    task: str | Unset = UNSET,
) -> GetCpFormFormResponse400 | GetCpFormFormResponse500 | PkgRoutesCpFormGetFormResponse | None:
    """Get form (public)

     Get form template with evaluated expressions and conditions. Public access without client
    authentication.

    Args:
        form (str):
        team (str):
        task (str | Unset):

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        GetCpFormFormResponse400 | GetCpFormFormResponse500 | PkgRoutesCpFormGetFormResponse
    """

    return sync_detailed(
        form=form,
        client=client,
        team=team,
        task=task,
    ).parsed


async def asyncio_detailed(
    form: str,
    *,
    client: AuthenticatedClient | Client,
    team: str,
    task: str | Unset = UNSET,
) -> Response[GetCpFormFormResponse400 | GetCpFormFormResponse500 | PkgRoutesCpFormGetFormResponse]:
    """Get form (public)

     Get form template with evaluated expressions and conditions. Public access without client
    authentication.

    Args:
        form (str):
        team (str):
        task (str | Unset):

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        Response[GetCpFormFormResponse400 | GetCpFormFormResponse500 | PkgRoutesCpFormGetFormResponse]
    """

    kwargs = _get_kwargs(
        form=form,
        team=team,
        task=task,
    )

    response = await client.get_async_httpx_client().request(**kwargs)

    return _build_response(client=client, response=response)


async def asyncio(
    form: str,
    *,
    client: AuthenticatedClient | Client,
    team: str,
    task: str | Unset = UNSET,
) -> GetCpFormFormResponse400 | GetCpFormFormResponse500 | PkgRoutesCpFormGetFormResponse | None:
    """Get form (public)

     Get form template with evaluated expressions and conditions. Public access without client
    authentication.

    Args:
        form (str):
        team (str):
        task (str | Unset):

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        GetCpFormFormResponse400 | GetCpFormFormResponse500 | PkgRoutesCpFormGetFormResponse
    """

    return (
        await asyncio_detailed(
            form=form,
            client=client,
            team=team,
            task=task,
        )
    ).parsed
