using Engine.Asset;
using Engine.Graphics.Pixel;
using OpenTK.Mathematics;

namespace Engine.Graphics.Texture;

public interface IConstTexture
{
  public Vector2i Size { get; }
  public int Width { get; }
  public int Height { get; }

  public void ReadPixels<T>(int parX, int parY, int parWidth, int parHeight, T[,] parPixels)
    where T : struct, IPixel;
}

public static class ConstTextureExtensions
{
  public static T[,] ReadPixels<T>(this IConstTexture parTexture) where T : struct, IPixel
  {
    return parTexture.ReadPixels<T>(0, 0, parTexture.Width, parTexture.Height);
  }

  public static T[,] ReadPixels<T>(this IConstTexture parTexture, int parX, int parY, int parWidth, int parHeight)
    where T : struct, IPixel
  {
    var pixels = new T[parWidth, parHeight];
    parTexture.ReadPixels(parX, parY, parWidth, parHeight, pixels);

    return pixels;
  }

  public static void ReadPixels<T>(this IConstTexture parTexture, Image<T> parImage) where T : struct, IPixel
  {
    parTexture.ReadPixels(0, 0, parImage);
  }

  public static void ReadPixels<T>(this IConstTexture parTexture, int parX, int parY, Image<T> parImage)
    where T : struct, IPixel
  {
    parTexture.ReadPixels(parX, parY, parImage.Width, parImage.Height, parImage.Pixels);
  }

  public static void ReadPixels<T>(this IConstTexture parTexture, T[,] parPixels) where T : struct, IPixel
  {
    parTexture.ReadPixels(0, 0, parTexture.Width, parTexture.Height, parPixels);
  }
}