package eu.jonahbauer.raytracing.render.material;

import eu.jonahbauer.raytracing.math.Ray;
import eu.jonahbauer.raytracing.math.Vec3;
import eu.jonahbauer.raytracing.render.Color;
import eu.jonahbauer.raytracing.scene.HitResult;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;
import java.util.Optional;

public record LambertianMaterial(@NotNull Color albedo) implements Material {
    public LambertianMaterial {
        Objects.requireNonNull(albedo, "albedo");
    }

    @Override
    public @NotNull Optional<ScatterResult> scatter(@NotNull Ray ray, @NotNull HitResult hit) {
        var newDirection = hit.normal().plus(Vec3.random(true));
        if (newDirection.isNearZero()) newDirection = hit.normal();

        var scattered = new Ray(hit.position(), newDirection);
        return Optional.of(new ScatterResult(scattered, albedo));
    }
}
