// <auto-generated>
/*
 * OpenAPI Petstore
 *
 * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
 *
 * The version of the OpenAPI document: 1.0.0
 * Generated by: https://github.com/openapitools/openapi-generator.git
 */

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 OpenAPIClientUtils = Org.OpenAPITools.Client.ClientUtils;
using Org.OpenAPITools.Client;

namespace Org.OpenAPITools.Model
{
    /// <summary>
    /// pet status in the store
    /// </summary>
    /// <value>pet status in the store</value>
    public enum PetStatus
    {
        /// <summary>
        /// Enum Available for value: available
        /// </summary>
        Available = 1,

        /// <summary>
        /// Enum Pending for value: pending
        /// </summary>
        Pending = 2,

        /// <summary>
        /// Enum Sold for value: sold
        /// </summary>
        Sold = 3
    }

    /// <summary>
    /// Converts <see cref="PetStatus"/> to and from the JSON value
    /// </summary>
    public static class PetStatusValueConverter
    {
        /// <summary>
        /// Parses a given value to <see cref="PetStatus"/>
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static PetStatus FromString(string value)
        {
            if (value.Equals("available"))
                return PetStatus.Available;

            if (value.Equals("pending"))
                return PetStatus.Pending;

            if (value.Equals("sold"))
                return PetStatus.Sold;

            throw new NotImplementedException($"Could not convert value to type PetStatus: '{value}'");
        }

        /// <summary>
        /// Parses a given value to <see cref="PetStatus"/>
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static PetStatus? FromStringOrDefault(string value)
        {
            if (value.Equals("available"))
                return PetStatus.Available;

            if (value.Equals("pending"))
                return PetStatus.Pending;

            if (value.Equals("sold"))
                return PetStatus.Sold;

            return null;
        }

        /// <summary>
        /// Converts the <see cref="PetStatus"/> to the json value
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public static string ToJsonValue(PetStatus value)
        {
            if (value == PetStatus.Available)
                return "available";

            if (value == PetStatus.Pending)
                return "pending";

            if (value == PetStatus.Sold)
                return "sold";

            throw new NotImplementedException($"Value could not be handled: '{value}'");
        }
    }

    /// <summary>
    /// A Json converter for type <see cref="PetStatus"/>
    /// </summary>
    /// <exception cref="NotImplementedException"></exception>
    public class PetStatusJsonConverter : JsonConverter<PetStatus>
    {
        /// <summary>
        /// Returns a  from the Json object
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="typeToConvert"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public override PetStatus Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            string rawValue = reader.GetString();

            PetStatus? result = rawValue == null
                ? null
                : PetStatusValueConverter.FromStringOrDefault(rawValue);

            if (result != null)
                return result.Value;

            throw new JsonException();
        }

        /// <summary>
        /// Writes the PetStatus to the json writer
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="petStatus"></param>
        /// <param name="options"></param>
        public override void Write(Utf8JsonWriter writer, PetStatus petStatus, JsonSerializerOptions options)
        {
            writer.WriteStringValue(petStatus.ToString());
        }
    }

    /// <summary>
    /// A Json converter for type <see cref="PetStatus"/>
    /// </summary>
    public class PetStatusNullableJsonConverter : JsonConverter<PetStatus?>
    {
        /// <summary>
        /// Returns a PetStatus from the Json object
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="typeToConvert"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public override PetStatus? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            string rawValue = reader.GetString();

            PetStatus? result = rawValue == null
                ? null
                : PetStatusValueConverter.FromStringOrDefault(rawValue);

            if (result != null)
                return result.Value;

            throw new JsonException();
        }

        /// <summary>
        /// Writes the DateTime to the json writer
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="petStatus"></param>
        /// <param name="options"></param>
        public override void Write(Utf8JsonWriter writer, PetStatus? petStatus, JsonSerializerOptions options)
        {
            writer.WriteStringValue(petStatus?.ToString() ?? "null");
        }
    }
}
