using System;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace MaddoShared.Benchmarks.Helpers;

/// <summary>
/// Helper class to generate test images for benchmarking
/// </summary>
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
public static class TestImageGenerator
{
    /// <summary>
    /// Generates a set of test JPEG images in the specified directory
    /// </summary>
    /// <param name="outputDirectory">Directory where images will be created</param>
    /// <param name="imageCount">Number of images to generate</param>
    /// <param name="width">Width of each image</param>
    /// <param name="height">Height of each image</param>
    /// <param name="includeSubfolders">Whether to create images in subfolders</param>
    public static void GenerateTestImages(
        string outputDirectory, 
        int imageCount, 
        int width = 4000, 
        int height = 3000,
        bool includeSubfolders = false)
    {
        Directory.CreateDirectory(outputDirectory);

        var random = new Random(42); // Fixed seed for reproducibility

        for (int i = 0; i < imageCount; i++)
        {
            var targetDir = outputDirectory;
            
            if (includeSubfolders && i % 10 == 0)
            {
                targetDir = Path.Combine(outputDirectory, $"Subfolder_{i / 10}");
                Directory.CreateDirectory(targetDir);
            }

            var filePath = Path.Combine(targetDir, $"test_image_{i:D5}.jpg");
            
            // Skip if already exists
            if (File.Exists(filePath))
                continue;

            using var bitmap = new Bitmap(width, height);
            using var graphics = Graphics.FromImage(bitmap);
            
            // Fill with a random color background
            var bgColor = Color.FromArgb(random.Next(256), random.Next(256), random.Next(256));
            graphics.Clear(bgColor);

            // Draw some random shapes to make it more realistic
            for (int j = 0; j < 20; j++)
            {
                var color = Color.FromArgb(random.Next(256), random.Next(256), random.Next(256));
                var brush = new SolidBrush(color);
                var x = random.Next(width);
                var y = random.Next(height);
                var w = random.Next(200, 800);
                var h = random.Next(200, 800);
                graphics.FillEllipse(brush, x, y, w, h);
            }

            // Add some text
            using var font = new Font("Arial", 48, FontStyle.Bold);
            var text = $"Test Image {i}";
            var textBrush = new SolidBrush(Color.White);
            graphics.DrawString(text, font, textBrush, new PointF(100, 100));

            // Save as JPEG with standard quality
            var encoder = GetEncoder(ImageFormat.Jpeg);
            var encoderParameters = new EncoderParameters(1);
            encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 85L);
            
            bitmap.Save(filePath, encoder, encoderParameters);
        }
    }

    /// <summary>
    /// Cleans up generated test images
    /// </summary>
    public static void CleanupTestImages(string directory)
    {
        if (Directory.Exists(directory))
        {
            Directory.Delete(directory, recursive: true);
        }
    }

    private static ImageCodecInfo GetEncoder(ImageFormat format)
    {
        var codecs = ImageCodecInfo.GetImageEncoders();
        foreach (var codec in codecs)
        {
            if (codec.FormatID == format.Guid)
            {
                return codec;
            }
        }
        return null;
    }
}
