/*
 * Warframe.market API
 *
 * ## Documentation is under very slow and heavy development  This document will gradually be supplemented and updated over some extended period of time.  ## Unstable endpoints Endpoint that is not stable and probably will be changed in the future will be marked with exclamation sign -> âš  
 *
 * The version of the OpenAPI document: 1.14.0
 * Generated by: https://github.com/openapitools/openapi-generator.git
 */


using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using System.ComponentModel.DataAnnotations;
using OpenAPIDateConverter = WarframeMarket.Client.OpenAPIDateConverter;
using System.Reflection;

namespace WarframeMarket.Model
{
    /// <summary>
    /// ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner
    /// </summary>
    [JsonConverter(typeof(ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInnerJsonConverter))]
    [DataContract(Name = "_items__url_name__dropsources_get_200_response_payload_dropsources_inner")]
    public partial class ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner : AbstractOpenAPISchema, IEquatable<ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner>, IValidatableObject
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner" /> class
        /// with the <see cref="DropsourceMission" /> class
        /// </summary>
        /// <param name="actualInstance">An instance of DropsourceMission.</param>
        public ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner(DropsourceMission actualInstance)
        {
            this.IsNullable = false;
            this.SchemaType= "anyOf";
            this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null.");
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner" /> class
        /// with the <see cref="DropsourceNpc" /> class
        /// </summary>
        /// <param name="actualInstance">An instance of DropsourceNpc.</param>
        public ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner(DropsourceNpc actualInstance)
        {
            this.IsNullable = false;
            this.SchemaType= "anyOf";
            this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null.");
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner" /> class
        /// with the <see cref="DropsourceRelic" /> class
        /// </summary>
        /// <param name="actualInstance">An instance of DropsourceRelic.</param>
        public ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner(DropsourceRelic actualInstance)
        {
            this.IsNullable = false;
            this.SchemaType= "anyOf";
            this.ActualInstance = actualInstance ?? throw new ArgumentException("Invalid instance found. Must not be null.");
        }


        private Object _actualInstance;

        /// <summary>
        /// Gets or Sets ActualInstance
        /// </summary>
        public override Object ActualInstance
        {
            get
            {
                return _actualInstance;
            }
            set
            {
                if (value.GetType() == typeof(DropsourceMission))
                {
                    this._actualInstance = value;
                }
                else if (value.GetType() == typeof(DropsourceNpc))
                {
                    this._actualInstance = value;
                }
                else if (value.GetType() == typeof(DropsourceRelic))
                {
                    this._actualInstance = value;
                }
                else
                {
                    throw new ArgumentException("Invalid instance found. Must be the following types: DropsourceMission, DropsourceNpc, DropsourceRelic");
                }
            }
        }

        /// <summary>
        /// Get the actual instance of `DropsourceMission`. If the actual instance is not `DropsourceMission`,
        /// the InvalidClassException will be thrown
        /// </summary>
        /// <returns>An instance of DropsourceMission</returns>
        public DropsourceMission GetDropsourceMission()
        {
            return (DropsourceMission)this.ActualInstance;
        }

        /// <summary>
        /// Get the actual instance of `DropsourceNpc`. If the actual instance is not `DropsourceNpc`,
        /// the InvalidClassException will be thrown
        /// </summary>
        /// <returns>An instance of DropsourceNpc</returns>
        public DropsourceNpc GetDropsourceNpc()
        {
            return (DropsourceNpc)this.ActualInstance;
        }

        /// <summary>
        /// Get the actual instance of `DropsourceRelic`. If the actual instance is not `DropsourceRelic`,
        /// the InvalidClassException will be thrown
        /// </summary>
        /// <returns>An instance of DropsourceRelic</returns>
        public DropsourceRelic GetDropsourceRelic()
        {
            return (DropsourceRelic)this.ActualInstance;
        }

        /// <summary>
        /// Returns the string presentation of the object
        /// </summary>
        /// <returns>String presentation of the object</returns>
        public override string ToString()
        {
            var sb = new StringBuilder();
            sb.Append("class ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner {\n");
            sb.Append("  ActualInstance: ").Append(this.ActualInstance).Append("\n");
            sb.Append("}\n");
            return sb.ToString();
        }

        /// <summary>
        /// Returns the JSON string presentation of the object
        /// </summary>
        /// <returns>JSON string presentation of the object</returns>
        public override string ToJson()
        {
            return JsonConvert.SerializeObject(this.ActualInstance, ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner.SerializerSettings);
        }

        /// <summary>
        /// Converts the JSON string into an instance of ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner
        /// </summary>
        /// <param name="jsonString">JSON string</param>
        /// <returns>An instance of ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner</returns>
        public static ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner FromJson(string jsonString)
        {
            ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner newItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner = null;

            if (string.IsNullOrEmpty(jsonString))
            {
                return newItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner;
            }

            try
            {
                newItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner = new ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner(JsonConvert.DeserializeObject<DropsourceMission>(jsonString, ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner.SerializerSettings));
                // deserialization is considered successful at this point if no exception has been thrown.
                return newItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner;
            }
            catch (Exception exception)
            {
                // deserialization failed, try the next one
                System.Diagnostics.Debug.WriteLine(string.Format("Failed to deserialize `{0}` into DropsourceMission: {1}", jsonString, exception.ToString()));
            }

            try
            {
                newItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner = new ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner(JsonConvert.DeserializeObject<DropsourceNpc>(jsonString, ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner.SerializerSettings));
                // deserialization is considered successful at this point if no exception has been thrown.
                return newItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner;
            }
            catch (Exception exception)
            {
                // deserialization failed, try the next one
                System.Diagnostics.Debug.WriteLine(string.Format("Failed to deserialize `{0}` into DropsourceNpc: {1}", jsonString, exception.ToString()));
            }

            try
            {
                newItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner = new ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner(JsonConvert.DeserializeObject<DropsourceRelic>(jsonString, ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner.SerializerSettings));
                // deserialization is considered successful at this point if no exception has been thrown.
                return newItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner;
            }
            catch (Exception exception)
            {
                // deserialization failed, try the next one
                System.Diagnostics.Debug.WriteLine(string.Format("Failed to deserialize `{0}` into DropsourceRelic: {1}", jsonString, exception.ToString()));
            }

            // no match found, throw an exception
            throw new InvalidDataException("The JSON string `" + jsonString + "` cannot be deserialized into any schema defined.");
        }

        /// <summary>
        /// Returns true if objects are equal
        /// </summary>
        /// <param name="input">Object to be compared</param>
        /// <returns>Boolean</returns>
        public override bool Equals(object input)
        {
            return this.Equals(input as ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner);
        }

        /// <summary>
        /// Returns true if ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner instances are equal
        /// </summary>
        /// <param name="input">Instance of ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner to be compared</param>
        /// <returns>Boolean</returns>
        public bool Equals(ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner input)
        {
            if (input == null)
                return false;

            return this.ActualInstance.Equals(input.ActualInstance);
        }

        /// <summary>
        /// Gets the hash code
        /// </summary>
        /// <returns>Hash code</returns>
        public override int GetHashCode()
        {
            unchecked // Overflow is fine, just wrap
            {
                int hashCode = 41;
                if (this.ActualInstance != null)
                    hashCode = hashCode * 59 + this.ActualInstance.GetHashCode();
                return hashCode;
            }
        }

        /// <summary>
        /// To validate all properties of the instance
        /// </summary>
        /// <param name="validationContext">Validation context</param>
        /// <returns>Validation Result</returns>
        IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
        {
            yield break;
        }
    }

    /// <summary>
    /// Custom JSON converter for ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner
    /// </summary>
    public class ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInnerJsonConverter : JsonConverter
    {
        /// <summary>
        /// To write the JSON string
        /// </summary>
        /// <param name="writer">JSON writer</param>
        /// <param name="value">Object to be converted into a JSON string</param>
        /// <param name="serializer">JSON Serializer</param>
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            writer.WriteRawValue((string)(typeof(ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner).GetMethod("ToJson").Invoke(value, null)));
        }

        /// <summary>
        /// To convert a JSON string into an object
        /// </summary>
        /// <param name="reader">JSON reader</param>
        /// <param name="objectType">Object type</param>
        /// <param name="existingValue">Existing value</param>
        /// <param name="serializer">JSON Serializer</param>
        /// <returns>The object converted from the JSON string</returns>
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if(reader.TokenType != JsonToken.Null)
            {
                return ItemsUrlNameDropsourcesGet200ResponsePayloadDropsourcesInner.FromJson(JObject.Load(reader).ToString(Formatting.None));
            }
            return null;
        }

        /// <summary>
        /// Check if the object can be converted
        /// </summary>
        /// <param name="objectType">Object type</param>
        /// <returns>True if the object can be converted</returns>
        public override bool CanConvert(Type objectType)
        {
            return false;
        }
    }

}
