from __future__ import annotations

from collections.abc import Mapping
from typing import TYPE_CHECKING, Any, TypeVar, cast

from attrs import define as _attrs_define
from attrs import field as _attrs_field

from ..models.images_response_background_type_0 import ImagesResponseBackgroundType0
from ..models.images_response_output_format_type_0 import ImagesResponseOutputFormatType0
from ..models.images_response_quality_type_0 import ImagesResponseQualityType0
from ..models.images_response_size_type_0 import ImagesResponseSizeType0
from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.image import Image
    from ..models.usage import Usage


T = TypeVar("T", bound="ImagesResponse")


@_attrs_define
class ImagesResponse:
    """The response from the image generation endpoint.

    Attributes:
        created (int):
        background (ImagesResponseBackgroundType0 | None | Unset):
        data (list[Image] | None | Unset):
        output_format (ImagesResponseOutputFormatType0 | None | Unset):
        quality (ImagesResponseQualityType0 | None | Unset):
        size (ImagesResponseSizeType0 | None | Unset):
        usage (None | Unset | Usage):
    """

    created: int
    background: ImagesResponseBackgroundType0 | None | Unset = UNSET
    data: list[Image] | None | Unset = UNSET
    output_format: ImagesResponseOutputFormatType0 | None | Unset = UNSET
    quality: ImagesResponseQualityType0 | None | Unset = UNSET
    size: ImagesResponseSizeType0 | None | Unset = UNSET
    usage: None | Unset | Usage = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        from ..models.usage import Usage

        created = self.created

        background: None | str | Unset
        if isinstance(self.background, Unset):
            background = UNSET
        elif isinstance(self.background, ImagesResponseBackgroundType0):
            background = self.background.value
        else:
            background = self.background

        data: list[dict[str, Any]] | None | Unset
        if isinstance(self.data, Unset):
            data = UNSET
        elif isinstance(self.data, list):
            data = []
            for data_type_0_item_data in self.data:
                data_type_0_item = data_type_0_item_data.to_dict()
                data.append(data_type_0_item)

        else:
            data = self.data

        output_format: None | str | Unset
        if isinstance(self.output_format, Unset):
            output_format = UNSET
        elif isinstance(self.output_format, ImagesResponseOutputFormatType0):
            output_format = self.output_format.value
        else:
            output_format = self.output_format

        quality: None | str | Unset
        if isinstance(self.quality, Unset):
            quality = UNSET
        elif isinstance(self.quality, ImagesResponseQualityType0):
            quality = self.quality.value
        else:
            quality = self.quality

        size: None | str | Unset
        if isinstance(self.size, Unset):
            size = UNSET
        elif isinstance(self.size, ImagesResponseSizeType0):
            size = self.size.value
        else:
            size = self.size

        usage: dict[str, Any] | None | Unset
        if isinstance(self.usage, Unset):
            usage = UNSET
        elif isinstance(self.usage, Usage):
            usage = self.usage.to_dict()
        else:
            usage = self.usage

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update(
            {
                "created": created,
            }
        )
        if background is not UNSET:
            field_dict["background"] = background
        if data is not UNSET:
            field_dict["data"] = data
        if output_format is not UNSET:
            field_dict["output_format"] = output_format
        if quality is not UNSET:
            field_dict["quality"] = quality
        if size is not UNSET:
            field_dict["size"] = size
        if usage is not UNSET:
            field_dict["usage"] = usage

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.image import Image
        from ..models.usage import Usage

        d = dict(src_dict)
        created = d.pop("created")

        def _parse_background(data: object) -> ImagesResponseBackgroundType0 | None | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, str):
                    raise TypeError()
                background_type_0 = ImagesResponseBackgroundType0(data)

                return background_type_0
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            return cast(ImagesResponseBackgroundType0 | None | Unset, data)

        background = _parse_background(d.pop("background", UNSET))

        def _parse_data(data: object) -> list[Image] | None | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, list):
                    raise TypeError()
                data_type_0 = []
                _data_type_0 = data
                for data_type_0_item_data in _data_type_0:
                    data_type_0_item = Image.from_dict(data_type_0_item_data)

                    data_type_0.append(data_type_0_item)

                return data_type_0
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            return cast(list[Image] | None | Unset, data)

        data = _parse_data(d.pop("data", UNSET))

        def _parse_output_format(data: object) -> ImagesResponseOutputFormatType0 | None | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, str):
                    raise TypeError()
                output_format_type_0 = ImagesResponseOutputFormatType0(data)

                return output_format_type_0
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            return cast(ImagesResponseOutputFormatType0 | None | Unset, data)

        output_format = _parse_output_format(d.pop("output_format", UNSET))

        def _parse_quality(data: object) -> ImagesResponseQualityType0 | None | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, str):
                    raise TypeError()
                quality_type_0 = ImagesResponseQualityType0(data)

                return quality_type_0
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            return cast(ImagesResponseQualityType0 | None | Unset, data)

        quality = _parse_quality(d.pop("quality", UNSET))

        def _parse_size(data: object) -> ImagesResponseSizeType0 | None | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, str):
                    raise TypeError()
                size_type_0 = ImagesResponseSizeType0(data)

                return size_type_0
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            return cast(ImagesResponseSizeType0 | None | Unset, data)

        size = _parse_size(d.pop("size", UNSET))

        def _parse_usage(data: object) -> None | Unset | Usage:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, dict):
                    raise TypeError()
                usage_type_0 = Usage.from_dict(data)

                return usage_type_0
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            return cast(None | Unset | Usage, data)

        usage = _parse_usage(d.pop("usage", UNSET))

        images_response = cls(
            created=created,
            background=background,
            data=data,
            output_format=output_format,
            quality=quality,
            size=size,
            usage=usage,
        )

        images_response.additional_properties = d
        return images_response

    @property
    def additional_keys(self) -> list[str]:
        return list(self.additional_properties.keys())

    def __getitem__(self, key: str) -> Any:
        return self.additional_properties[key]

    def __setitem__(self, key: str, value: Any) -> None:
        self.additional_properties[key] = value

    def __delitem__(self, key: str) -> None:
        del self.additional_properties[key]

    def __contains__(self, key: str) -> bool:
        return key in self.additional_properties
