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

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

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

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

        public CreatedByType Type {get; set; }


        public static CreatedBy CreateStr(string str) {
            CreatedByType typ = CreatedByType.Str;

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

        public static CreatedBy CreateNumber(double number) {
            CreatedByType typ = CreatedByType.Number;

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

        public static CreatedBy CreateNull() {
            CreatedByType typ = CreatedByType.Null;
            return new CreatedBy(typ);
        }

        public class CreatedByConverter : JsonConverter
        {

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

            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 CreatedBy(CreatedByType.Str) {
                        Str = json[1..^1]
                    };
                } 
                try {
                    var converted = Convert.ToDouble(json);
                    return new CreatedBy(CreatedByType.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;
                }
                CreatedBy res = (CreatedBy)value;
                if (CreatedByType.FromString(res.Type).Equals(CreatedByType.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;
                }

            }
        }

    }

}