
//------------------------------------------------------------------------------
// <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.Utils;
    using Newtonsoft.Json.Linq;
    using Newtonsoft.Json;
    using System.Numerics;
    using System;
    using UnityEngine;
    

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

        public string Value { get; private set; }
        public static LobbyV3CreatedByType Str { get { return new LobbyV3CreatedByType("str"); } }
        public static LobbyV3CreatedByType Number { get { return new LobbyV3CreatedByType("number"); } }
        public static LobbyV3CreatedByType Null { get { return new LobbyV3CreatedByType("null"); } }

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

        public override int GetHashCode()
        {
            return Value.GetHashCode();
        }
    }
    
/// <summary>
/// UserId or email address for the user that created the lobby.
/// </summary>
    [JsonConverter(typeof(LobbyV3CreatedBy.LobbyV3CreatedByConverter))]
    public class LobbyV3CreatedBy {
        public LobbyV3CreatedBy(LobbyV3CreatedByType type) {
            Type = type;
        }
        public string? Str { get; set; } 
        public double? Number { get; set; } 

        public LobbyV3CreatedByType Type {get; set; }


        public static LobbyV3CreatedBy CreateStr(string str) {
            LobbyV3CreatedByType typ = LobbyV3CreatedByType.Str;

            LobbyV3CreatedBy res = new LobbyV3CreatedBy(typ);
            res.Str = str;
            return res;
        }

        public static LobbyV3CreatedBy CreateNumber(double number) {
            LobbyV3CreatedByType typ = LobbyV3CreatedByType.Number;

            LobbyV3CreatedBy res = new LobbyV3CreatedBy(typ);
            res.Number = number;
            return res;
        }

        public static LobbyV3CreatedBy CreateNull() {
            LobbyV3CreatedByType typ = LobbyV3CreatedByType.Null;
            return new LobbyV3CreatedBy(typ);
        }

        public class LobbyV3CreatedByConverter : JsonConverter
        {

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

            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;
                }
                if (json[0] == '"' && json[^1] == '"'){
                    return new LobbyV3CreatedBy(LobbyV3CreatedByType.Str) {
                        Str = json[1..^1]
                    };
                } 
                try {
                    var converted = Convert.ToDouble(json);
                    return new LobbyV3CreatedBy(LobbyV3CreatedByType.Number) {
                        Number = converted
                    };
                } catch (System.FormatException) {
                    // try next option
                }

                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;
                }
                LobbyV3CreatedBy res = (LobbyV3CreatedBy)value;
                if (LobbyV3CreatedByType.FromString(res.Type).Equals(LobbyV3CreatedByType.Null))
                {
                    writer.WriteRawValue("null");
                    return;
                }
                if (res.Str != null)
                {
                    writer.WriteRawValue(Utilities.SerializeJSON(res.Str));
                    return;
                }
                if (res.Number != null)
                {
                    writer.WriteRawValue(Utilities.SerializeJSON(res.Number));
                    return;
                }

            }
        }

    }

}