package eu.jonahbauer.raytracing.render.canvas;

import eu.jonahbauer.raytracing.render.texture.Color;
import eu.jonahbauer.raytracing.render.ImageFormat;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;

import static org.junit.jupiter.api.Assertions.assertEquals;

class ImageTest {

    @Test
    void test(@TempDir Path dir) throws IOException {
        var image = new Image(256, 256);

        for (var y = 0; y < image.getHeight(); y++) {
            for (var x = 0; x < image.getWidth(); x++) {
                var r = (double) x / (image.getWidth() - 1);
                var g = (double) y / (image.getHeight() - 1);
                var b = 0;

                image.set(x, y, new Color(r, g, b));
            }
        }

        System.out.println(dir);
        ImageFormat.PPM.write(image, dir.resolve("img.ppm"));

        String expected;
        String actual;

        try (var in = Objects.requireNonNull(ImageTest.class.getResourceAsStream("simple_image.ppm"))) {
            expected = new String(in.readAllBytes(), StandardCharsets.US_ASCII);
        }

        try (var in = Files.newInputStream(dir.resolve("img.ppm"))) {
            actual = new String(in.readAllBytes(), StandardCharsets.US_ASCII);
        }

        assertEquals(expected, actual);
    }

}