use crate::Arena;
use crate::core::sampler::CreateSampler;
use crate::utils::{FileLoc, ParameterDictionary};
use shared::core::geometry::Point2i;
use shared::core::options::get_options;
use shared::core::sampler::{HaltonSampler, RandomizeStrategy};
use std::fmt::Error;

impl CreateSampler for HaltonSampler {
    fn create(
        params: &ParameterDictionary,
        full_res: Point2i,
        loc: &FileLoc,
        arena: &mut Arena,
    ) -> Result<Self, Error> {
        let options = get_options();
        let nsamp = options
            .quick_render
            .then_some(1)
            .or(options.pixel_samples)
            .unwrap_or_else(|| params.get_one_int("pixelsamples", 16));
        let seed = params.get_one_int("seed", options.seed);
        let s = match params
            .get_one_string("randomization", "permutedigits")
            .as_str()
        {
            "none" => RandomizeStrategy::None,
            "permutedigits" => RandomizeStrategy::PermuteDigits,
            "fastowen" => RandomizeStrategy::FastOwen,
            "owen" => RandomizeStrategy::Owen,
            _ => {
                return Err(format!(
                    "{}: Unknown randomization strategy for Halton",
                    loc
                ));
            }
        };

        let sampler = HaltonSampler::new(nsamp as u32, full_res, s, seed as u64);
        arena.alloc(sampler);
        Ok(sampler)
    }
}
