using System.Windows;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using ImageProcessing.Core;
using Moq;
using XplorePlane.Services;
using System;

namespace XplorePlane.Tests.Helpers
{
    /// <summary>
    /// 测试辅助工具：创建 Mock 和测试用图像
    /// </summary>
    internal static class TestHelpers
    {
        public static readonly string[] DefaultKeys = { "Blur", "Sharpen", "Threshold" };

        /// <summary>
        /// 创建一个 1x1 像素的 BitmapSource，用于测试
        /// </summary>
        public static BitmapSource CreateTestBitmap(int width = 4, int height = 4)
        {
            var pixels = new byte[width * height * 4];
            var bitmap = BitmapSource.Create(
                width, height, 96, 96,
                PixelFormats.Bgra32, null,
                pixels, width * 4);
            bitmap.Freeze();
            return bitmap;
        }

        /// <summary>
        /// 创建一个超大图像（用于测试预览缩放）
        /// </summary>
        public static BitmapSource CreateLargeBitmap(int width = 4096, int height = 4096)
            => CreateTestBitmap(width, height);

        /// <summary>
        /// 创建标准 Mock IImageProcessingService，注册 DefaultKeys 中的算子
        /// </summary>
        public static Mock<IImageProcessingService> CreateMockImageService(
            string[]? keys = null,
            ProcessorParameter[]? parameters = null)
        {
            keys ??= DefaultKeys;
            parameters ??= Array.Empty<ProcessorParameter>();

            var mock = new Mock<IImageProcessingService>();
            mock.Setup(s => s.GetAvailableProcessors()).Returns(keys);
            mock.Setup(s => s.GetProcessorDisplayName(It.IsAny<string>()))
                .Returns<string>(k => k + "算子");
            mock.Setup(s => s.GetProcessorParameters(It.IsAny<string>()))
                .Returns(parameters);
            mock.Setup(s => s.ProcessImageAsync(
                    It.IsAny<BitmapSource>(),
                    It.IsAny<string>(),
                    It.IsAny<IDictionary<string, object>>(),
                    It.IsAny<IProgress<double>>(),
                    It.IsAny<CancellationToken>()))
                .ReturnsAsync((BitmapSource src, string _, IDictionary<string, object> _, IProgress<double> _, CancellationToken ct) =>
                {
                    ct.ThrowIfCancellationRequested();
                    return src;
                });
            return mock;
        }

        /// <summary>
        /// 创建带数值参数的 ProcessorParameter
        /// </summary>
        public static ProcessorParameter MakeIntParam(string name, int value = 5, int min = 0, int max = 10)
            => new ProcessorParameter(name, name, typeof(int), value, min, max);
    }
}
