use super::*;

#[derive(Debug)]
pub(super) struct SpectrumScaleTexture2d {
    pub(super) tex: Either<Color, Arc<dyn Pbrt2dSpectrumTexture>>,
    pub(super) scale: Either<Float, Arc<dyn Pbrt2dFloatTexture>>,
}

impl PbrtTexture for SpectrumScaleTexture2d {
    fn get_dimension(&self) -> TextureDimension {
        TextureDimension::D2
    }

    fn get_2d_float_texture(self: Arc<Self>) -> Result<Arc<dyn Pbrt2dFloatTexture>> {
        Err(miette!("Unable to convert to float texture"))
    }

    fn get_2d_spectrum_texture(self: Arc<Self>) -> Result<Arc<dyn Pbrt2dSpectrumTexture>> {
        Ok(self)
    }
}

impl Pbrt2dSpectrumTexture for SpectrumScaleTexture2d {
    fn get(&self, u: Float, v: Float) -> Color {
        let x = match &self.tex {
            Either::A(x) => *x,
            Either::B(t) => t.get(u, v),
        };

        let s = match &self.scale {
            Either::A(s) => *s,
            Either::B(t) => t.get(u, v),
        };

        x * s
    }
}
