package eu.jonahbauer.raytracing.scene.util;

import eu.jonahbauer.raytracing.math.AABB;
import eu.jonahbauer.raytracing.math.Ray;
import eu.jonahbauer.raytracing.scene.Hittable;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public final class HittableList extends HittableCollection {
    private final @NotNull List<Hittable> objects;
    private final @NotNull AABB bbox;

    public HittableList(@NotNull List<? extends @NotNull Hittable> objects) {
        this.objects = new ArrayList<>(objects);
        this.bbox = AABB.getBoundingBox(this.objects).orElse(AABB.EMPTY);
    }

    public HittableList(@NotNull Hittable @NotNull... objects) {
        this(List.of(objects));
    }

    @Override
    public void hit(@NotNull Ray ray, @NotNull State state) {
        objects.forEach(object -> hit(state, ray, object));
    }

    @Override
    public @NotNull AABB getBoundingBox() {
        return bbox;
    }
}
