const std = @import("std");
const ray = @import("./ray.zig");
const hittable = @import("./hittable.zig");

const info = std.log.info;
const ArrayList = std.ArrayList;
const Ray = ray.Ray;
const HitRecord = hittable.HitRecord;
const Hittable = hittable.Hittable;

pub const HittableList = struct {
    objects: ArrayList(Hittable),

    pub fn init() HittableList {
        const allocator = std.heap.page_allocator;

        return HittableList{
            .objects = ArrayList(Hittable).init(allocator),
        };
    }
    pub fn hit(hittable_list: HittableList, r: Ray, t_min: f32, t_max: f32) ?HitRecord {
        var temp_rec: ?HitRecord = null;
        var closest_so_far = t_max;
        for (hittable_list.objects.items) |object| {
            if (object.hit(r, t_min, closest_so_far)) |rec| {
                temp_rec = rec;
                closest_so_far = rec.t;
            }
        }
        return temp_rec;
    }
    pub fn add(hittable_list: *HittableList, object: Hittable) anyerror!void {
        try hittable_list.objects.append(object);
    }
};
