use crate::complex::Complex32;
use crate::fft::{DFT, FFTDirection};
use std::f32::consts::PI;

pub struct NaiveDFT {
    direction: FFTDirection,
    size: usize,
}

impl DFT for NaiveDFT {
    fn create(size: usize, direction: FFTDirection) -> Self
    where
        Self: Sized,
    {
        NaiveDFT { direction, size }
    }

    fn execute(&mut self, input: &[Complex32], output: &mut [Complex32], window: fn(f32) -> f32) {
        for (freq, out) in output.iter_mut().enumerate() {
            *out = Complex32::zero();
            for (i, inp) in input.iter().enumerate() {
                *out = *out
                    + ((*inp
                        * Complex32::cexp(
                            -2. * self.direction.sign() * PI * (i * freq) as f32 / self.size as f32,
                        ))
                        * window(i as f32 / self.size as f32));
            }
        }
    }
}
