package eu.jonahbauer.raytracing.render.canvas;

import eu.jonahbauer.raytracing.render.Color;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

public final class Image implements Canvas {
    private final int width;
    private final int height;

    private final Color[][] data;

    public Image(int width, int height) {
        this.width = width;
        this.height = height;

        if (width <= 0) throw new IllegalArgumentException("width must be positive");
        if (height <= 0) throw new IllegalArgumentException("height must be positive");

        this.data = new Color[height][width];
    }

    @Override
    public int getWidth() {
        return width;
    }

    @Override
    public int getHeight() {
        return height;
    }

    @Override
    public @NotNull Color get(int x, int y) {
        Objects.checkIndex(x, width);
        Objects.checkIndex(y, height);
        return Objects.requireNonNullElse(this.data[y][x], Color.BLACK);
    }

    @Override
    public void set(int x, int y, @NotNull Color color) {
        Objects.checkIndex(x, width);
        Objects.checkIndex(y, height);
        this.data[y][x] = Objects.requireNonNull(color);
    }
}
