using System.Threading;
using System.Threading.Tasks;
using Npgsql.BackendMessages;
using Npgsql.Internal.TypeHandling;
using Npgsql.PostgresTypes;
using NpgsqlTypes;

namespace Npgsql.Internal.TypeHandlers.GeometricHandlers;

/// <summary>
/// A type handler for the PostgreSQL polygon data type.
/// </summary>
/// <remarks>
/// See https://www.postgresql.org/docs/current/static/datatype-geometric.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 PolygonHandler : NpgsqlTypeHandler<NpgsqlPolygon>
{
    public PolygonHandler(PostgresType pgType) : base(pgType) {}

    #region Read

    /// <inheritdoc />
    public override async ValueTask<NpgsqlPolygon> Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription? fieldDescription = null)
    {
        await buf.Ensure(4, async);
        var numPoints = buf.ReadInt32();
        var result = new NpgsqlPolygon(numPoints);
        for (var i = 0; i < numPoints; i++)
        {
            await buf.Ensure(16, async);
            result.Add(new NpgsqlPoint(buf.ReadDouble(), buf.ReadDouble()));
        }
        return result;
    }

    #endregion

    #region Write

    /// <inheritdoc />
    public override int ValidateAndGetLength(NpgsqlPolygon value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
        => 4 + value.Count * 16;

    /// <inheritdoc />
    public override async Task Write(NpgsqlPolygon value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async, CancellationToken cancellationToken = default)
    {
        if (buf.WriteSpaceLeft < 4)
            await buf.Flush(async, cancellationToken);
        buf.WriteInt32(value.Count);

        foreach (var p in value)
        {
            if (buf.WriteSpaceLeft < 16)
                await buf.Flush(async, cancellationToken);
            buf.WriteDouble(p.X);
            buf.WriteDouble(p.Y);
        }
    }

    #endregion
}

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

            NpgsqlPolygon converted => ((INpgsqlTypeHandler<NpgsqlPolygon>)this).ValidateAndGetLength(converted, ref lengthCache, parameter),


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

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

            NpgsqlPolygon 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 PolygonHandler")
        };
}