<gh_stars>0// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

// <auto-generated/>

#nullable disable

using System;
using System.Collections.Generic;
using System.Text.Json;
using Azure;
using Azure.Core;

namespace NotificationHubs.Models
{
    public partial class NotificationHubJob : IUtf8JsonSerializable
    {
        void IUtf8JsonSerializable.Write(Utf8JsonWriter writer)
        {
            writer.WriteStartObject();
            if (Optional.IsDefined(JobId))
            {
                writer.WritePropertyName("jobId");
                writer.WriteStringValue(JobId);
            }
            if (Optional.IsDefined(OutputFileName))
            {
                writer.WritePropertyName("outputFileName");
                writer.WriteStringValue(OutputFileName);
            }
            if (Optional.IsDefined(FailuresFileName))
            {
                writer.WritePropertyName("failuresFileName");
                writer.WriteStringValue(FailuresFileName);
            }
            if (Optional.IsDefined(Progress))
            {
                if (Progress != null)
                {
                    writer.WritePropertyName("progress");
                    writer.WriteNumberValue(Progress.Value);
                }
                else
                {
                    writer.WriteNull("progress");
                }
            }
            writer.WritePropertyName("type");
            writer.WriteStringValue(Type.ToSerialString());
            if (Optional.IsDefined(Status))
            {
                if (Status != null)
                {
                    writer.WritePropertyName("status");
                    writer.WriteStringValue(Status.Value.ToSerialString());
                }
                else
                {
                    writer.WriteNull("status");
                }
            }
            writer.WritePropertyName("outputContainerUrl");
            writer.WriteStringValue(OutputContainerUrl);
            if (Optional.IsDefined(ImportFileUrl))
            {
                writer.WritePropertyName("importFileUrl");
                writer.WriteStringValue(ImportFileUrl);
            }
            if (Optional.IsCollectionDefined(InputProperties))
            {
                writer.WritePropertyName("inputProperties");
                writer.WriteStartObject();
                foreach (var item in InputProperties)
                {
                    writer.WritePropertyName(item.Key);
                    writer.WriteStringValue(item.Value);
                }
                writer.WriteEndObject();
            }
            if (Optional.IsDefined(Failure))
            {
                writer.WritePropertyName("failure");
                writer.WriteStringValue(Failure);
            }
            if (Optional.IsCollectionDefined(OutputProperties))
            {
                writer.WritePropertyName("outputProperties");
                writer.WriteStartObject();
                foreach (var item in OutputProperties)
                {
                    writer.WritePropertyName(item.Key);
                    writer.WriteStringValue(item.Value);
                }
                writer.WriteEndObject();
            }
            if (Optional.IsDefined(CreatedAt))
            {
                if (CreatedAt != null)
                {
                    writer.WritePropertyName("createdAt");
                    writer.WriteStringValue(CreatedAt.Value, "O");
                }
                else
                {
                    writer.WriteNull("createdAt");
                }
            }
            if (Optional.IsDefined(UpdatedAt))
            {
                if (UpdatedAt != null)
                {
                    writer.WritePropertyName("updatedAt");
                    writer.WriteStringValue(UpdatedAt.Value, "O");
                }
                else
                {
                    writer.WriteNull("updatedAt");
                }
            }
            writer.WriteEndObject();
        }

        internal static NotificationHubJob DeserializeNotificationHubJob(JsonElement element)
        {
            Optional<string> jobId = default;
            Optional<string> outputFileName = default;
            Optional<string> failuresFileName = default;
            Optional<double?> progress = default;
            NotificationHubJobType type = default;
            Optional<NotificationHubJobStatus?> status = default;
            string outputContainerUrl = default;
            Optional<string> importFileUrl = default;
            Optional<IDictionary<string, string>> inputProperties = default;
            Optional<string> failure = default;
            Optional<IDictionary<string, string>> outputProperties = default;
            Optional<DateTimeOffset?> createdAt = default;
            Optional<DateTimeOffset?> updatedAt = default;
            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("jobId"))
                {
                    jobId = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("outputFileName"))
                {
                    outputFileName = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("failuresFileName"))
                {
                    failuresFileName = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("progress"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        progress = null;
                        continue;
                    }
                    progress = property.Value.GetDouble();
                    continue;
                }
                if (property.NameEquals("type"))
                {
                    type = property.Value.GetString().ToNotificationHubJobType();
                    continue;
                }
                if (property.NameEquals("status"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        status = null;
                        continue;
                    }
                    status = property.Value.GetString().ToNotificationHubJobStatus();
                    continue;
                }
                if (property.NameEquals("outputContainerUrl"))
                {
                    outputContainerUrl = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("importFileUrl"))
                {
                    importFileUrl = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("inputProperties"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    Dictionary<string, string> dictionary = new Dictionary<string, string>();
                    foreach (var property0 in property.Value.EnumerateObject())
                    {
                        dictionary.Add(property0.Name, property0.Value.GetString());
                    }
                    inputProperties = dictionary;
                    continue;
                }
                if (property.NameEquals("failure"))
                {
                    failure = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("outputProperties"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    Dictionary<string, string> dictionary = new Dictionary<string, string>();
                    foreach (var property0 in property.Value.EnumerateObject())
                    {
                        dictionary.Add(property0.Name, property0.Value.GetString());
                    }
                    outputProperties = dictionary;
                    continue;
                }
                if (property.NameEquals("createdAt"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        createdAt = null;
                        continue;
                    }
                    createdAt = property.Value.GetDateTimeOffset("O");
                    continue;
                }
                if (property.NameEquals("updatedAt"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        updatedAt = null;
                        continue;
                    }
                    updatedAt = property.Value.GetDateTimeOffset("O");
                    continue;
                }
            }
            return new NotificationHubJob(jobId, outputFileName, failuresFileName, Optional.ToNullable(progress), type, Optional.ToNullable(status), outputContainerUrl, importFileUrl, Optional.ToDictionary(inputProperties), failure, Optional.ToDictionary(outputProperties), Optional.ToNullable(createdAt), Optional.ToNullable(updatedAt));
        }

        internal RequestContent ToRequestContent()
        {
            var content = new Utf8JsonRequestContent();
            content.JsonWriter.WriteObjectValue(this);
            return content;
        }

        internal static NotificationHubJob FromResponse(Response response)
        {
            using var document = JsonDocument.Parse(response.Content);
            return DeserializeNotificationHubJob(document.RootElement);
        }
    }
}
