package eu.jonahbauer.raytracing.scene.transform;

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

public final class Translate extends Transform {
    private final @NotNull Vec3 offset;
    private final @NotNull AABB bbox;

    public Translate(@NotNull Hittable object, @NotNull Vec3 offset) {
        super(object);
        this.offset = offset;

        var bbox = object.getBoundingBox();
        this.bbox = new AABB(
                bbox.min().plus(offset),
                bbox.max().plus(offset)
        );
    }

    @Override
    protected @NotNull Ray transform(@NotNull Ray ray) {
        return new Ray(ray.origin().minus(offset), ray.direction());
    }

    @Override
    protected @NotNull HitResult transform(@NotNull HitResult result) {
        return new HitResult(
                result.t(),
                result.position().plus(offset),
                result.normal(),
                result.material(),
                result.frontFace()
        );
    }

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