import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

// A line chart, written as an SVG file. An SVG is a text file naming shapes
// --- a line from here to there, a word at this point --- so drawing one is
// string building, the same work as a toString, and a browser does the rest.
// Growth and Steps call it to draw their tables. It is not the day's topic;
// it is here to show that a useful picture is a program's output too.
public class Chart {
    static final int WIDTH = 480, HEIGHT = 320;                       // the whole picture
    static final int LEFT = 70, RIGHT = 100, TOP = 50, BOTTOM = 60;   // room for the labels

    // writes one chart: a title, the x values, and one line per series, named
    public static void write(Path file, String title, int[] xs, double[][] series, String[] names)
            throws IOException {
        int plotWidth = WIDTH - LEFT - RIGHT;
        int plotHeight = HEIGHT - TOP - BOTTOM;
        double xMax = xs[xs.length - 1];
        double yMax = 0;
        for (double[] ys : series) {
            for (double y : ys) {
                yMax = Math.max(yMax, y);
            }
        }
        StringBuilder svg = new StringBuilder();
        svg.append("<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"" + WIDTH + "\" height=\"" + HEIGHT
                   + "\" font-family=\"sans-serif\" font-size=\"13\">\n");
        svg.append("<rect width=\"" + WIDTH + "\" height=\"" + HEIGHT + "\" fill=\"white\"/>\n");
        svg.append(text(WIDTH / 2.0, 24, "middle", "bold", "#000", title));
        // the two axes, each with its lowest, middle and highest value
        svg.append(line(LEFT, TOP, LEFT, TOP + plotHeight));
        svg.append(line(LEFT, TOP + plotHeight, LEFT + plotWidth, TOP + plotHeight));
        for (int k = 0; k <= 2; k++) {
            svg.append(text(LEFT + plotWidth * k / 2.0, TOP + plotHeight + 20, "middle", "normal", "#444",
                            label(xMax * k / 2)));
            svg.append(text(LEFT - 8, TOP + plotHeight - plotHeight * k / 2.0 + 4, "end", "normal", "#444",
                            label(yMax * k / 2)));
        }
        // one line per series, with its name at the right end
        double previousLabelY = Double.MAX_VALUE;
        for (int s = 0; s < series.length; s++) {
            StringBuilder points = new StringBuilder();
            for (int i = 0; i < xs.length; i++) {
                double px = LEFT + xs[i] / xMax * plotWidth;
                double py = TOP + plotHeight - series[s][i] / yMax * plotHeight;
                points.append(String.format("%.1f,%.1f ", px, py));
            }
            String colour = colourOf(names[s]);
            svg.append("<polyline points=\"" + points + "\" fill=\"none\" stroke=\"" + colour
                       + "\" stroke-width=\"2.5\"/>\n");
            double labelY = TOP + plotHeight - series[s][xs.length - 1] / yMax * plotHeight + 4;
            if (Math.abs(previousLabelY - labelY) < 15) {   // two names at the same height: lift this one
                labelY = previousLabelY - 15;
            }
            svg.append(text(LEFT + plotWidth + 8, labelY, "start", "bold", colour, names[s]));
            previousLabelY = labelY;
        }
        svg.append("</svg>\n");
        Files.writeString(file, svg.toString());
    }

    static String line(double x1, double y1, double x2, double y2) {
        return String.format("<line x1=\"%.1f\" y1=\"%.1f\" x2=\"%.1f\" y2=\"%.1f\" stroke=\"#444\"/>%n",
                             x1, y1, x2, y2);
    }

    static String text(double x, double y, String anchor, String weight, String colour, String words) {
        return String.format("<text x=\"%.1f\" y=\"%.1f\" text-anchor=\"%s\" font-weight=\"%s\" fill=\"%s\">%s</text>%n",
                             x, y, anchor, weight, colour, words);
    }

    // 400000 as 400k, 1212.8 as 1213, 28.5 as 28.5, 0 as 0
    static String label(double v) {
        if (v >= 10_000) {
            return String.format("%.0fk", v / 1000);
        }
        String s = String.format(v >= 100 ? "%.0f" : "%.1f", v);
        return s.endsWith(".0") ? s.substring(0, s.length() - 2) : s;
    }

    // the same colour for the same collection on every chart
    static String colourOf(String name) {
        if (name.equals("hash set")) {
            return "#c47a16";
        }
        if (name.equals("tree set")) {
            return "#3c6e71";
        }
        return "#1f1f1f";
    }
}
