package org.uct.cs.simplify;

import org.apache.commons.cli.*;
import org.uct.cs.simplify.img.BluePrintGenerator;
import org.uct.cs.simplify.ply.header.PLYHeader;
import org.uct.cs.simplify.ply.reader.ImprovedPLYReader;
import org.uct.cs.simplify.util.Timer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class BlueprintifyMain
{
    public static void main(String[] args)
    {
        CommandLine cmd = parseArgs(args);
        try (Timer ignored = new Timer("Elapsed:"))
        {
            // process arguments
            int resolution = (cmd.hasOption("resolution") ? (int) cmd.getParsedOptionValue("resolution") : 1024);
            float alphamod = (cmd.hasOption("alphamod") ? (float) cmd.getParsedOptionValue("alphamod") : 0.1f);
            String filename = cmd.getOptionValue("filename");
            String outputDirectory = cmd.getOptionValue("output");

            // process output dir
            File outputDir = new File(new File(outputDirectory).getCanonicalPath());
            if (!outputDir.exists() && !outputDir.mkdirs())
                throw new IOException("Could not create output directory " + outputDir);

            File inputFile = new File(filename);
            PLYHeader header = new PLYHeader(inputFile);
            ImprovedPLYReader r = new ImprovedPLYReader(header, inputFile);

            // process input model to find output name
            File outputFile = new File(outputDir, inputFile.getName() + ".png");

            BufferedImage bi = BluePrintGenerator.CreateImage(r, resolution, alphamod);
            ImageIO.write(bi, "png", outputFile);

            System.out.println("Saved blueprint to " + outputFile);
        }
        catch (ParseException | IOException e)
        {
            e.printStackTrace();
        }
    }

    private static CommandLine parseArgs(String[] args)
    {
        CommandLineParser clp = new BasicParser();

        Options options = new Options();

        Option o1 = new Option("f", "filename", true, "Path to PLY model to process");
        o1.setRequired(true);
        options.addOption(o1);

        Option o2 = new Option("o", "output", true, "Destination directory of blueprint");
        o2.setRequired(true);
        options.addOption(o2);

        Option o3 = new Option("r", "resolution", true, "Resolution of image to output (default 1024)");
        o3.setType(Short.class);
        options.addOption(o3);

        Option o4 = new Option("a", "alphamod", true, "Translucency of applied pixels (default 0.1f)");
        o4.setType(Float.class);
        options.addOption(o4);

        try
        {
            return clp.parse(options, args);
        }
        catch (ParseException e)
        {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("BlueprintifyPLY --filename <path> --output <path>", options);
            System.exit(1);
            return null;
        }
    }


}

@@ -402,16 +402,66 @@
 yMain%0A%7B%0A
+%0A    private static final String format = %22png%22;%0A%0A
     publ
@@ -1576,20 +1576,26 @@
 e() + %22.
-png%22
+%22 + format
 );%0A%0A    
@@ -1708,21 +1708,22 @@
 ite(bi, 
-%22png%22
+format
 , output

refactor out image format