use shared::Float;
use shared::core::light::LightSampleContext;
use shared::core::sampler::{Sampler, SamplerTrait};
use shared::spectra::SampledSpectrum;

pub struct PathState {
    pub l: SampledSpectrum,
    pub beta: SampledSpectrum,
    pub depth: usize,
    pub specular_bounce: bool,
    pub any_non_specular_bounces: bool,
    pub eta_scale: Float,
    pub prev_ctx: LightSampleContext,
    pub prev_pdf: Float,
}

impl PathState {
    pub fn new() -> Self {
        Self {
            l: SampledSpectrum::new(0.0),
            beta: SampledSpectrum::new(1.0),
            depth: 0,
            specular_bounce: false,
            any_non_specular_bounces: false,
            eta_scale: 1.0,
            prev_ctx: LightSampleContext::default(),
            prev_pdf: 1.0,
        }
    }

    pub fn terminated(&self) -> bool {
        self.beta.is_black()
    }

    /// Termination check
    pub fn russian_roulette(&mut self, sampler: &mut Sampler, min_depth: usize) -> bool {
        let rr_beta = self.beta * self.eta_scale;
        if rr_beta.max_component_value() < 1.0 && self.depth > min_depth {
            let q = (1.0 - rr_beta.max_component_value()).max(0.0);
            if sampler.get1d() < q {
                return true; // terminate
            }
            self.beta /= 1.0 - q;
        }
        false
    }
}
