# coding: utf-8

"""
    asset-lake-api-server

    No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)

    The version of the OpenAPI document: 0.1
    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 inspect import getfullargspec
from typing import TYPE_CHECKING, Any, List, Optional, Union

from pydantic.v1 import BaseModel, Field, StrictStr, ValidationError, validator

from clients.asset_lake.models.checkpoint_data import CheckpointData
from clients.asset_lake.models.file_data import FileData
from clients.asset_lake.models.lora_data import LoraData
from clients.asset_lake.models.textual_inversion_data import TextualInversionData
from clients.asset_lake.models.vae_data import VAEData
from clients.asset_lake.models.volume_data import VolumeData

DATA_ONE_OF_SCHEMAS = [
    "CheckpointData",
    "FileData",
    "LoraData",
    "TextualInversionData",
    "VAEData",
    "VolumeData",
]


class Data(BaseModel):
    """
    Data
    """

    # data type: FileData
    oneof_schema_1_validator: Optional[FileData] = None
    # data type: CheckpointData
    oneof_schema_2_validator: Optional[CheckpointData] = None
    # data type: LoraData
    oneof_schema_3_validator: Optional[LoraData] = None
    # data type: TextualInversionData
    oneof_schema_4_validator: Optional[TextualInversionData] = None
    # data type: VAEData
    oneof_schema_5_validator: Optional[VAEData] = None
    # data type: VolumeData
    oneof_schema_6_validator: Optional[VolumeData] = None
    if TYPE_CHECKING:
        actual_instance: Union[
            CheckpointData,
            FileData,
            LoraData,
            TextualInversionData,
            VAEData,
            VolumeData,
        ]
    else:
        actual_instance: Any
    one_of_schemas: List[str] = Field(DATA_ONE_OF_SCHEMAS, const=True)

    class Config:
        validate_assignment = True

    discriminator_value_class_map = {}

    def __init__(self, *args, **kwargs) -> None:
        if args:
            if len(args) > 1:
                raise ValueError(
                    "If a position argument is used, only 1 is allowed to set `actual_instance`"
                )
            if kwargs:
                raise ValueError(
                    "If a position argument is used, keyword arguments cannot be used."
                )
            super().__init__(actual_instance=args[0])
        else:
            super().__init__(**kwargs)

    @validator("actual_instance")
    def actual_instance_must_validate_oneof(cls, v):
        instance = Data.construct()
        error_messages = []
        match = 0
        # validate data type: FileData
        if not isinstance(v, FileData):
            error_messages.append(f"Error! Input type `{type(v)}` is not `FileData`")
        else:
            match += 1
        # validate data type: CheckpointData
        if not isinstance(v, CheckpointData):
            error_messages.append(
                f"Error! Input type `{type(v)}` is not `CheckpointData`"
            )
        else:
            match += 1
        # validate data type: LoraData
        if not isinstance(v, LoraData):
            error_messages.append(f"Error! Input type `{type(v)}` is not `LoraData`")
        else:
            match += 1
        # validate data type: TextualInversionData
        if not isinstance(v, TextualInversionData):
            error_messages.append(
                f"Error! Input type `{type(v)}` is not `TextualInversionData`"
            )
        else:
            match += 1
        # validate data type: VAEData
        if not isinstance(v, VAEData):
            error_messages.append(f"Error! Input type `{type(v)}` is not `VAEData`")
        else:
            match += 1
        # validate data type: VolumeData
        if not isinstance(v, VolumeData):
            error_messages.append(f"Error! Input type `{type(v)}` is not `VolumeData`")
        else:
            match += 1
        if match > 1:
            # more than 1 match
            raise ValueError(
                "Multiple matches found when setting `actual_instance` in Data with oneOf schemas: CheckpointData, FileData, LoraData, TextualInversionData, VAEData, VolumeData. Details: "
                + ", ".join(error_messages)
            )
        elif match == 0:
            # no match
            raise ValueError(
                "No match found when setting `actual_instance` in Data with oneOf schemas: CheckpointData, FileData, LoraData, TextualInversionData, VAEData, VolumeData. Details: "
                + ", ".join(error_messages)
            )
        else:
            return v

    @classmethod
    def from_dict(cls, obj: dict) -> Data:
        return cls.from_json(json.dumps(obj))

    @classmethod
    def from_json(cls, json_str: str) -> Data:
        """Returns the object represented by the json string"""
        instance = Data.construct()
        error_messages = []
        match = 0

        # use oneOf discriminator to lookup the data type
        _data_type = json.loads(json_str).get("asset_type")
        if not _data_type:
            raise ValueError(
                "Failed to lookup data type from the field `asset_type` in the input."
            )

        # check if data type is `CheckpointData`
        if _data_type == "CheckpointData":
            instance.actual_instance = CheckpointData.from_json(json_str)
            return instance

        # check if data type is `FileData`
        if _data_type == "FileData":
            instance.actual_instance = FileData.from_json(json_str)
            return instance

        # check if data type is `LoraData`
        if _data_type == "LoraData":
            instance.actual_instance = LoraData.from_json(json_str)
            return instance

        # check if data type is `TextualInversionData`
        if _data_type == "TextualInversionData":
            instance.actual_instance = TextualInversionData.from_json(json_str)
            return instance

        # check if data type is `VAEData`
        if _data_type == "VAEData":
            instance.actual_instance = VAEData.from_json(json_str)
            return instance

        # check if data type is `VolumeData`
        if _data_type == "VolumeData":
            instance.actual_instance = VolumeData.from_json(json_str)
            return instance

        # check if data type is `CheckpointData`
        if _data_type == "checkpoint":
            instance.actual_instance = CheckpointData.from_json(json_str)
            return instance

        # check if data type is `VAEData`
        if _data_type == "encoder":
            instance.actual_instance = VAEData.from_json(json_str)
            return instance

        # check if data type is `FileData`
        if _data_type == "file":
            instance.actual_instance = FileData.from_json(json_str)
            return instance

        # check if data type is `LoraData`
        if _data_type == "lora":
            instance.actual_instance = LoraData.from_json(json_str)
            return instance

        # check if data type is `TextualInversionData`
        if _data_type == "textual_inversion":
            instance.actual_instance = TextualInversionData.from_json(json_str)
            return instance

        # check if data type is `VolumeData`
        if _data_type == "volume":
            instance.actual_instance = VolumeData.from_json(json_str)
            return instance

        # deserialize data into FileData
        try:
            instance.actual_instance = FileData.from_json(json_str)
            match += 1
        except (ValidationError, ValueError) as e:
            error_messages.append(str(e))
        # deserialize data into CheckpointData
        try:
            instance.actual_instance = CheckpointData.from_json(json_str)
            match += 1
        except (ValidationError, ValueError) as e:
            error_messages.append(str(e))
        # deserialize data into LoraData
        try:
            instance.actual_instance = LoraData.from_json(json_str)
            match += 1
        except (ValidationError, ValueError) as e:
            error_messages.append(str(e))
        # deserialize data into TextualInversionData
        try:
            instance.actual_instance = TextualInversionData.from_json(json_str)
            match += 1
        except (ValidationError, ValueError) as e:
            error_messages.append(str(e))
        # deserialize data into VAEData
        try:
            instance.actual_instance = VAEData.from_json(json_str)
            match += 1
        except (ValidationError, ValueError) as e:
            error_messages.append(str(e))
        # deserialize data into VolumeData
        try:
            instance.actual_instance = VolumeData.from_json(json_str)
            match += 1
        except (ValidationError, ValueError) as e:
            error_messages.append(str(e))

        if match > 1:
            # more than 1 match
            raise ValueError(
                "Multiple matches found when deserializing the JSON string into Data with oneOf schemas: CheckpointData, FileData, LoraData, TextualInversionData, VAEData, VolumeData. Details: "
                + ", ".join(error_messages)
            )
        elif match == 0:
            # no match
            raise ValueError(
                "No match found when deserializing the JSON string into Data with oneOf schemas: CheckpointData, FileData, LoraData, TextualInversionData, VAEData, VolumeData. Details: "
                + ", ".join(error_messages)
            )
        else:
            return instance

    def to_json(self) -> str:
        """Returns the JSON representation of the actual instance"""
        if self.actual_instance is None:
            return "null"

        to_json = getattr(self.actual_instance, "to_json", None)
        if callable(to_json):
            return self.actual_instance.to_json()
        else:
            return json.dumps(self.actual_instance)

    def to_dict(self) -> dict:
        """Returns the dict representation of the actual instance"""
        if self.actual_instance is None:
            return None

        to_dict = getattr(self.actual_instance, "to_dict", None)
        if callable(to_dict):
            return self.actual_instance.to_dict()
        else:
            # primitive type
            return self.actual_instance

    def to_str(self) -> str:
        """Returns the string representation of the actual instance"""
        return pprint.pformat(self.dict())
