package eu.jonahbauer.raytracing.scene;

import eu.jonahbauer.raytracing.math.AABB;
import eu.jonahbauer.raytracing.math.Ray;
import eu.jonahbauer.raytracing.render.Color;
import eu.jonahbauer.raytracing.scene.util.HittableBinaryTree;
import eu.jonahbauer.raytracing.scene.util.HittableCollection;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Objects;

public final class Scene extends HittableCollection {
    private final @NotNull HittableCollection objects;
    private final @NotNull SkyBox background;

    public Scene(@NotNull List<? extends @NotNull Hittable> objects) {
        this(Color.BLACK, objects);
    }

    public Scene(@NotNull Color background, @NotNull List<? extends @NotNull Hittable> objects) {
        this(SkyBox.solid(background), objects);
    }

    public Scene(@NotNull SkyBox background, @NotNull List<? extends @NotNull Hittable> objects) {
        this.objects = new HittableBinaryTree(objects);
        this.background = Objects.requireNonNull(background);
    }

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

    public Scene(@NotNull Color background, @NotNull Hittable @NotNull... objects) {
        this(background, List.of(objects));
    }

    public Scene(@NotNull SkyBox background, @NotNull Hittable @NotNull... objects) {
        this(background, List.of(objects));
    }

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

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

    public @NotNull Color getBackgroundColor(@NotNull Ray ray) {
        return background.getColor(ray);
    }
}
