package org.concord.energy3d.simulation;

import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.swing.JButton;

import org.concord.energy3d.gui.EnergyPanel;
import org.concord.energy3d.gui.MainPanel;
import org.concord.energy3d.model.Building;
import org.concord.energy3d.model.Foundation;
import org.concord.energy3d.model.HousePart;
import org.concord.energy3d.model.Wall;
import org.concord.energy3d.scene.Scene;
import org.concord.energy3d.scene.SceneManager;
import org.concord.energy3d.util.Util;

/**
 * @author Charles Xie
 * 
 */
public abstract class Analysis {

	Graph graph;
	volatile boolean analysisStopped;
	static Point windowLocation = new Point();
	JButton runButton;

	public double getResult(final String name) {
		return graph.getSum(name);
	}

	public Map<String, Double> getRecordedResults(final String name) {
		Map<String, Double> recordedResults = new TreeMap<String, Double>();
		for (Results r : graph.getRecords()) {
			Map<String, List<Double>> x = r.getData();
			List<Double> list = x.get(name);
			if (list != null) {
				double sum = 0;
				for (Double d : list) {
					sum += d;
				}
				recordedResults.put(r.getID() + (r.getFileName() == null ? "" : " (file: " + r.getFileName() + ")"), sum);
			}
		}
		return recordedResults;
	}

	public int getNumberOfDataPoints() {
		return graph.getLength();
	}

	void stopAnalysis() {
		analysisStopped = true;
	}

	void runAnalysis(final Runnable task) {
		onStart();
		new Thread(task, getClass().getName()).start();
	}

	// return the exception if unsuccessful
	Throwable compute() {
		try {
			EnergyPanel.getInstance().computeNow();
		} catch (final Throwable e) {
			e.printStackTrace();
			return e;
		}
		try {
			SceneManager.getInstance().refreshNow();
		} catch (final Exception e) {
			e.printStackTrace();
		}
		updateGraph();
		Scene.getInstance().redrawAll();
		return null;
	}

	abstract void updateGraph();

	void onCompletion() {
		EnergyPanel.getInstance().requestDisableActions(null);
		EnergyPanel.getInstance().progress(0);
		runButton.setEnabled(true);
	}

	private void onStart() {
		EnergyPanel.getInstance().requestDisableActions(this);
		Util.selectSilently(MainPanel.getInstance().getSolarButton(), true);
		SceneManager.getInstance().setSolarColorMapWithoutUpdate(true);
		SceneManager.getInstance().setHeatFlowArrows(true);
		graph.clearData();
		SceneManager.getInstance().getSolarLand().setVisible(true);
	}

	public static boolean isBuildingComplete(final Foundation foundation) {
		final Building b = new Building((int) foundation.getId());
		final ArrayList<HousePart> children = foundation.getChildren();
		for (final HousePart x : children) {
			if (x instanceof Wall)
				b.addWall((Wall) x);
		}
		if (!b.isWallComplete())
			return false;
		return b.getRoof() != null;
	}

}

@@ -608,17 +608,16 @@
 s Xie%0A *
- 
 %0A */%0Apub
@@ -910,16 +910,22 @@
 me) %7B%0A%09%09
+final 
 Map%3CStri
@@ -992,16 +992,22 @@
 %0A%09%09for (
+final 
 Results 
@@ -1035,16 +1035,22 @@
 )) %7B%0A%09%09%09
+final 
 Map%3CStri
@@ -1087,16 +1087,22 @@
 a();%0A%09%09%09
+final 
 List%3CDou
@@ -1169,16 +1169,16 @@
 um = 0;%0A
-
 %09%09%09%09for 
@@ -1178,16 +1178,22 @@
 %09%09%09for (
+final 
 Double d
@@ -1957,16 +1957,59 @@
 wAll();%0A
+%09%09SceneManager.getInstance().refreshNow();%0A
 %09%09return

Fix problem with simoulation not drawing heatflow arrows the first run