/*
 * Copyright 2012 s_wolff.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.aegee.runanddine.pathfinder.optimizers;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import org.aegee.runanddine.RunAndDineApplication;

/**
 * Sets up and utilizes GLPK for path optimization
 * 
 * @author s_wolff
 */
public class GLPK
{

	public static synchronized File writeGLPKProgram(int n, double walkingDistances[][],
		Map<Integer, Integer> predefinedMappings) throws IOException
	{
		assert n % 3 == 0;
		assert predefinedMappings.size() == n / 3;
		assert walkingDistances.length == n;
		assert walkingDistances[0].length == n;

		InputStream inputStream = PathOptimizer.class.getResourceAsStream("GLPK_template.mod");
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

		StringBuilder fileDataBuilder = new StringBuilder();
		String read;
		while ((read = reader.readLine()) != null)
		{
			fileDataBuilder.append(read);
			fileDataBuilder.append("\n");
		}
		String fileDate = fileDataBuilder.toString();

		// set n and k
		fileDate = fileDate.replace("INSERT_N", "" + n);
		fileDate = fileDate.replace("INSERT_K", "" + n / 3);

		// format pre made groups
		String pre_abs = "";
		String pre_opt = "";
		int counter = 1;
		for (int key : predefinedMappings.keySet())
		{
			pre_abs += "          " + counter + "  " + (key + 1) + "\n";
			pre_opt += "          " + counter + "  " + (predefinedMappings.get(key) + 1) + "\n";
			counter++;
		}

		// set predefined mappings
		fileDate = fileDate.replace("INSERT_PRE_ABS", pre_abs.trim());
		fileDate = fileDate.replace("INSERT_PRE_OPT", pre_opt.trim());

		// create wd table
		String wd = "";
		for (int i = 1; i <= n; i++)
		{
			wd += i + "   ";
		}
		wd += ":=\n";
		for (int i = 0; i < walkingDistances.length; i++)
		{
			wd += "" + (i + 1);
			for (int j = 0; j < walkingDistances[i].length; j++)
			{
				wd += "   " + walkingDistances[i][j];
			}
			wd += "\n";
		}
		fileDate = fileDate.replace("INSERT_WD", wd);

		File file = new File(System.getProperty("user.home"), "problem.mod");
		file.delete();
		file.createNewFile();
		FileWriter fileWriter = new FileWriter(file);
		fileWriter.append(fileDate);
		fileWriter.flush();
		fileWriter.close();

		return file;
	}

	public static synchronized Map<Integer, Integer> invokeGLPK(File f) throws IOException
	{
		// TODO: document that glpsol must be found there
		String cmd = String.format("%s -m %s", RunAndDineApplication.GLPK_PATH, f.getAbsolutePath());
		Runtime r = Runtime.getRuntime();

		Process p = r.exec(cmd);
		InputStream is = p.getInputStream();
		BufferedInputStream fis = new BufferedInputStream(is);
		InputStreamReader isr = new InputStreamReader(fis);
		BufferedReader br = new BufferedReader(isr);

		String output = "";
		String s;
		while ((s = br.readLine()) != null)
		{
			output += s + "\n";
		}

		return parseGLPKOutput(output);
	}

	private static Map<Integer, Integer> parseGLPKOutput(String output)
	{
		// RunAndDineApplication.debug(output);
		String lines[] = output.split("\n");
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(lines.length);
		for (String line : lines)
		{
			if (line.startsWith("--x--") && line.endsWith("--1"))
			{
				line = line.replaceFirst("--x--", "");
				line = line.substring(0, line.length() - 3);
				String cord[] = line.split(":");
				int i = new Integer(cord[0]);
				int j = new Integer(cord[1]);
				map.put(i - 1, j - 1);
			}
		}
		return map;
	}
}
