# coding: utf-8

"""
    World News API

    The world's news wrapped into a single API.

    The version of the OpenAPI document: 1.2.0
    Contact: mail@worldnewsapi.com
    Generated by OpenAPI Generator (https://openapi-generator.tech)

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

from __future__ import annotations
import pprint
import re  # noqa: F401
import json

from pydantic import BaseModel, ConfigDict, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from worldnewsapi.models.extract_news200_response_images_inner import ExtractNews200ResponseImagesInner
from worldnewsapi.models.extract_news200_response_videos_inner import ExtractNews200ResponseVideosInner
from typing import Optional, Set
from typing_extensions import Self


class ExtractNews200Response(BaseModel):
    """
    ExtractNews200Response
    """

  # noqa: E501
    title: Optional[StrictStr] = None
    text: Optional[StrictStr] = None
    url: Optional[StrictStr] = None
    image: Optional[StrictStr] = None
    images: Optional[List[ExtractNews200ResponseImagesInner]] = None
    video: Optional[StrictStr] = None
    videos: Optional[List[ExtractNews200ResponseVideosInner]] = None
    publish_date: Optional[StrictStr] = None
    author: Optional[StrictStr] = None
    authors: Optional[List[Optional[StrictStr]]] = None
    language: Optional[StrictStr] = None
    __properties: ClassVar[List[str]] = [
        "title", "text", "url", "image", "images", "video", "videos",
        "publish_date", "author", "authors", "language"
    ]

    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 ExtractNews200Response 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 images (list)
        _items = []
        if self.images:
            for _item in self.images:
                if _item:
                    _items.append(_item.to_dict())
            _dict['images'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in videos (list)
        _items = []
        if self.videos:
            for _item in self.videos:
                if _item:
                    _items.append(_item.to_dict())
            _dict['videos'] = _items
        # set to None if title (nullable) is None
        # and model_fields_set contains the field
        if self.title is None and "title" in self.model_fields_set:
            _dict['title'] = None

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

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

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

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

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

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

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

        return _dict

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

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

        _obj = cls.model_validate({
            "title":
            obj.get("title"),
            "text":
            obj.get("text"),
            "url":
            obj.get("url"),
            "image":
            obj.get("image"),
            "images": [
                ExtractNews200ResponseImagesInner.from_dict(_item)
                for _item in obj["images"]
            ] if obj.get("images") is not None else None,
            "video":
            obj.get("video"),
            "videos": [
                ExtractNews200ResponseVideosInner.from_dict(_item)
                for _item in obj["videos"]
            ] if obj.get("videos") is not None else None,
            "publish_date":
            obj.get("publish_date"),
            "author":
            obj.get("author"),
            "authors":
            obj.get("authors"),
            "language":
            obj.get("language")
        })
        return _obj
