
//------------------------------------------------------------------------------
// <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 HathoraCloud.Models.Shared
{
    using HathoraCloud.Models.Shared;
    using HathoraCloud.Utils;
    using Newtonsoft.Json.Linq;
    using Newtonsoft.Json;
    using System.Collections.Generic;
    using System.Numerics;
    using System;
    using UnityEngine;
    

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

        public string Value { get; private set; }
        public static ScopesType ArrayOfScope { get { return new ScopesType("arrayOfScope"); } }
        public static ScopesType Two { get { return new ScopesType("2"); } }
        public static ScopesType Null { get { return new ScopesType("null"); } }

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

        public override int GetHashCode()
        {
            return Value.GetHashCode();
        }
    }
    
/// <summary>
/// If not defined, the token has Admin access.
/// </summary>
    [JsonConverter(typeof(Scopes.ScopesConverter))]
    public class Scopes {
        public Scopes(ScopesType type) {
            Type = type;
        }
        public List<Scope>? ArrayOfScope { get; set; } 
        public Two? Two { get; set; } 

        public ScopesType Type {get; set; }


        public static Scopes CreateArrayOfScope(List<Scope> arrayOfScope) {
            ScopesType typ = ScopesType.ArrayOfScope;

            Scopes res = new Scopes(typ);
            res.ArrayOfScope = arrayOfScope;
            return res;
        }

        public static Scopes CreateTwo(Two two) {
            ScopesType typ = ScopesType.Two;

            Scopes res = new Scopes(typ);
            res.Two = two;
            return res;
        }

        public static Scopes CreateNull() {
            ScopesType typ = ScopesType.Null;
            return new Scopes(typ);
        }

        public class ScopesConverter : JsonConverter
        {

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

            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;
                }
                try
                {
                    List<Scope>? arrayOfScope = JsonConvert.DeserializeObject<List<Scope>>(json, new JsonSerializerSettings(){ NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Error, Converters = Utilities.GetJsonDeserializers(typeof(List<Scope>))});
                    return new Scopes(ScopesType.ArrayOfScope) {
                        ArrayOfScope = arrayOfScope
                    };
                }
                catch (Exception ex)
                {
                    if (!(ex is Newtonsoft.Json.JsonReaderException || ex is Newtonsoft.Json.JsonSerializationException)) {
                        throw ex;
                    }
                }
                try
                {
                    Two? two = JsonConvert.DeserializeObject<Two>(json, new JsonSerializerSettings(){ NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Error, Converters = Utilities.GetJsonDeserializers(typeof(Two))});
                    return new Scopes(ScopesType.Two) {
                        Two = two
                    };
                }
                catch (Exception ex)
                {
                    if (!(ex is Newtonsoft.Json.JsonReaderException || ex is Newtonsoft.Json.JsonSerializationException)) {
                        throw ex;
                    }
                }

                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;
                }
                Scopes res = (Scopes)value;
                if (ScopesType.FromString(res.Type).Equals(ScopesType.Null))
                {
                    writer.WriteRawValue("null");
                    return;
                }
                if (res.ArrayOfScope != null)
                {
                    writer.WriteRawValue(Utilities.SerializeJSON(res.ArrayOfScope));
                    return;
                }
                if (res.Two != null)
                {
                    writer.WriteRawValue(Utilities.SerializeJSON(res.Two));
                    return;
                }

            }
        }

    }

}