import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class TestGen {
    private static final int R = 6371;
    private static final Random rand = new Random("Where there is love there is life.".hashCode());

    private static int testCounter = 0;

    class Point {
        int x, y, z;

        public Point(int x, int y, int z) {
            this.x = x;
            this.y = y;
            this.z = z;
        }

        @Override
        public String toString() {
            return String.format("%d %d %d", x, y, z);
        }
    }

    List<Point> pointsOnEarth;

    void genPointsOnEarth() {
        pointsOnEarth = new ArrayList<>();
        for (int x = -R; x <= R; x++) {
            for (int y = -R; y <= R; y++) {
                if (x == 0 && y == 0) {
                    // skip north and south poles
                    continue;
                }
                int z = R * R - x * x - y * y;
                if (z < 0) {
                    continue;
                }
                z = (int) Math.floor(Math.sqrt(z));
                if (x * x + y * y + z * z == R * R) {
                    pointsOnEarth.add(new Point(x, y, -z));
                    if (z != 0) {
                        pointsOnEarth.add(new Point(x, y, z));
                    }
                }
            }
        }
        Collections.shuffle(pointsOnEarth, rand);
    }

    void testNorthPole() {
        writeTest(new Point(0, 0, R));
    }

    void testEquator() {
        writeTests(pointsOnEarth.stream().filter(p -> p.z == 0).limit(10).collect(Collectors.toList()));
    }

    void testAboveEquator() {
        writeTests(pointsOnEarth.stream().filter(p -> p.z > 0).limit(10).collect(Collectors.toList()));
    }

    void testBelowEquator() {
        writeTests(pointsOnEarth.stream().filter(p -> p.z < 0).limit(10).collect(Collectors.toList()));
    }

    void testBigZ() {
        writeTests(pointsOnEarth.stream().filter(p -> R - Math.abs(p.z) <= 20).limit(10).collect(Collectors.toList()));
    }

    void testBigX() {
        writeTests(pointsOnEarth.stream().filter(p -> R - Math.abs(p.x) <= 20).limit(10).collect(Collectors.toList()));
    }

    void testBigY() {
        writeTests(pointsOnEarth.stream().filter(p -> R - Math.abs(p.y) <= 20).limit(10).collect(Collectors.toList()));
    }

    void writeTests(List<Point> points) {
        for (Point point : points) {
            writeTest(point);
        }
    }

    void writeTest(Point p) {
        PrintWriter out;
        try {
            File file = new File(String.format("../tests/%02d", ++testCounter));
            file.getParentFile().mkdirs();
            out = new PrintWriter(file);
            System.out.println(testCounter);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        out.println(p.toString());
        out.close();
    }

    void run() {
        genPointsOnEarth();
        testNorthPole();
        testEquator();
        testAboveEquator();
        testBelowEquator();
        testBigZ();
        testBigX();
        testBigY();
    }

    public static void main(String[] args) {
        new TestGen().run();
    }
}
