package ch.tkuhn.memetools;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import org.supercsv.io.CsvListReader;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;

public class RenderGraph {

	@Parameter(description = "input-file", required = true)
	private List<String> parameters = new ArrayList<String>();

	private File inputFile;

	@Parameter(names = "-o", description = "Output file")
	private File outputFile;

	@Parameter(names = "-d", description = "The directory to read the raw data from")
	private File rawWosDataDir;

	@Parameter(names = "-s", description = "Size of output bitmap")
	private int size = 10000;

	@Parameter(names = "-sc", description = "Scaling factor")
	private float scale = 0.5f;

	@Parameter(names = "-ds", description = "Size of dots")
	private int dotSize = 5;

	private File logFile;

	public static final void main(String[] args) {
		RenderGraph obj = new RenderGraph();
		JCommander jc = new JCommander(obj);
		try {
			jc.parse(args);
		} catch (ParameterException ex) {
			jc.usage();
			System.exit(1);
		}
		if (obj.parameters.size() != 1) {
			System.err.println("ERROR: Exactly one main argument is needed");
			jc.usage();
			System.exit(1);
		}
		obj.inputFile = new File(obj.parameters.get(0));
		obj.run();
	}

	private static String wosFolder = "wos";

	private BufferedImage image;

	public RenderGraph() {
	}

	public void run() {
		try {
			init();
			processNodes();
			processEdges();
			writeImage();
		} catch (Throwable th) {
			log(th);
			System.exit(1);
		}
		log("Finished");
	}

	private void init() throws IOException {
		logFile = new File(MemeUtils.getLogDir(), getOutputFileName() + ".log");
		log("==========");
		log("Starting...");

		if (outputFile == null) {
			outputFile = new File(MemeUtils.getOutputDataDir(), getOutputFileName() + ".png");
		}
		if (rawWosDataDir == null) {
			rawWosDataDir = new File(MemeUtils.getRawDataDir(), wosFolder);
		}
		image = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
	}

	private void processNodes() throws IOException {
		log("Reading base points from gext file: " + inputFile);
		image.getGraphics().setColor(new Color(255,0,0,16));
		BufferedReader r = new BufferedReader(new FileReader(inputFile), 64*1024);
		CsvListReader csvReader = new CsvListReader(r, MemeUtils.getCsvPreference());
		List<String> line;
		while ((line = csvReader.read()) != null) {
			float x = Float.parseFloat(line.get(1));
			float y = Float.parseFloat(line.get(2));
			image.getGraphics().fillOval((int) (x*scale - dotSize/2.0), (int) (y*scale - dotSize/2.0), dotSize, dotSize);
		}
		csvReader.close();
	}

	private void processEdges() {
		// TODO
	}

	private void writeImage() throws IOException {
		log("Writing image to" + outputFile + " ...");
		ImageIO.write(image, "png", outputFile);
	}

	private String getOutputFileName() {
		return "im-" + inputFile.getName().replaceAll("\\..*$", "");
	}

	private void log(Object obj) {
		MemeUtils.log(logFile, obj);
	}

}

@@ -45,16 +45,42 @@
 .Color;%0A
+import java.awt.Graphics;%0A
 import j
@@ -108,16 +108,16 @@
 dImage;%0A
-
 import j
@@ -1094,17 +1094,17 @@
 tSize = 
-5
+4
 ;%0A%0A%09priv
@@ -1632,16 +1632,44 @@
 e image;
+%0A%09private Graphics graphics;
 %0A%0A%09publi
@@ -2320,16 +2320,50 @@
 T_RGB);%0A
+%09%09graphics = image.getGraphics();%0A
 %09%7D%0A%0A%09pri
@@ -2419,36 +2419,34 @@
 og(%22
-Reading base point
+Processing node
 s from 
-gex
+inpu
 t fi
@@ -2467,35 +2467,97 @@
 ile);%0A%09%09
-image.getGraphics()
+graphics.setColor(Color.WHITE);%0A%09%09graphics.fillRect(0, 0, size, size);%0A%09%09graphics
 .setColo
@@ -2572,16 +2572,19 @@
 lor(
-255,0,0,
+0, 0, 255, 
 16))
@@ -2900,35 +2900,24 @@
 2));%0A%09%09%09
-image.getG
+g
 raphics
-()
 .fillOva

Fix and improve graph rendering