mod gaussian;
mod simd_gaussian;

#[cfg(feature = "unsafe-simd")]
mod unsafe_simd_gaussian;

use crate::SimdImpl;
use gaussian::RecursiveGaussian;
use simd_gaussian::SimdGaussian;

#[cfg(feature = "unsafe-simd")]
use unsafe_simd_gaussian::UnsafeSimdGaussian;

/// Structure handling image blur with selectable implementation.
///
/// Supports runtime switching between:
/// - Scalar: f64 IIR baseline (most accurate)
/// - SIMD: Safe SIMD via wide crate
/// - UnsafeSimd: Raw x86 intrinsics (fastest)
pub struct Blur {
    width: usize,
    height: usize,
    impl_type: SimdImpl,
    // Scalar backend
    scalar_kernel: RecursiveGaussian,
    scalar_temp: Vec<f32>,
    // Safe SIMD backend
    simd: SimdGaussian,
    // Unsafe SIMD backend
    #[cfg(feature = "unsafe-simd")]
    unsafe_simd: UnsafeSimdGaussian,
}

impl Blur {
    /// Create a new [Blur] with the default implementation (SIMD).
    #[must_use]
    pub fn new(width: usize, height: usize) -> Self {
        Self::with_simd_impl(width, height, SimdImpl::default())
    }

    /// Create a new [Blur] with a specific implementation.
    #[must_use]
    pub fn with_simd_impl(width: usize, height: usize, impl_type: SimdImpl) -> Self {
        Blur {
            width,
            height,
            impl_type,
            scalar_kernel: RecursiveGaussian,
            scalar_temp: vec![0.0f32; width * height],
            simd: SimdGaussian::new(width),
            #[cfg(feature = "unsafe-simd")]
            unsafe_simd: UnsafeSimdGaussian::new(width),
        }
    }

    /// Get the current implementation type.
    pub fn impl_type(&self) -> SimdImpl {
        self.impl_type
    }

    /// Set the implementation type.
    pub fn set_impl(&mut self, impl_type: SimdImpl) {
        self.impl_type = impl_type;
    }

    /// Truncates the internal buffers to fit images of the given width and height.
    pub fn shrink_to(&mut self, width: usize, height: usize) {
        self.scalar_temp.truncate(width * height);
        self.simd.shrink_to(width, height);
        #[cfg(feature = "unsafe-simd")]
        self.unsafe_simd.shrink_to(width, height);
        self.width = width;
        self.height = height;
    }

    /// Blur the given image using the selected implementation.
    pub fn blur(&mut self, img: &[Vec<f32>; 3]) -> [Vec<f32>; 3] {
        [
            self.blur_plane(&img[0]),
            self.blur_plane(&img[1]),
            self.blur_plane(&img[2]),
        ]
    }

    /// Blur the given image into pre-allocated output buffers (zero-allocation).
    pub fn blur_into(&mut self, img: &[Vec<f32>; 3], out: &mut [Vec<f32>; 3]) {
        self.blur_plane_into(&img[0], &mut out[0]);
        self.blur_plane_into(&img[1], &mut out[1]);
        self.blur_plane_into(&img[2], &mut out[2]);
    }

    fn blur_plane(&mut self, plane: &[f32]) -> Vec<f32> {
        let mut out = vec![0f32; self.width * self.height];
        self.blur_plane_into(plane, &mut out);
        out
    }

    fn blur_plane_into(&mut self, plane: &[f32], out: &mut [f32]) {
        match self.impl_type {
            SimdImpl::Scalar => self.blur_plane_scalar_into(plane, out),
            SimdImpl::Simd => self.blur_plane_simd_into(plane, out),
            #[cfg(feature = "unsafe-simd")]
            SimdImpl::UnsafeSimd => self.blur_plane_unsafe_simd_into(plane, out),
        }
    }

    fn blur_plane_scalar_into(&mut self, plane: &[f32], out: &mut [f32]) {
        self.scalar_kernel
            .horizontal_pass(plane, &mut self.scalar_temp, self.width);
        self.scalar_kernel.vertical_pass_chunked::<128, 32>(
            &self.scalar_temp,
            out,
            self.width,
            self.height,
        );
    }

    fn blur_plane_simd_into(&mut self, plane: &[f32], out: &mut [f32]) {
        self.simd
            .blur_single_plane_into(plane, out, self.width, self.height);
    }

    #[cfg(feature = "unsafe-simd")]
    fn blur_plane_unsafe_simd_into(&mut self, plane: &[f32], out: &mut [f32]) {
        self.unsafe_simd
            .blur_single_plane_into(plane, out, self.width, self.height);
    }
}
