"""
This file is generated by the openapi-python-client tool via the generate_api_client.py script

It is a customized template from the openapi-python-client tool's default template:
https://github.com/openapi-generators/openapi-python-client/blob/861ef5622f10fc96d240dc9becb0edf94e61446c/openapi_python_client/templates/model.py.jinja

The main change is:
- Fix typing issues
"""

# flake8: noqa: C901

from enum import Enum
from typing import Any, List, Literal, Type, TypeVar, Union, cast

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

from ..models.telemetry_field_type_0_item_type_2 import TelemetryFieldType0ItemType2
from ..models.telemetry_sort_options_direction import TelemetrySortOptionsDirection

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


@_attrs_define
class TelemetrySortOptions:
    """
    Attributes:
        field (Union[List[Union[Literal['count'], Literal['lookup']]], List[Union[Literal['feedback_metrics'],
            TelemetryFieldType0ItemType2, str]]]):
        direction (TelemetrySortOptionsDirection):
    """

    field: Union[
        List[Union[Literal["count"], Literal["lookup"]]],
        List[Union[Literal["feedback_metrics"], TelemetryFieldType0ItemType2, str]],
    ]
    direction: TelemetrySortOptionsDirection
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        field: Union[List[Union[Literal["count"], Literal["lookup"]]], List[Union[Literal["feedback_metrics"], str]]]
        if isinstance(self.field, list):
            field = []
            for componentsschemas_telemetry_field_type_0_item_data in self.field:
                componentsschemas_telemetry_field_type_0_item: Union[Literal["feedback_metrics"], str]
                if isinstance(componentsschemas_telemetry_field_type_0_item_data, TelemetryFieldType0ItemType2):
                    componentsschemas_telemetry_field_type_0_item = (
                        componentsschemas_telemetry_field_type_0_item_data.value
                        if isinstance(componentsschemas_telemetry_field_type_0_item_data, Enum)
                        else componentsschemas_telemetry_field_type_0_item_data
                    )
                else:
                    componentsschemas_telemetry_field_type_0_item = componentsschemas_telemetry_field_type_0_item_data
                field.append(componentsschemas_telemetry_field_type_0_item)

        direction = self.direction.value if isinstance(self.direction, Enum) else self.direction

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update(
            {
                "field": field,
                "direction": direction,
            }
        )

        return field_dict

    @classmethod
    def from_dict(cls: Type[T], src_dict: dict[str, Any]) -> T:
        d = src_dict.copy()

        def _parse_field(
            data: object,
        ) -> Union[
            List[Union[Literal["count"], Literal["lookup"]]],
            List[Union[Literal["feedback_metrics"], TelemetryFieldType0ItemType2, str]],
        ]:
            try:
                if not isinstance(data, list):
                    raise TypeError()
                componentsschemas_telemetry_field_type_0 = []
                _componentsschemas_telemetry_field_type_0 = data
                for componentsschemas_telemetry_field_type_0_item_data in _componentsschemas_telemetry_field_type_0:

                    def _parse_componentsschemas_telemetry_field_type_0_item(
                        data: object,
                    ) -> Union[Literal["feedback_metrics"], TelemetryFieldType0ItemType2, str]:
                        componentsschemas_telemetry_field_type_0_item_type_0 = cast(Literal["feedback_metrics"], data)
                        if componentsschemas_telemetry_field_type_0_item_type_0 != "feedback_metrics":
                            raise ValueError(
                                f"/components/schemas/TelemetryField_type_0_item_type_0 must match const 'feedback_metrics', got '{componentsschemas_telemetry_field_type_0_item_type_0}'"
                            )
                        return componentsschemas_telemetry_field_type_0_item_type_0
                        try:
                            if not isinstance(data, str):
                                raise TypeError()
                            componentsschemas_telemetry_field_type_0_item_type_2 = TelemetryFieldType0ItemType2(data)

                            return componentsschemas_telemetry_field_type_0_item_type_2
                        except:  # noqa: E722
                            pass
                        return cast(Union[Literal["feedback_metrics"], TelemetryFieldType0ItemType2, str], data)

                    componentsschemas_telemetry_field_type_0_item = (
                        _parse_componentsschemas_telemetry_field_type_0_item(
                            componentsschemas_telemetry_field_type_0_item_data
                        )
                    )

                    componentsschemas_telemetry_field_type_0.append(componentsschemas_telemetry_field_type_0_item)

                return componentsschemas_telemetry_field_type_0
            except:  # noqa: E722
                pass
            if not isinstance(data, list):
                raise TypeError()
            componentsschemas_telemetry_field_type_1 = []
            _componentsschemas_telemetry_field_type_1 = data
            for componentsschemas_telemetry_field_type_1_item_data in _componentsschemas_telemetry_field_type_1:

                def _parse_componentsschemas_telemetry_field_type_1_item(
                    data: object,
                ) -> Union[Literal["count"], Literal["lookup"]]:
                    componentsschemas_telemetry_field_type_1_item_type_0 = cast(Literal["lookup"], data)
                    if componentsschemas_telemetry_field_type_1_item_type_0 != "lookup":
                        raise ValueError(
                            f"/components/schemas/TelemetryField_type_1_item_type_0 must match const 'lookup', got '{componentsschemas_telemetry_field_type_1_item_type_0}'"
                        )
                    return componentsschemas_telemetry_field_type_1_item_type_0
                    componentsschemas_telemetry_field_type_1_item_type_1 = cast(Literal["count"], data)
                    if componentsschemas_telemetry_field_type_1_item_type_1 != "count":
                        raise ValueError(
                            f"/components/schemas/TelemetryField_type_1_item_type_1 must match const 'count', got '{componentsschemas_telemetry_field_type_1_item_type_1}'"
                        )
                    return componentsschemas_telemetry_field_type_1_item_type_1

                componentsschemas_telemetry_field_type_1_item = _parse_componentsschemas_telemetry_field_type_1_item(
                    componentsschemas_telemetry_field_type_1_item_data
                )

                componentsschemas_telemetry_field_type_1.append(componentsschemas_telemetry_field_type_1_item)

            return componentsschemas_telemetry_field_type_1

        field = _parse_field(d.pop("field"))

        direction = TelemetrySortOptionsDirection(d.pop("direction"))

        telemetry_sort_options = cls(
            field=field,
            direction=direction,
        )

        telemetry_sort_options.additional_properties = d
        return telemetry_sort_options

    @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
