from __future__ import annotations

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

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

from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.tracking_event import TrackingEvent


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


@_attrs_define
class Tracking:
    """Version 1

    Example:
        {'events': [{'status': 'Shipment information uploaded to Deutsche Post'}], 'publicUrl': '', 'barcode': '',
            'awb': ''}

    Attributes:
        awb (str): AWB number
        barcode (str): Barcode
        events (list[TrackingEvent]): All events
        public_url (str): Public URL
        from_city (str | Unset): Sender city
        from_country (str | Unset): Sender country
        from_name (str | Unset): Sender name
        from_zip (str | Unset): Sender zip code
        to_city (str | Unset): Recipient city
        to_country (str | Unset): Recipient country
        to_name (str | Unset): Recipient name
        to_zip (str | Unset): Recipient zip code
    """

    awb: str
    barcode: str
    events: list[TrackingEvent]
    public_url: str
    from_city: str | Unset = UNSET
    from_country: str | Unset = UNSET
    from_name: str | Unset = UNSET
    from_zip: str | Unset = UNSET
    to_city: str | Unset = UNSET
    to_country: str | Unset = UNSET
    to_name: str | Unset = UNSET
    to_zip: str | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

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

        barcode = self.barcode

        events = []
        for events_item_data in self.events:
            events_item = events_item_data.to_dict()
            events.append(events_item)

        public_url = self.public_url

        from_city = self.from_city

        from_country = self.from_country

        from_name = self.from_name

        from_zip = self.from_zip

        to_city = self.to_city

        to_country = self.to_country

        to_name = self.to_name

        to_zip = self.to_zip

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update(
            {
                "awb": awb,
                "barcode": barcode,
                "events": events,
                "publicUrl": public_url,
            }
        )
        if from_city is not UNSET:
            field_dict["fromCity"] = from_city
        if from_country is not UNSET:
            field_dict["fromCountry"] = from_country
        if from_name is not UNSET:
            field_dict["fromName"] = from_name
        if from_zip is not UNSET:
            field_dict["fromZip"] = from_zip
        if to_city is not UNSET:
            field_dict["toCity"] = to_city
        if to_country is not UNSET:
            field_dict["toCountry"] = to_country
        if to_name is not UNSET:
            field_dict["toName"] = to_name
        if to_zip is not UNSET:
            field_dict["toZip"] = to_zip

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.tracking_event import TrackingEvent

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

        barcode = d.pop("barcode")

        events = []
        _events = d.pop("events")
        for events_item_data in _events:
            events_item = TrackingEvent.from_dict(events_item_data)

            events.append(events_item)

        public_url = d.pop("publicUrl")

        from_city = d.pop("fromCity", UNSET)

        from_country = d.pop("fromCountry", UNSET)

        from_name = d.pop("fromName", UNSET)

        from_zip = d.pop("fromZip", UNSET)

        to_city = d.pop("toCity", UNSET)

        to_country = d.pop("toCountry", UNSET)

        to_name = d.pop("toName", UNSET)

        to_zip = d.pop("toZip", UNSET)

        tracking = cls(
            awb=awb,
            barcode=barcode,
            events=events,
            public_url=public_url,
            from_city=from_city,
            from_country=from_country,
            from_name=from_name,
            from_zip=from_zip,
            to_city=to_city,
            to_country=to_country,
            to_name=to_name,
            to_zip=to_zip,
        )

        tracking.additional_properties = d
        return tracking

    @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
