from __future__ import annotations

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

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

from ..models.region import Region
from ..models.search_text_params_safesearch import SearchTextParamsSafesearch
from ..models.timelimit import Timelimit
from ..types import UNSET, Unset

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


@_attrs_define
class SearchTextParams:
    """
    Attributes:
        keywords (str | Unset):  Default: ''.
        region (Region | Unset):
        safesearch (SearchTextParamsSafesearch | Unset):  Default: SearchTextParamsSafesearch.MODERATE.
        timelimit (None | Timelimit | Unset):
        max_results (int | Unset):  Default: 3.
    """

    keywords: str | Unset = ""
    region: Region | Unset = UNSET
    safesearch: SearchTextParamsSafesearch | Unset = SearchTextParamsSafesearch.MODERATE
    timelimit: None | Timelimit | Unset = UNSET
    max_results: int | Unset = 3
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        keywords = self.keywords

        region: str | Unset = UNSET
        if not isinstance(self.region, Unset):
            region = self.region.value

        safesearch: str | Unset = UNSET
        if not isinstance(self.safesearch, Unset):
            safesearch = self.safesearch.value

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

        max_results = self.max_results

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if keywords is not UNSET:
            field_dict["keywords"] = keywords
        if region is not UNSET:
            field_dict["region"] = region
        if safesearch is not UNSET:
            field_dict["safesearch"] = safesearch
        if timelimit is not UNSET:
            field_dict["timelimit"] = timelimit
        if max_results is not UNSET:
            field_dict["max_results"] = max_results

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        d = dict(src_dict)
        keywords = d.pop("keywords", UNSET)

        _region = d.pop("region", UNSET)
        region: Region | Unset
        if isinstance(_region, Unset):
            region = UNSET
        else:
            region = Region(_region)

        _safesearch = d.pop("safesearch", UNSET)
        safesearch: SearchTextParamsSafesearch | Unset
        if isinstance(_safesearch, Unset):
            safesearch = UNSET
        else:
            safesearch = SearchTextParamsSafesearch(_safesearch)

        def _parse_timelimit(data: object) -> None | Timelimit | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, str):
                    raise TypeError()
                timelimit_type_0 = Timelimit(data)

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

        timelimit = _parse_timelimit(d.pop("timelimit", UNSET))

        max_results = d.pop("max_results", UNSET)

        search_text_params = cls(
            keywords=keywords,
            region=region,
            safesearch=safesearch,
            timelimit=timelimit,
            max_results=max_results,
        )

        search_text_params.additional_properties = d
        return search_text_params

    @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
