# coding: utf-8

"""
Mozart platform API

API for interacting with the Mozart platform.

The version of the OpenAPI document: 0.2.0
Contact: support@bang-olufsen.dk
Generated by OpenAPI Generator (https://openapi-generator.tech)

Do not edit the class manually.
"""  # noqa: E501

from __future__ import annotations

import json
import pprint
import re  # noqa: F401
from typing import Any, ClassVar, Dict, List, Optional, Set
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field, StrictBool, StrictStr
from typing_extensions import Annotated, Self

from mozart_api.models.latency_profile import LatencyProfile
from mozart_api.models.speaker_group_member import SpeakerGroupMember


class SpeakerGroup(BaseModel):
    """
    SpeakerGroup
    """  # noqa: E501

    crossover_frequency: Annotated[
        Optional[Annotated[int, Field(le=300, strict=True, ge=20)]],
        Field(
            description="Crossover frequency for bass management in Hz",
            alias="crossoverFrequency",
        ),
    ] = None
    enable_phase_compensation: Annotated[
        Optional[StrictBool],
        Field(
            description="Enable phase compensation in bass management.",
            alias="enablePhaseCompensation",
        ),
    ] = None
    friendly_name: Annotated[
        Optional[StrictStr],
        Field(
            description="Friendly name of the SpeakerGroup. Required when creating a new group ",
            alias="friendlyName",
        ),
    ] = None
    id: Optional[UUID] = None
    is_deleteable: Annotated[Optional[StrictBool], Field(alias="isDeleteable")] = None
    items: Optional[List[SpeakerGroupMember]] = None
    latency_profile: Annotated[
        Optional[LatencyProfile], Field(alias="latencyProfile")
    ] = None
    room_compensation_id: Annotated[
        Optional[UUID],
        Field(
            description="ID of the calculated coefficients for this group, made during advanced room compensation. When updating an existing group, an empty string will clear roomCompensationId. ",
            alias="roomCompensationId",
        ),
    ] = None
    speaker_preset: Annotated[
        Optional[Annotated[int, Field(le=99, strict=True, ge=0)]],
        Field(
            description="Speaker preset / listening mode eg. BL28 curtain position etc.",
            alias="speakerPreset",
        ),
    ] = None
    __properties: ClassVar[List[str]] = [
        "crossoverFrequency",
        "enablePhaseCompensation",
        "friendlyName",
        "id",
        "isDeleteable",
        "items",
        "latencyProfile",
        "roomCompensationId",
        "speakerPreset",
    ]

    model_config = ConfigDict(
        populate_by_name=True,
        validate_assignment=True,
        protected_namespaces=(),
    )

    def to_str(self) -> str:
        """Returns the string representation of the model using alias"""
        return pprint.pformat(self.model_dump(by_alias=True))

    def to_json(self) -> str:
        """Returns the JSON representation of the model using alias"""
        # TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
        return json.dumps(self.to_dict())

    @classmethod
    def from_json(cls, json_str: str) -> Optional[Self]:
        """Create an instance of SpeakerGroup from a JSON string"""
        return cls.from_dict(json.loads(json_str))

    def to_dict(self) -> Dict[str, Any]:
        """Return the dictionary representation of the model using alias.

        This has the following differences from calling pydantic's
        `self.model_dump(by_alias=True)`:

        * `None` is only added to the output dict for nullable fields that
          were set at model initialization. Other fields with value `None`
          are ignored.
        """
        excluded_fields: Set[str] = set([])

        _dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        # override the default output from pydantic by calling `to_dict()` of each item in items (list)
        _items = []
        if self.items:
            for _item_items in self.items:
                if _item_items:
                    _items.append(_item_items.to_dict())
            _dict["items"] = _items
        # override the default output from pydantic by calling `to_dict()` of latency_profile
        if self.latency_profile:
            _dict["latencyProfile"] = self.latency_profile.to_dict()
        # set to None if crossover_frequency (nullable) is None
        # and model_fields_set contains the field
        if (
            self.crossover_frequency is None
            and "crossover_frequency" in self.model_fields_set
        ):
            _dict["crossoverFrequency"] = None

        # set to None if enable_phase_compensation (nullable) is None
        # and model_fields_set contains the field
        if (
            self.enable_phase_compensation is None
            and "enable_phase_compensation" in self.model_fields_set
        ):
            _dict["enablePhaseCompensation"] = None

        # set to None if friendly_name (nullable) is None
        # and model_fields_set contains the field
        if self.friendly_name is None and "friendly_name" in self.model_fields_set:
            _dict["friendlyName"] = None

        # set to None if is_deleteable (nullable) is None
        # and model_fields_set contains the field
        if self.is_deleteable is None and "is_deleteable" in self.model_fields_set:
            _dict["isDeleteable"] = None

        # set to None if items (nullable) is None
        # and model_fields_set contains the field
        if self.items is None and "items" in self.model_fields_set:
            _dict["items"] = None

        # set to None if room_compensation_id (nullable) is None
        # and model_fields_set contains the field
        if (
            self.room_compensation_id is None
            and "room_compensation_id" in self.model_fields_set
        ):
            _dict["roomCompensationId"] = None

        # set to None if speaker_preset (nullable) is None
        # and model_fields_set contains the field
        if self.speaker_preset is None and "speaker_preset" in self.model_fields_set:
            _dict["speakerPreset"] = None

        return _dict

    @classmethod
    def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
        """Create an instance of SpeakerGroup from a dict"""
        if obj is None:
            return None

        if not isinstance(obj, dict):
            return cls.model_validate(obj)

        _obj = cls.model_validate({
            "crossoverFrequency": obj.get("crossoverFrequency"),
            "enablePhaseCompensation": obj.get("enablePhaseCompensation"),
            "friendlyName": obj.get("friendlyName"),
            "id": obj.get("id"),
            "isDeleteable": obj.get("isDeleteable"),
            "items": [SpeakerGroupMember.from_dict(_item) for _item in obj["items"]]
            if obj.get("items") is not None
            else None,
            "latencyProfile": LatencyProfile.from_dict(obj["latencyProfile"])
            if obj.get("latencyProfile") is not None
            else None,
            "roomCompensationId": obj.get("roomCompensationId"),
            "speakerPreset": obj.get("speakerPreset"),
        })
        return _obj
