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

namespace Npgsql.Internal.TypeHandlers.InternalTypeHandlers;

/// <summary>
/// A type handler for the PostgreSQL "char" type, used only internally.
/// </summary>
/// <remarks>
/// See https://www.postgresql.org/docs/current/static/datatype-character.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 InternalCharHandler : NpgsqlSimpleTypeHandler<char>,
    INpgsqlSimpleTypeHandler<byte>, INpgsqlSimpleTypeHandler<short>, INpgsqlSimpleTypeHandler<int>, INpgsqlSimpleTypeHandler<long>
{
    public InternalCharHandler(PostgresType pgType) : base(pgType) {}

    #region Read

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

    byte INpgsqlSimpleTypeHandler<byte>.Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription)
        => buf.ReadByte();

    short INpgsqlSimpleTypeHandler<short>.Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription)
        => buf.ReadByte();

    int INpgsqlSimpleTypeHandler<int>.Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription)
        => buf.ReadByte();

    long INpgsqlSimpleTypeHandler<long>.Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription)
        => buf.ReadByte();

    #endregion

    #region Write

    /// <inheritdoc />
    public int ValidateAndGetLength(byte value, NpgsqlParameter? parameter)          => 1;

    /// <inheritdoc />
    public override int ValidateAndGetLength(char value, NpgsqlParameter? parameter)
    {
        _ = checked((byte)value);
        return 1;
    }

    /// <inheritdoc />
    public int ValidateAndGetLength(short value, NpgsqlParameter? parameter)
    {
        _ = checked((byte)value);
        return 1;
    }

    /// <inheritdoc />
    public int ValidateAndGetLength(int value, NpgsqlParameter? parameter)
    {
        _ = checked((byte)value);
        return 1;
    }

    /// <inheritdoc />
    public int ValidateAndGetLength(long value, NpgsqlParameter? parameter)
    {
        _ = checked((byte)value);
        return 1;
    }

    /// <inheritdoc />
    public override void Write(char value, NpgsqlWriteBuffer buf, NpgsqlParameter? parameter) => buf.WriteByte((byte)value);
    /// <inheritdoc />
    public void Write(byte value, NpgsqlWriteBuffer buf, NpgsqlParameter? parameter) => buf.WriteByte(value);
    /// <inheritdoc />
    public void Write(short value, NpgsqlWriteBuffer buf, NpgsqlParameter? parameter) => buf.WriteByte((byte)value);
    /// <inheritdoc />
    public void Write(int value, NpgsqlWriteBuffer buf, NpgsqlParameter? parameter) => buf.WriteByte((byte)value);
    /// <inheritdoc />
    public void Write(long value, NpgsqlWriteBuffer buf, NpgsqlParameter? parameter) => buf.WriteByte((byte)value);

    #endregion
}

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

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

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

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

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

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


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

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

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

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

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

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

            Int64 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 InternalCharHandler")
        };
}