// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) TypeSpec Code Generator.

package tsptest.armstreamstyleserialization.models;

import com.azure.core.annotation.Immutable;
import com.azure.core.util.Base64Url;
import com.azure.core.util.CoreUtils;
import com.azure.core.util.DateTimeRfc1123;
import com.azure.core.util.logging.ClientLogger;
import com.azure.json.JsonReader;
import com.azure.json.JsonSerializable;
import com.azure.json.JsonToken;
import com.azure.json.JsonWriter;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

/**
 * The Encoded model.
 */
@Immutable
public final class Encoded implements JsonSerializable<Encoded> {
    private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];

    /*
     * The timeInSeconds property.
     */
    private Long timeInSeconds;

    /*
     * The timeInSecondsPrimitive property.
     */
    private long timeInSecondsPrimitive;

    /*
     * The timeInSecondsFraction property.
     */
    private Double timeInSecondsFraction;

    /*
     * The timeInSecondsFractionPrimitive property.
     */
    private double timeInSecondsFractionPrimitive;

    /*
     * The dateTime property.
     */
    private OffsetDateTime dateTime;

    /*
     * The dateTimeRfc7231 property.
     */
    private DateTimeRfc1123 dateTimeRfc7231;

    /*
     * The unixTimestamp property.
     */
    private Long unixTimestamp;

    /*
     * The unixTimestampPrimitive property.
     */
    private long unixTimestampPrimitive;

    /*
     * The base64 property.
     */
    private byte[] base64;

    /*
     * The base64url property.
     */
    private Base64Url base64url;

    /*
     * The unknownDurationFormat property.
     */
    private String unknownDurationFormat;

    /*
     * The unknownDateTimeFormat property.
     */
    private String unknownDateTimeFormat;

    /*
     * The unknownBytes property.
     */
    private String unknownBytes;

    /**
     * Creates an instance of Encoded class.
     */
    private Encoded() {
    }

    /**
     * Get the timeInSeconds property: The timeInSeconds property.
     * 
     * @return the timeInSeconds value.
     */
    public Duration timeInSeconds() {
        if (this.timeInSeconds == null) {
            return null;
        }
        return Duration.ofSeconds(this.timeInSeconds);
    }

    /**
     * Get the timeInSecondsPrimitive property: The timeInSecondsPrimitive property.
     * 
     * @return the timeInSecondsPrimitive value.
     */
    public Duration timeInSecondsPrimitive() {
        return Duration.ofSeconds(this.timeInSecondsPrimitive);
    }

    /**
     * Get the timeInSecondsFraction property: The timeInSecondsFraction property.
     * 
     * @return the timeInSecondsFraction value.
     */
    public Duration timeInSecondsFraction() {
        if (this.timeInSecondsFraction == null) {
            return null;
        }
        return Duration.ofNanos((long) (this.timeInSecondsFraction * 1000_000_000L));
    }

    /**
     * Get the timeInSecondsFractionPrimitive property: The timeInSecondsFractionPrimitive property.
     * 
     * @return the timeInSecondsFractionPrimitive value.
     */
    public Duration timeInSecondsFractionPrimitive() {
        return Duration.ofNanos((long) (this.timeInSecondsFractionPrimitive * 1000_000_000L));
    }

    /**
     * Get the dateTime property: The dateTime property.
     * 
     * @return the dateTime value.
     */
    public OffsetDateTime dateTime() {
        return this.dateTime;
    }

    /**
     * Get the dateTimeRfc7231 property: The dateTimeRfc7231 property.
     * 
     * @return the dateTimeRfc7231 value.
     */
    public OffsetDateTime dateTimeRfc7231() {
        if (this.dateTimeRfc7231 == null) {
            return null;
        }
        return this.dateTimeRfc7231.getDateTime();
    }

    /**
     * Get the unixTimestamp property: The unixTimestamp property.
     * 
     * @return the unixTimestamp value.
     */
    public OffsetDateTime unixTimestamp() {
        if (this.unixTimestamp == null) {
            return null;
        }
        return OffsetDateTime.ofInstant(Instant.ofEpochSecond(this.unixTimestamp), ZoneOffset.UTC);
    }

    /**
     * Get the unixTimestampPrimitive property: The unixTimestampPrimitive property.
     * 
     * @return the unixTimestampPrimitive value.
     */
    public OffsetDateTime unixTimestampPrimitive() {
        return OffsetDateTime.ofInstant(Instant.ofEpochSecond(this.unixTimestampPrimitive), ZoneOffset.UTC);
    }

    /**
     * Get the base64 property: The base64 property.
     * 
     * @return the base64 value.
     */
    public byte[] base64() {
        return CoreUtils.clone(this.base64);
    }

    /**
     * Get the base64url property: The base64url property.
     * 
     * @return the base64url value.
     */
    public byte[] base64url() {
        if (this.base64url == null) {
            return EMPTY_BYTE_ARRAY;
        }
        return this.base64url.decodedBytes();
    }

    /**
     * Get the unknownDurationFormat property: The unknownDurationFormat property.
     * 
     * @return the unknownDurationFormat value.
     */
    public String unknownDurationFormat() {
        return this.unknownDurationFormat;
    }

    /**
     * Get the unknownDateTimeFormat property: The unknownDateTimeFormat property.
     * 
     * @return the unknownDateTimeFormat value.
     */
    public String unknownDateTimeFormat() {
        return this.unknownDateTimeFormat;
    }

    /**
     * Get the unknownBytes property: The unknownBytes property.
     * 
     * @return the unknownBytes value.
     */
    public String unknownBytes() {
        return this.unknownBytes;
    }

    /**
     * Validates the instance.
     * 
     * @throws IllegalArgumentException thrown if the instance is not valid.
     */
    public void validate() {
        if (timeInSecondsPrimitive() == null) {
            throw LOGGER.atError()
                .log(new IllegalArgumentException("Missing required property timeInSecondsPrimitive in model Encoded"));
        }
        if (timeInSecondsFractionPrimitive() == null) {
            throw LOGGER.atError()
                .log(new IllegalArgumentException(
                    "Missing required property timeInSecondsFractionPrimitive in model Encoded"));
        }
        if (unixTimestampPrimitive() == null) {
            throw LOGGER.atError()
                .log(new IllegalArgumentException("Missing required property unixTimestampPrimitive in model Encoded"));
        }
    }

    private static final ClientLogger LOGGER = new ClientLogger(Encoded.class);

    /**
     * {@inheritDoc}
     */
    @Override
    public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
        jsonWriter.writeStartObject();
        jsonWriter.writeLongField("timeInSecondsPrimitive", this.timeInSecondsPrimitive);
        jsonWriter.writeDoubleField("timeInSecondsFractionPrimitive", this.timeInSecondsFractionPrimitive);
        jsonWriter.writeLongField("unixTimestampPrimitive", this.unixTimestampPrimitive);
        jsonWriter.writeNumberField("timeInSeconds", this.timeInSeconds);
        jsonWriter.writeNumberField("timeInSecondsFraction", this.timeInSecondsFraction);
        jsonWriter.writeStringField("dateTime",
            this.dateTime == null ? null : DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(this.dateTime));
        jsonWriter.writeStringField("dateTimeRfc7231", Objects.toString(this.dateTimeRfc7231, null));
        jsonWriter.writeNumberField("unixTimestamp", this.unixTimestamp);
        jsonWriter.writeBinaryField("base64", this.base64);
        jsonWriter.writeStringField("base64url", Objects.toString(this.base64url, null));
        jsonWriter.writeStringField("unknownDurationFormat", this.unknownDurationFormat);
        jsonWriter.writeStringField("unknownDateTimeFormat", this.unknownDateTimeFormat);
        jsonWriter.writeStringField("unknownBytes", this.unknownBytes);
        return jsonWriter.writeEndObject();
    }

    /**
     * Reads an instance of Encoded from the JsonReader.
     * 
     * @param jsonReader The JsonReader being read.
     * @return An instance of Encoded if the JsonReader was pointing to an instance of it, or null if it was pointing to
     * JSON null.
     * @throws IllegalStateException If the deserialized JSON object was missing any required properties.
     * @throws IOException If an error occurs while reading the Encoded.
     */
    public static Encoded fromJson(JsonReader jsonReader) throws IOException {
        return jsonReader.readObject(reader -> {
            Encoded deserializedEncoded = new Encoded();
            while (reader.nextToken() != JsonToken.END_OBJECT) {
                String fieldName = reader.getFieldName();
                reader.nextToken();

                if ("timeInSecondsPrimitive".equals(fieldName)) {
                    deserializedEncoded.timeInSecondsPrimitive = reader.getLong();
                } else if ("timeInSecondsFractionPrimitive".equals(fieldName)) {
                    deserializedEncoded.timeInSecondsFractionPrimitive = reader.getDouble();
                } else if ("unixTimestampPrimitive".equals(fieldName)) {
                    deserializedEncoded.unixTimestampPrimitive = reader.getLong();
                } else if ("timeInSeconds".equals(fieldName)) {
                    deserializedEncoded.timeInSeconds = reader.getNullable(JsonReader::getLong);
                } else if ("timeInSecondsFraction".equals(fieldName)) {
                    deserializedEncoded.timeInSecondsFraction = reader.getNullable(JsonReader::getDouble);
                } else if ("dateTime".equals(fieldName)) {
                    deserializedEncoded.dateTime = reader
                        .getNullable(nonNullReader -> CoreUtils.parseBestOffsetDateTime(nonNullReader.getString()));
                } else if ("dateTimeRfc7231".equals(fieldName)) {
                    deserializedEncoded.dateTimeRfc7231
                        = reader.getNullable(nonNullReader -> new DateTimeRfc1123(nonNullReader.getString()));
                } else if ("unixTimestamp".equals(fieldName)) {
                    deserializedEncoded.unixTimestamp = reader.getNullable(JsonReader::getLong);
                } else if ("base64".equals(fieldName)) {
                    deserializedEncoded.base64 = reader.getBinary();
                } else if ("base64url".equals(fieldName)) {
                    deserializedEncoded.base64url
                        = reader.getNullable(nonNullReader -> new Base64Url(nonNullReader.getString()));
                } else if ("unknownDurationFormat".equals(fieldName)) {
                    deserializedEncoded.unknownDurationFormat = reader.getString();
                } else if ("unknownDateTimeFormat".equals(fieldName)) {
                    deserializedEncoded.unknownDateTimeFormat = reader.getString();
                } else if ("unknownBytes".equals(fieldName)) {
                    deserializedEncoded.unknownBytes = reader.getString();
                } else {
                    reader.skipChildren();
                }
            }

            return deserializedEncoded;
        });
    }
}
