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 ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.cron_schedule import CronSchedule
    from ..models.data_block import DataBlock
    from ..models.interval_trigger_model import IntervalTriggerModel


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


@_attrs_define
class ScheduleUpdate:
    """
    Attributes:
        name (None | str | Unset):
        trigger (CronSchedule | IntervalTriggerModel | None | Unset):
        runnable_id (None | str | Unset):
        input_ (DataBlock | None | Unset):
        run_id (None | str | Unset):
    """

    name: None | str | Unset = UNSET
    trigger: CronSchedule | IntervalTriggerModel | None | Unset = UNSET
    runnable_id: None | str | Unset = UNSET
    input_: DataBlock | None | Unset = UNSET
    run_id: None | str | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        from ..models.cron_schedule import CronSchedule
        from ..models.data_block import DataBlock
        from ..models.interval_trigger_model import IntervalTriggerModel

        name: None | str | Unset
        if isinstance(self.name, Unset):
            name = UNSET
        else:
            name = self.name

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

        runnable_id: None | str | Unset
        if isinstance(self.runnable_id, Unset):
            runnable_id = UNSET
        else:
            runnable_id = self.runnable_id

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

        run_id: None | str | Unset
        if isinstance(self.run_id, Unset):
            run_id = UNSET
        else:
            run_id = self.run_id

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if name is not UNSET:
            field_dict["name"] = name
        if trigger is not UNSET:
            field_dict["trigger"] = trigger
        if runnable_id is not UNSET:
            field_dict["runnable_id"] = runnable_id
        if input_ is not UNSET:
            field_dict["input"] = input_
        if run_id is not UNSET:
            field_dict["run_id"] = run_id

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.cron_schedule import CronSchedule
        from ..models.data_block import DataBlock
        from ..models.interval_trigger_model import IntervalTriggerModel

        d = dict(src_dict)

        def _parse_name(data: object) -> None | str | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            return cast(None | str | Unset, data)

        name = _parse_name(d.pop("name", UNSET))

        def _parse_trigger(data: object) -> CronSchedule | IntervalTriggerModel | None | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, dict):
                    raise TypeError()
                trigger_type_0 = CronSchedule.from_dict(data)

                return trigger_type_0
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            try:
                if not isinstance(data, dict):
                    raise TypeError()
                trigger_type_1 = IntervalTriggerModel.from_dict(data)

                return trigger_type_1
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            return cast(CronSchedule | IntervalTriggerModel | None | Unset, data)

        trigger = _parse_trigger(d.pop("trigger", UNSET))

        def _parse_runnable_id(data: object) -> None | str | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            return cast(None | str | Unset, data)

        runnable_id = _parse_runnable_id(d.pop("runnable_id", UNSET))

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

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

        input_ = _parse_input_(d.pop("input", UNSET))

        def _parse_run_id(data: object) -> None | str | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            return cast(None | str | Unset, data)

        run_id = _parse_run_id(d.pop("run_id", UNSET))

        schedule_update = cls(
            name=name,
            trigger=trigger,
            runnable_id=runnable_id,
            input_=input_,
            run_id=run_id,
        )

        schedule_update.additional_properties = d
        return schedule_update

    @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
