//------------------------------------------------------------------------------
// <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 GetAllMediaLibraryOptimizedForStreamingType
    {
        private GetAllMediaLibraryOptimizedForStreamingType(string value) { Value = value; }

        public string Value { get; private set; }
        public static GetAllMediaLibraryOptimizedForStreamingType OptimizedForStreaming1 { get { return new GetAllMediaLibraryOptimizedForStreamingType("optimizedForStreaming_1"); } }
        
        public static GetAllMediaLibraryOptimizedForStreamingType Boolean { get { return new GetAllMediaLibraryOptimizedForStreamingType("boolean"); } }
        
        public static GetAllMediaLibraryOptimizedForStreamingType Null { get { return new GetAllMediaLibraryOptimizedForStreamingType("null"); } }

        public override string ToString() { return Value; }
        public static implicit operator String(GetAllMediaLibraryOptimizedForStreamingType v) { return v.Value; }
        public static GetAllMediaLibraryOptimizedForStreamingType FromString(string v) {
            switch(v) {
                case "optimizedForStreaming_1": return OptimizedForStreaming1;
                case "boolean": return Boolean;
                case "null": return Null;
                default: throw new ArgumentException("Invalid value for GetAllMediaLibraryOptimizedForStreamingType");
            }
        }
        public override bool Equals(object? obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return false;
            }
            return Value.Equals(((GetAllMediaLibraryOptimizedForStreamingType)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(GetAllMediaLibraryOptimizedForStreaming.GetAllMediaLibraryOptimizedForStreamingConverter))]
    public class GetAllMediaLibraryOptimizedForStreaming {
        public GetAllMediaLibraryOptimizedForStreaming(GetAllMediaLibraryOptimizedForStreamingType type) {
            Type = type;
        }

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

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

        public GetAllMediaLibraryOptimizedForStreamingType Type { get; set; }


        public static GetAllMediaLibraryOptimizedForStreaming CreateOptimizedForStreaming1(OptimizedForStreaming1 optimizedForStreaming1) {
            GetAllMediaLibraryOptimizedForStreamingType typ = GetAllMediaLibraryOptimizedForStreamingType.OptimizedForStreaming1;

            GetAllMediaLibraryOptimizedForStreaming res = new GetAllMediaLibraryOptimizedForStreaming(typ);
            res.OptimizedForStreaming1 = optimizedForStreaming1;
            return res;
        }

        public static GetAllMediaLibraryOptimizedForStreaming CreateBoolean(bool boolean) {
            GetAllMediaLibraryOptimizedForStreamingType typ = GetAllMediaLibraryOptimizedForStreamingType.Boolean;

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

        public static GetAllMediaLibraryOptimizedForStreaming CreateNull() {
            GetAllMediaLibraryOptimizedForStreamingType typ = GetAllMediaLibraryOptimizedForStreamingType.Null;
            return new GetAllMediaLibraryOptimizedForStreaming(typ);
        }

        public class GetAllMediaLibraryOptimizedForStreamingConverter : JsonConverter
        {

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

            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 GetAllMediaLibraryOptimizedForStreaming(GetAllMediaLibraryOptimizedForStreamingType.OptimizedForStreaming1)
                    {
                        OptimizedForStreaming1 = ResponseBodyDeserializer.DeserializeUndiscriminatedUnionMember<OptimizedForStreaming1>(json)
                    };
                }
                catch (ResponseBodyDeserializer.MissingMemberException)
                {
                    fallbackCandidates.Add((typeof(OptimizedForStreaming1), new GetAllMediaLibraryOptimizedForStreaming(GetAllMediaLibraryOptimizedForStreamingType.OptimizedForStreaming1), "OptimizedForStreaming1"));
                }
                catch (ResponseBodyDeserializer.DeserializationException)
                {
                    // try next option
                }
                catch (Exception)
                {
                    throw;
                }

                try
                {
                    var converted = Convert.ToBoolean(json);
                    return new GetAllMediaLibraryOptimizedForStreaming(GetAllMediaLibraryOptimizedForStreamingType.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;
                }
                GetAllMediaLibraryOptimizedForStreaming res = (GetAllMediaLibraryOptimizedForStreaming)value;
                if (GetAllMediaLibraryOptimizedForStreamingType.FromString(res.Type).Equals(GetAllMediaLibraryOptimizedForStreamingType.Null))
                {
                    writer.WriteRawValue("null");
                    return;
                }
                if (res.OptimizedForStreaming1 != null)
                {
                    writer.WriteRawValue(Utilities.SerializeJSON(res.OptimizedForStreaming1));
                    return;
                }
                if (res.Boolean != null)
                {
                    writer.WriteRawValue(Utilities.SerializeJSON(res.Boolean));
                    return;
                }

            }

        }

    }
}