using Voile.Resources;
using StbImageSharp;
using Voile.VFS;

namespace Voile
{
    /// <summary>
    /// Loads a 2D texture from PNG or JPG files using StbImageSharp.
    /// </summary>
    public class Texture2dLoader : ResourceLoader<Texture2d>
    {
        public override IEnumerable<string> SupportedExtensions => new string[]
        {
            ".png",
            ".jpg",
            ".jpeg"
        };

        protected override Texture2d LoadResource(string path)
        {
            ImageResult image;
            using (var stream = VirtualFileSystem.Read(path))
            {
                image = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha);
            }

            Texture2d result = new(path, image.Data)
            {
                Width = image.Width,
                Height = image.Height
            };

            return result;
        }
    }
}