//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
//
// Changes to this file may cause incorrect behavior and will be lost when
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
#nullable enable
namespace LukeHagar.PlexAPI.SDK.Models.Requests
{
    using LukeHagar.PlexAPI.SDK.Models.Requests;
    using LukeHagar.PlexAPI.SDK.Utils;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using System;
    using System.Collections.Generic;
    using System.Numerics;
    using System.Reflection;
    

    public class OptimizedForStreamingType
    {
        private OptimizedForStreamingType(string value) { Value = value; }

        public string Value { get; private set; }
        public static OptimizedForStreamingType One { get { return new OptimizedForStreamingType("1"); } }
        
        public static OptimizedForStreamingType Boolean { get { return new OptimizedForStreamingType("boolean"); } }
        
        public static OptimizedForStreamingType Null { get { return new OptimizedForStreamingType("null"); } }

        public override string ToString() { return Value; }
        public static implicit operator String(OptimizedForStreamingType v) { return v.Value; }
        public static OptimizedForStreamingType FromString(string v) {
            switch(v) {
                case "1": return One;
                case "boolean": return Boolean;
                case "null": return Null;
                default: throw new ArgumentException("Invalid value for OptimizedForStreamingType");
            }
        }
        public override bool Equals(object? obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return false;
            }
            return Value.Equals(((OptimizedForStreamingType)obj).Value);
        }

        public override int GetHashCode()
        {
            return Value.GetHashCode();
        }
    }


    /// <summary>
    /// Has this media been optimized for streaming. NOTE: This can be 0, 1, false or true
    /// </summary>
    [JsonConverter(typeof(OptimizedForStreaming.OptimizedForStreamingConverter))]
    public class OptimizedForStreaming {
        public OptimizedForStreaming(OptimizedForStreamingType type) {
            Type = type;
        }

        [SpeakeasyMetadata("form:explode=true")]
        public One? One { get; set; }

        [SpeakeasyMetadata("form:explode=true")]
        public bool? Boolean { get; set; }

        public OptimizedForStreamingType Type { get; set; }


        public static OptimizedForStreaming CreateOne(One one) {
            OptimizedForStreamingType typ = OptimizedForStreamingType.One;

            OptimizedForStreaming res = new OptimizedForStreaming(typ);
            res.One = one;
            return res;
        }

        public static OptimizedForStreaming CreateBoolean(bool boolean) {
            OptimizedForStreamingType typ = OptimizedForStreamingType.Boolean;

            OptimizedForStreaming res = new OptimizedForStreaming(typ);
            res.Boolean = boolean;
            return res;
        }

        public static OptimizedForStreaming CreateNull() {
            OptimizedForStreamingType typ = OptimizedForStreamingType.Null;
            return new OptimizedForStreaming(typ);
        }

        public class OptimizedForStreamingConverter : JsonConverter
        {

            public override bool CanConvert(System.Type objectType) => objectType == typeof(OptimizedForStreaming);

            public override bool CanRead => true;

            public override object? ReadJson(JsonReader reader, System.Type objectType, object? existingValue, JsonSerializer serializer)
            {
                var json = JRaw.Create(reader).ToString();
                if (json == "null")
                {
                    return null;
                }

                var fallbackCandidates = new List<(System.Type, object, string)>();

                try
                {
                    return new OptimizedForStreaming(OptimizedForStreamingType.One)
                    {
                        One = ResponseBodyDeserializer.DeserializeUndiscriminatedUnionMember<One>(json)
                    };
                }
                catch (ResponseBodyDeserializer.MissingMemberException)
                {
                    fallbackCandidates.Add((typeof(One), new OptimizedForStreaming(OptimizedForStreamingType.One), "One"));
                }
                catch (ResponseBodyDeserializer.DeserializationException)
                {
                    // try next option
                }
                catch (Exception)
                {
                    throw;
                }

                try
                {
                    var converted = Convert.ToBoolean(json);
                    return new OptimizedForStreaming(OptimizedForStreamingType.Boolean)
                    {
                        Boolean = converted
                    };
                }
                catch (System.FormatException)
                {
                    // try next option
                }

                if (fallbackCandidates.Count > 0)
                {
                    fallbackCandidates.Sort((a, b) => ResponseBodyDeserializer.CompareFallbackCandidates(a.Item1, b.Item1, json));
                    foreach(var (deserializationType, returnObject, propertyName) in fallbackCandidates)
                    {
                        try
                        {
                            return ResponseBodyDeserializer.DeserializeUndiscriminatedUnionFallback(deserializationType, returnObject, propertyName, json);
                        }
                        catch (ResponseBodyDeserializer.DeserializationException)
                        {
                            // try next fallback option
                        }
                        catch (Exception)
                        {
                            throw;
                        }
                    }
                }

                throw new InvalidOperationException("Could not deserialize into any supported types.");
            }

            public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
            {
                if (value == null) {
                    writer.WriteRawValue("null");
                    return;
                }
                OptimizedForStreaming res = (OptimizedForStreaming)value;
                if (OptimizedForStreamingType.FromString(res.Type).Equals(OptimizedForStreamingType.Null))
                {
                    writer.WriteRawValue("null");
                    return;
                }
                if (res.One != null)
                {
                    writer.WriteRawValue(Utilities.SerializeJSON(res.One));
                    return;
                }
                if (res.Boolean != null)
                {
                    writer.WriteRawValue(Utilities.SerializeJSON(res.Boolean));
                    return;
                }

            }

        }

    }
}