package cz.vutbr.feec.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.JLabel;

import cz.vutbr.feec.math.GNPConfig;

/**
 * The Class Main. Tato trida je urcena ke spousteni projektu a ke kompozici
 * jejich hlavnich casti.
 */
public class MainWindow {

	/** The config panel. */
	private ConfigPanel configPanel;
	
	/** The config. */
	private GNPConfig config; 
	
	private JButton computeGeneticAlgorithm;
	
	private JButton computeSimplexDownhill;
	
	private JButton generateNewPositions;
	
	private JLabel computingMethodLabel;
	
	private JLabel averageNormErrGA;
	
	private JLabel averageNormErrSD;
	
	private JLabel errorLabel;

	/**
	 * Main method to execute program.
	 * 
	 * @param args
	 *            the args
	 */
	
	public static void main(String[] args) {
		final MainWindow m = new MainWindow();
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame f = m.createGui();
				m.original.readLmsFromConf();
				f.setLocationRelativeTo(null);
				f.setVisible(true);
			}
		});
	}

	///** Mnozina landmarku v ADT seznam. */
	//private final Vector<LandmarkGui> landmarks;

	/** Panel, kde jsou landmarky editovatelne. */
	private final ImageSelectPanelDraggable original;

	/** Panel, kde je spoctena pozice landmarku. */
	private final ImageSelectPanelDraggable computed;

	/** Vychozi velikost panelu (osa x) */
	private static final int SIZE_X = 715;

	/** Vychozi velikost panelu (osa y) */
	private static final int SIZE_Y = 482;

	/**
	 * The Constructor. Jedina cesta jak vytvorit tridu main a s tim i pole
	 * landmarku.
	 */
	public MainWindow() {
		config = new GNPConfig(new Vector<LandmarkGui>());
		config.setNoiseRatio(0.075);
		// http://www.rgagnon.com/javadetails/java-0240.html
		original = new PanelLmEdit(SIZE_X, SIZE_Y, "images/europe.jpg",
				config);
		computed = new PanelLmComputed(SIZE_X, SIZE_Y, "images/europe.jpg",
				config);
	}

	/**
	 * Metoda vytvari kraficke uzivatelske rozhrani.
	 * 
	 * @return JFrame
	 */
	public JFrame createGui() {
		WindowUtilities.setNativeLookAndFeel();
		
		for (int i = 0; i < 31; i++) {
			LandmarkGui lm = new LandmarkGui();
			lm.setFixedPosition(false);
			config.getLandmarks().add(lm);
		}
		
		configPanel = new ConfigPanel(config);
		
		computingMethodLabel = new JLabel("Computing method: ");
		computingMethodLabel.setVisible(false);
		
		errorLabel = new JLabel("Average normalized error(GA, SD): ");
		errorLabel.setVisible(true);
		
		averageNormErrGA = new JLabel("not computed    ");
		averageNormErrGA.setVisible(true);
		
		averageNormErrSD = new JLabel("not computed");
		averageNormErrSD.setVisible(true);
		
		generateNewPositions = new JButton("Generate new positions");
		generateNewPositions.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					original.readLmsFromConf();
					original.repaint();
				} catch (Exception e1) {					
					System.out.println(e1);
				}
				computed.repaint();
			}
		});
		generateNewPositions.setVisible(true);
		
		
		computeGeneticAlgorithm = new JButton("Genetic Algorithm");
		computeGeneticAlgorithm.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					original.setChanged(false);
					computed.repaint();
				//} catch (CostException e1) {	
				} catch (Exception e1) {					
					System.out.println(e1);
				}
				computed.repaint();
			}
		});
		computeGeneticAlgorithm.setVisible(false);

		computeSimplexDownhill = new JButton("Simplex Downhill");
		computeSimplexDownhill.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					original.setChanged(false);
					computed.repaint();
				computed.repaint();
			}
		});
		computeSimplexDownhill.setVisible(false);

/*				for (int i = 0; i < ConfigPanel.getSliderValue(); i++) {
			LandmarkGui lm = new LandmarkGui();
			config.getLandmarks().add(lm);
	
		}
*/		
		computed.draggingEnabled(false);
		original.draggingEnabled(false);

		JTabbedPane tabbedPane = new JTabbedPane();
		tabbedPane.addTab("Original", original);
		tabbedPane.addTab("Computed", computed);
		tabbedPane.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				JTabbedPane p = (JTabbedPane) e.getSource();
				int i = p.getSelectedIndex();
				if (i == 1 || i == 2) {
					configPanel.setEnabled(false);
					configPanel.setVisible(false);
					generateNewPositions.setVisible(false);
					computingMethodLabel.setVisible(true);
					computeGeneticAlgorithm.setVisible(true);
					computeSimplexDownhill.setVisible(true);
					//configPanel.isJSliderChanged() vraci true, pokud byl změněn počet landmarků
					//tím je zajištěno nastavení vypočtených pozic dynamických LM na souřadnice [0,0]
					//bez tohoto dojde k chybě při výpočtu pozice hostů - používá vypočtené pozice z lm
					//pozice host-oblast vyznačená červeným "mrakem"
					if ((i == 1 && original.isChanged()) || (i == 1 && configPanel.isConfigPanelChanged())) {
						try {
							original.setChanged(false);
							configPanel.setConfigPanelChanged(false);
							computed.landmarkPositionInit();
						} catch (Exception e1) {
							System.out.println(e1);
						}
					}
					if (i == 2) {
					//	updateHeatMap(SIZE_X / 2, SIZE_Y / 2);
					}
				} else {
					configPanel.setEnabled(true);
					configPanel.setVisible(true);
					generateNewPositions.setVisible(true);
					computingMethodLabel.setVisible(false);
					computeGeneticAlgorithm.setVisible(false);
					computeSimplexDownhill.setVisible(false);
				}
			}
		});

		JFrame f = new JFrame("Global Network Position");
		f.setResizable(false);
		//URL url = getClass().getResource("/images/fektlogo.jpg");
		f.setIconImage(new ImageIcon("images/fektlogo.jpg").getImage());
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.getContentPane().add(tabbedPane, BorderLayout.NORTH);
	
		JPanel cont = new JPanel();
		cont.setPreferredSize(new Dimension(600, 60));
		cont.add(configPanel);
		cont.add(generateNewPositions);
		cont.add(computingMethodLabel);
		cont.add(computeGeneticAlgorithm);
		cont.add(computeSimplexDownhill);

		JPanel infoPanel = new JPanel();
		infoPanel.setPreferredSize(new Dimension(600, 30));
		infoPanel.add(errorLabel);
		infoPanel.add(averageNormErrGA);
		infoPanel.add(averageNormErrSD);

		f.getContentPane().add(cont, BorderLayout.CENTER);
		f.getContentPane().add(infoPanel, BorderLayout.SOUTH);
		f.pack();
		configPanel.setMapHeight(original.getHeight());
		configPanel.setMapWidth(original.getWidth());
		return f;
	}
	
	public static int width() {
		return SIZE_X;
	}
	
	public static int height() {
		return SIZE_Y;
	}
}
