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

        public string Value { get; private set; }
        public static GetMediaMetaDataOptimizedForStreamingType GetMediaMetaDataOptimizedForStreaming1 { get { return new GetMediaMetaDataOptimizedForStreamingType("get-media-meta-data_optimizedForStreaming_1"); } }
        
        public static GetMediaMetaDataOptimizedForStreamingType Boolean { get { return new GetMediaMetaDataOptimizedForStreamingType("boolean"); } }
        
        public static GetMediaMetaDataOptimizedForStreamingType Null { get { return new GetMediaMetaDataOptimizedForStreamingType("null"); } }

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

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

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

        public GetMediaMetaDataOptimizedForStreamingType Type { get; set; }


        public static GetMediaMetaDataOptimizedForStreaming CreateGetMediaMetaDataOptimizedForStreaming1(GetMediaMetaDataOptimizedForStreaming1 getMediaMetaDataOptimizedForStreaming1) {
            GetMediaMetaDataOptimizedForStreamingType typ = GetMediaMetaDataOptimizedForStreamingType.GetMediaMetaDataOptimizedForStreaming1;

            GetMediaMetaDataOptimizedForStreaming res = new GetMediaMetaDataOptimizedForStreaming(typ);
            res.GetMediaMetaDataOptimizedForStreaming1 = getMediaMetaDataOptimizedForStreaming1;
            return res;
        }

        public static GetMediaMetaDataOptimizedForStreaming CreateBoolean(bool boolean) {
            GetMediaMetaDataOptimizedForStreamingType typ = GetMediaMetaDataOptimizedForStreamingType.Boolean;

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

        public static GetMediaMetaDataOptimizedForStreaming CreateNull() {
            GetMediaMetaDataOptimizedForStreamingType typ = GetMediaMetaDataOptimizedForStreamingType.Null;
            return new GetMediaMetaDataOptimizedForStreaming(typ);
        }

        public class GetMediaMetaDataOptimizedForStreamingConverter : JsonConverter
        {

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

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

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

            }

        }

    }
}