// <auto-generated>
/*
 * API ebike_v1 1.0.0
 *
 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
 *
 * The version of the OpenAPI document: 1.0.0
 * Generated by: https://github.com/openapitools/openapi-generator.git
 */

#nullable enable

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.ComponentModel.DataAnnotations;
using Org.OpenAPITools.Client;

namespace Org.OpenAPITools.Model
{
    /// <summary>
    /// Route
    /// </summary>
    public partial class Route : IValidatableObject
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Route" /> class.
        /// </summary>
        /// <param name="distance">The distance in meters</param>
        /// <param name="ebikeId">ebikeId</param>
        /// <param name="name">The name of the route</param>
        /// <param name="riderId">riderId</param>
        /// <param name="id">id (default to 0)</param>
        [JsonConstructor]
        public Route(int distance, int ebikeId, string name, int riderId, Option<int?> id = default)
        {
            Distance = distance;
            EbikeId = ebikeId;
            Name = name;
            RiderId = riderId;
            IdOption = id;
            OnCreated();
        }

        partial void OnCreated();

        /// <summary>
        /// The distance in meters
        /// </summary>
        /// <value>The distance in meters</value>
        [JsonPropertyName("distance")]
        public int Distance { get; set; }

        /// <summary>
        /// Gets or Sets EbikeId
        /// </summary>
        [JsonPropertyName("ebike_id")]
        public int EbikeId { get; set; }

        /// <summary>
        /// The name of the route
        /// </summary>
        /// <value>The name of the route</value>
        [JsonPropertyName("name")]
        public string Name { get; set; }

        /// <summary>
        /// Gets or Sets RiderId
        /// </summary>
        [JsonPropertyName("rider_id")]
        public int RiderId { get; set; }

        /// <summary>
        /// Used to track the state of Id
        /// </summary>
        [JsonIgnore]
        [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
        public Option<int?> IdOption { get; private set; }

        /// <summary>
        /// Gets or Sets Id
        /// </summary>
        [JsonPropertyName("id")]
        public int? Id { get { return this.IdOption.Value; } set { this.IdOption = new(value); } }

        /// <summary>
        /// Returns the string presentation of the object
        /// </summary>
        /// <returns>String presentation of the object</returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("class Route {\n");
            sb.Append("  Distance: ").Append(Distance).Append("\n");
            sb.Append("  EbikeId: ").Append(EbikeId).Append("\n");
            sb.Append("  Name: ").Append(Name).Append("\n");
            sb.Append("  RiderId: ").Append(RiderId).Append("\n");
            sb.Append("  Id: ").Append(Id).Append("\n");
            sb.Append("}\n");
            return sb.ToString();
        }

        /// <summary>
        /// To validate all properties of the instance
        /// </summary>
        /// <param name="validationContext">Validation context</param>
        /// <returns>Validation Result</returns>
        IEnumerable<ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
        {
            yield break;
        }
    }

    /// <summary>
    /// A Json converter for type <see cref="Route" />
    /// </summary>
    public class RouteJsonConverter : JsonConverter<Route>
    {
        /// <summary>
        /// Deserializes json to <see cref="Route" />
        /// </summary>
        /// <param name="utf8JsonReader"></param>
        /// <param name="typeToConvert"></param>
        /// <param name="jsonSerializerOptions"></param>
        /// <returns></returns>
        /// <exception cref="JsonException"></exception>
        public override Route Read(ref Utf8JsonReader utf8JsonReader, Type typeToConvert, JsonSerializerOptions jsonSerializerOptions)
        {
            int currentDepth = utf8JsonReader.CurrentDepth;

            if (utf8JsonReader.TokenType != JsonTokenType.StartObject && utf8JsonReader.TokenType != JsonTokenType.StartArray)
                throw new JsonException();

            JsonTokenType startingTokenType = utf8JsonReader.TokenType;

            Option<int?> distance = default;
            Option<int?> ebikeId = default;
            Option<string?> name = default;
            Option<int?> riderId = default;
            Option<int?> id = default;

            while (utf8JsonReader.Read())
            {
                if (startingTokenType == JsonTokenType.StartObject && utf8JsonReader.TokenType == JsonTokenType.EndObject && currentDepth == utf8JsonReader.CurrentDepth)
                    break;

                if (startingTokenType == JsonTokenType.StartArray && utf8JsonReader.TokenType == JsonTokenType.EndArray && currentDepth == utf8JsonReader.CurrentDepth)
                    break;

                if (utf8JsonReader.TokenType == JsonTokenType.PropertyName && currentDepth == utf8JsonReader.CurrentDepth - 1)
                {
                    string? localVarJsonPropertyName = utf8JsonReader.GetString();
                    utf8JsonReader.Read();

                    switch (localVarJsonPropertyName)
                    {
                        case "distance":
                            distance = new Option<int?>(utf8JsonReader.TokenType == JsonTokenType.Null ? (int?)null : utf8JsonReader.GetInt32());
                            break;
                        case "ebike_id":
                            ebikeId = new Option<int?>(utf8JsonReader.TokenType == JsonTokenType.Null ? (int?)null : utf8JsonReader.GetInt32());
                            break;
                        case "name":
                            name = new Option<string?>(utf8JsonReader.GetString()!);
                            break;
                        case "rider_id":
                            riderId = new Option<int?>(utf8JsonReader.TokenType == JsonTokenType.Null ? (int?)null : utf8JsonReader.GetInt32());
                            break;
                        case "id":
                            id = new Option<int?>(utf8JsonReader.TokenType == JsonTokenType.Null ? (int?)null : utf8JsonReader.GetInt32());
                            break;
                        default:
                            break;
                    }
                }
            }

            if (!distance.IsSet)
                throw new ArgumentException("Property is required for class Route.", nameof(distance));

            if (!ebikeId.IsSet)
                throw new ArgumentException("Property is required for class Route.", nameof(ebikeId));

            if (!name.IsSet)
                throw new ArgumentException("Property is required for class Route.", nameof(name));

            if (!riderId.IsSet)
                throw new ArgumentException("Property is required for class Route.", nameof(riderId));

            if (distance.IsSet && distance.Value == null)
                throw new ArgumentNullException(nameof(distance), "Property is not nullable for class Route.");

            if (ebikeId.IsSet && ebikeId.Value == null)
                throw new ArgumentNullException(nameof(ebikeId), "Property is not nullable for class Route.");

            if (name.IsSet && name.Value == null)
                throw new ArgumentNullException(nameof(name), "Property is not nullable for class Route.");

            if (riderId.IsSet && riderId.Value == null)
                throw new ArgumentNullException(nameof(riderId), "Property is not nullable for class Route.");

            if (id.IsSet && id.Value == null)
                throw new ArgumentNullException(nameof(id), "Property is not nullable for class Route.");

            return new Route(distance.Value!.Value!, ebikeId.Value!.Value!, name.Value!, riderId.Value!.Value!, id);
        }

        /// <summary>
        /// Serializes a <see cref="Route" />
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="route"></param>
        /// <param name="jsonSerializerOptions"></param>
        /// <exception cref="NotImplementedException"></exception>
        public override void Write(Utf8JsonWriter writer, Route route, JsonSerializerOptions jsonSerializerOptions)
        {
            writer.WriteStartObject();

            WriteProperties(writer, route, jsonSerializerOptions);
            writer.WriteEndObject();
        }

        /// <summary>
        /// Serializes the properties of <see cref="Route" />
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="route"></param>
        /// <param name="jsonSerializerOptions"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void WriteProperties(Utf8JsonWriter writer, Route route, JsonSerializerOptions jsonSerializerOptions)
        {
            if (route.Name == null)
                throw new ArgumentNullException(nameof(route.Name), "Property is required for class Route.");

            writer.WriteNumber("distance", route.Distance);

            writer.WriteNumber("ebike_id", route.EbikeId);

            writer.WriteString("name", route.Name);

            writer.WriteNumber("rider_id", route.RiderId);

            if (route.IdOption.IsSet)
                writer.WriteNumber("id", route.IdOption.Value!.Value);
        }
    }
}
