use super::*;
use crate::globals::get_options;
use anyhow::Result;
use shared::core::sampler::StratifiedSampler;

impl CreateSampler for StratifiedSampler {
    fn create(
        params: &ParameterDictionary,
        _full_res: Point2i,
        _loc: &FileLoc,
        _arena: &Arena,
    ) -> Result<Sampler> {
        let options = get_options();
        let jitter = params.get_one_bool("jitter", true)?;
        let (x_samples, y_samples) = if options.quick_render {
            (1, 1)
        } else if let Some(n) = options.pixel_samples {
            let div = (n as f64).sqrt() as i32;
            let y = (1..=div).rev().find(|d| n % d == 0).unwrap();

            (n / y, y)
        } else {
            (
                params.get_one_int("xsamples", 4)?,
                params.get_one_int("ysamples", 4)?,
            )
        };
        let seed = params.get_one_int("seed", options.seed)?;
        let sampler = Self::new(x_samples, y_samples, Some(seed as u64), jitter);
        // arena.aloc(sampler);

        Ok(Sampler::Stratified(sampler))
    }
}
