# This file was auto-generated by Fern from our API Definition.

import typing
from json.decoder import JSONDecodeError

from ..core.api_error import ApiError
from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.http_response import AsyncHttpResponse, HttpResponse
from ..core.pydantic_utilities import parse_obj_as
from ..core.request_options import RequestOptions
from ..errors.unprocessable_entity_error import UnprocessableEntityError
from ..types.http_validation_error import HttpValidationError
from ..types.response_display_record_result import ResponseDisplayRecordResult
from .types.record_action import RecordAction

# this is used as the default value for optional parameters
OMIT = typing.cast(typing.Any, ...)


class RawDisplayClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    def record(
        self,
        *,
        action: RecordAction,
        save_path: typing.Optional[str] = OMIT,
        fps: typing.Optional[int] = OMIT,
        crf: typing.Optional[int] = OMIT,
        max_duration: typing.Optional[float] = OMIT,
        width: typing.Optional[int] = OMIT,
        height: typing.Optional[int] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[ResponseDisplayRecordResult]:
        """
        Control display screen recording (start/stop/status).

        Records the entire X11 desktop using ffmpeg x11grab,
        including browser UI, multiple tabs, popups, terminal, etc.

        Parameters
        ----------
        action : RecordAction
            Recording action: start, stop, or status

        save_path : typing.Optional[str]
            Output file path (default: /tmp/recordings/recording_{timestamp}.mp4)

        fps : typing.Optional[int]
            Frames per second

        crf : typing.Optional[int]
            H.264 CRF quality (0=lossless, 51=worst)

        max_duration : typing.Optional[float]
            Max recording duration in seconds

        width : typing.Optional[int]
            Video width in pixels (auto-detected from X11 if omitted)

        height : typing.Optional[int]
            Video height in pixels (auto-detected from X11 if omitted)

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[ResponseDisplayRecordResult]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/display/record",
            method="POST",
            json={
                "action": action,
                "save_path": save_path,
                "fps": fps,
                "crf": crf,
                "max_duration": max_duration,
                "width": width,
                "height": height,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ResponseDisplayRecordResult,
                    parse_obj_as(
                        type_=ResponseDisplayRecordResult,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)


class AsyncRawDisplayClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def record(
        self,
        *,
        action: RecordAction,
        save_path: typing.Optional[str] = OMIT,
        fps: typing.Optional[int] = OMIT,
        crf: typing.Optional[int] = OMIT,
        max_duration: typing.Optional[float] = OMIT,
        width: typing.Optional[int] = OMIT,
        height: typing.Optional[int] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[ResponseDisplayRecordResult]:
        """
        Control display screen recording (start/stop/status).

        Records the entire X11 desktop using ffmpeg x11grab,
        including browser UI, multiple tabs, popups, terminal, etc.

        Parameters
        ----------
        action : RecordAction
            Recording action: start, stop, or status

        save_path : typing.Optional[str]
            Output file path (default: /tmp/recordings/recording_{timestamp}.mp4)

        fps : typing.Optional[int]
            Frames per second

        crf : typing.Optional[int]
            H.264 CRF quality (0=lossless, 51=worst)

        max_duration : typing.Optional[float]
            Max recording duration in seconds

        width : typing.Optional[int]
            Video width in pixels (auto-detected from X11 if omitted)

        height : typing.Optional[int]
            Video height in pixels (auto-detected from X11 if omitted)

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[ResponseDisplayRecordResult]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/display/record",
            method="POST",
            json={
                "action": action,
                "save_path": save_path,
                "fps": fps,
                "crf": crf,
                "max_duration": max_duration,
                "width": width,
                "height": height,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ResponseDisplayRecordResult,
                    parse_obj_as(
                        type_=ResponseDisplayRecordResult,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
