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

        public string Value { get; private set; }
        public static OrgTokenScopesType ArrayOfScope { get { return new OrgTokenScopesType("arrayOfScope"); } }
        public static OrgTokenScopesType OrgToken2 { get { return new OrgTokenScopesType("OrgToken_2"); } }
        public static OrgTokenScopesType Null { get { return new OrgTokenScopesType("null"); } }

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

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

        public OrgTokenScopesType Type {get; set; }


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

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

        public static OrgTokenScopes CreateOrgToken2(OrgToken2 orgToken2) {
            OrgTokenScopesType typ = OrgTokenScopesType.OrgToken2;

            OrgTokenScopes res = new OrgTokenScopes(typ);
            res.OrgToken2 = orgToken2;
            return res;
        }

        public static OrgTokenScopes CreateNull() {
            OrgTokenScopesType typ = OrgTokenScopesType.Null;
            return new OrgTokenScopes(typ);
        }

        public class OrgTokenScopesConverter : JsonConverter
        {

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

            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 OrgTokenScopes(OrgTokenScopesType.ArrayOfScope) {
                        ArrayOfScope = arrayOfScope
                    };
                }
                catch (Exception ex)
                {
                    if (!(ex is Newtonsoft.Json.JsonReaderException || ex is Newtonsoft.Json.JsonSerializationException)) {
                        throw ex;
                    }
                }
                try
                {
                    OrgToken2? orgToken2 = JsonConvert.DeserializeObject<OrgToken2>(json, new JsonSerializerSettings(){ NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Error, Converters = Utilities.GetJsonDeserializers(typeof(OrgToken2))});
                    return new OrgTokenScopes(OrgTokenScopesType.OrgToken2) {
                        OrgToken2 = orgToken2
                    };
                }
                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;
                }
                OrgTokenScopes res = (OrgTokenScopes)value;
                if (OrgTokenScopesType.FromString(res.Type).Equals(OrgTokenScopesType.Null))
                {
                    writer.WriteRawValue("null");
                    return;
                }
                if (res.ArrayOfScope != null)
                {
                    writer.WriteRawValue(Utilities.SerializeJSON(res.ArrayOfScope));
                    return;
                }
                if (res.OrgToken2 != null)
                {
                    writer.WriteRawValue(Utilities.SerializeJSON(res.OrgToken2));
                    return;
                }

            }
        }

    }

}