use crate::{
	ray::Ray,
	vec3::{Color, Vec3},
};

use super::Material;

#[derive(Debug, Clone, Copy)]
pub struct Metal {
	albedo: Color,
	fuzz: f64,
}

impl Metal {
	pub fn new(albedo: Color, fuzz: f64) -> Self {
		Self { albedo, fuzz: fuzz.min(1.0) }
	}
}

impl Material for Metal {
	fn scatter(
		&self,
		r_in: &crate::ray::Ray,
		hit_record: &crate::hittable::HitRecord,
	) -> Option<(Color, Ray)> {
		let reflected = r_in.direction().reflect(hit_record.normal).unit_vector()
			+ (self.fuzz * Vec3::random_unit_vector());
		let scattered = Ray::new(hit_record.p, reflected);
		match scattered.direction().dot(&hit_record.normal) > 0.0 {
			true => Some((self.albedo, scattered)),
			false => None,
		}
	}
}
