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

pub struct NaiveDFT {
    direction: FFTDirection,
    size: usize,
    output: Box<[Complex32]>,
}

impl DFTAlgorithm for NaiveDFT {
    fn create(size: usize, direction: FFTDirection) -> Self
    where
        Self: Sized,
    {
        NaiveDFT { direction, size, output: vec![Complex32::zero(); size].into() }
    }

    fn execute(&mut self, input: &[Complex32]) {
        for (freq, out) in self.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,
                        ));
            }
        }
    }

    fn get_output(&self) -> &[Complex32] 
    {
         &self.output
    }
}
