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

        public string Value { get; private set; }
        public static GetMediaMetaDataLibraryOptimizedForStreamingType GetMediaMetaDataOptimizedForStreamingLibrary1 { get { return new GetMediaMetaDataLibraryOptimizedForStreamingType("get-media-meta-data_optimizedForStreaming_Library_1"); } }
        
        public static GetMediaMetaDataLibraryOptimizedForStreamingType Boolean { get { return new GetMediaMetaDataLibraryOptimizedForStreamingType("boolean"); } }
        
        public static GetMediaMetaDataLibraryOptimizedForStreamingType Null { get { return new GetMediaMetaDataLibraryOptimizedForStreamingType("null"); } }

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

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

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

        public GetMediaMetaDataLibraryOptimizedForStreamingType Type { get; set; }


        public static GetMediaMetaDataLibraryOptimizedForStreaming CreateGetMediaMetaDataOptimizedForStreamingLibrary1(GetMediaMetaDataOptimizedForStreamingLibrary1 getMediaMetaDataOptimizedForStreamingLibrary1) {
            GetMediaMetaDataLibraryOptimizedForStreamingType typ = GetMediaMetaDataLibraryOptimizedForStreamingType.GetMediaMetaDataOptimizedForStreamingLibrary1;

            GetMediaMetaDataLibraryOptimizedForStreaming res = new GetMediaMetaDataLibraryOptimizedForStreaming(typ);
            res.GetMediaMetaDataOptimizedForStreamingLibrary1 = getMediaMetaDataOptimizedForStreamingLibrary1;
            return res;
        }

        public static GetMediaMetaDataLibraryOptimizedForStreaming CreateBoolean(bool boolean) {
            GetMediaMetaDataLibraryOptimizedForStreamingType typ = GetMediaMetaDataLibraryOptimizedForStreamingType.Boolean;

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

        public static GetMediaMetaDataLibraryOptimizedForStreaming CreateNull() {
            GetMediaMetaDataLibraryOptimizedForStreamingType typ = GetMediaMetaDataLibraryOptimizedForStreamingType.Null;
            return new GetMediaMetaDataLibraryOptimizedForStreaming(typ);
        }

        public class GetMediaMetaDataLibraryOptimizedForStreamingConverter : JsonConverter
        {

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

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

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

            }

        }

    }
}