using Npgsql.BackendMessages;
using Npgsql.Internal.TypeHandling;
using Npgsql.PostgresTypes;

namespace Npgsql.Internal.TypeHandlers.NumericHandlers;

/// <summary>
/// A type handler for PostgreSQL unsigned 32-bit data types. This is only used for internal types.
/// </summary>
/// <remarks>
/// See https://www.postgresql.org/docs/current/static/datatype-oid.html.
///
/// The type handler API allows customizing Npgsql's behavior in powerful ways. However, although it is public, it
/// should be considered somewhat unstable, and may change in breaking ways, including in non-major releases.
/// Use it at your own risk.
/// </remarks>
public partial class UInt32Handler : NpgsqlSimpleTypeHandler<uint>
{
    public UInt32Handler(PostgresType pgType) : base(pgType) {}

    /// <inheritdoc />
    public override uint Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription = null)
        => buf.ReadUInt32();

    /// <inheritdoc />
    public override int ValidateAndGetLength(uint value, NpgsqlParameter? parameter) => 4;

    /// <inheritdoc />
    public override void Write(uint value, NpgsqlWriteBuffer buf, NpgsqlParameter? parameter)
        => buf.WriteUInt32(value);
}

partial class UInt32Handler
{
    public override int ValidateObjectAndGetLength(object value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
        => value switch
        {

            UInt32 converted => ((INpgsqlSimpleTypeHandler<UInt32>)this).ValidateAndGetLength(converted, parameter),


            _ => throw new InvalidCastException($"Can't write CLR type {value.GetType()} with handler type UInt32Handler")
        };

    public override Task WriteObjectWithLength(object? value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async, CancellationToken cancellationToken = default)
        => value switch
        {

            UInt32 converted => WriteWithLength(converted, buf, lengthCache, parameter, async, cancellationToken),


            DBNull => WriteWithLength(DBNull.Value, buf, lengthCache, parameter, async, cancellationToken),
            null => WriteWithLength(DBNull.Value, buf, lengthCache, parameter, async, cancellationToken),
            _ => throw new InvalidCastException($"Can't write CLR type {value.GetType()} with handler type UInt32Handler")
        };
}