/*
   Main frame of Dynamic TSP
*/

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

public class DTSP extends Frame {    
    MenuBar menuBalk;
	Menu bestandMenu;
	MenuItem exitMenuItem;
	Button leesButton;
	Button pauzeButton;
	Button mapButton;
	TextArea invoerArea;
	TextArea uitvoerArea;
	
	TextField alfabox;
	TextField betabox;
	TextField qbox;
	TextField rhobox;
	TextField ebox;
	
	Label alfalabel;
	Label betalabel;
	Label qlabel;
	Label rholabel;
	Label elabel;
	
	public final int defaultalfa = 1;
    public final int defaultbeta = 6;
    public final int defaultQ = 100;
    public final double defaultrho = 0.5;
    public final int defaulte = 5; 
	
	
	
	private AntsThread rekenThread;
	public Kaart kaart;
	
	public Map map;
	private boolean mapExists = false;

    /* Constructor */
    DTSP(){
        setLayout(null);
		setVisible(false);
		setSize(800,600);
		setBackground((Color.blue));
        setTitle("Dynamic TSP");
        kaart = new Kaart();
       
        
        //Componenten
        leesButton = new Button();
        leesButton.setLabel("Lees gegevens");
		add(leesButton);
		leesButton.setBackground(java.awt.Color.lightGray);
		leesButton.setBounds(684,65,103,46);
        invoerArea = new TextArea();
        add(invoerArea);
		invoerArea.setBounds(36,65,638,182);
		//initialisatie voor tijdelijk testen:
		//25 steden:
		//invoerArea.setText("5 5 \n13 10\n9 12\n10 39\n35 27\n46 20\n91 13\n81 35\n89 45\n66 60\n90 70\n85 72\n91 90\n65 91\n51 80\n49 75\n40 54\n25 57\n18 77\n29 92\n33 45\n85 67\n90 61\n22 30\n69 25");
		//100 steden:
		invoerArea.setText("83 7\n24 45\n6 27\n87 69\n64 17\n97 97\n64 66\n67 42\n75 88\n72 68\n80 90\n86 59\n73 55\n49 83\n2 50\n94 74\n79 21\n57 87\n8 22\n78 23\n65 88\n21 57\n59 61\n5 35\n43 99\n97 35\n73 34\n80 20\n88 88\n52 72\n31 4\n66 26\n72 8\n42 47\n61 66\n85 69\n94 6\n50 51\n28 36\n49 12\n25 47\n67 12\n52 79\n53 52\n30 58\n41 45\n63 16\n38 94\n3 16\n56 21\n76 82\n27 62\n29 62\n0 60\n22 56\n44 21\n59 26\n25 79\n32 23\n90 69\n65 46\n54 14\n11 52\n38 72\n50 47\n45 99\n89 88\n31 36\n84 80\n6 67\n29 9\n90 3\n25 71\n69 99\n26 90\n71 97\n37 16\n15 56\n9 99\n92 35\n21 75\n84 68\n75 28\n88 86\n72 35\n52 8\n48 95\n82 37\n94 11\n20 78\n76 58\n23 88\n63 28\n96 29\n89 97\n5 23\n26 47\n13 43\n62 1\n68 28");
		uitvoerArea = new TextArea();
		add(uitvoerArea);
		uitvoerArea.setBounds(36,268,639,188);
		pauzeButton = new Button();
        pauzeButton.setLabel("Pauze");
		add(pauzeButton);
		pauzeButton.setBackground(java.awt.Color.lightGray);
		pauzeButton.setBounds(684,268,103,46);
		
		mapButton = new Button();
        mapButton.setLabel("Teken kaart");
		add(mapButton);
		mapButton.setBackground(java.awt.Color.lightGray);
		mapButton.setBounds(684,400,103,46);
		
		
		
		//parameterboxes
		alfabox = new TextField();
		add(alfabox);
		alfabox.setBounds(50,470,30,20);
		alfabox.setText(String.valueOf(defaultalfa));
		betabox = new TextField();
		add(betabox);
		betabox.setBounds(50,500,30,20);
		betabox.setText(String.valueOf(defaultbeta));
		qbox = new TextField();
		add(qbox);
		qbox.setBounds(50,530,30,20);
		qbox.setText(String.valueOf(defaultQ));
		rhobox = new TextField();
		add(rhobox);
		rhobox.setBounds(50,560,30,20);
		rhobox.setText(String.valueOf(defaultrho));
		ebox = new TextField();
		add(ebox);
		ebox.setBounds(50,590,30,20);
		ebox.setText(String.valueOf(defaulte));
		
		alfalabel = new Label("Alfa");
		add(alfalabel);
		alfalabel.setBounds(10,470,35,20);
		betalabel = new Label("Beta");
		add(betalabel);
		betalabel.setBounds(10,500,35,20);
		qlabel = new Label("Q");
		add(qlabel);
		qlabel.setBounds(10,530,35,20);
		rholabel = new Label("Rho");
		add(rholabel);
		rholabel.setBounds(10,560,35,20);
		elabel = new Label("e");
		add(elabel);
		elabel.setBounds(10,590,35,20);
		
        
        //Menu
        menuBalk = new java.awt.MenuBar();
		bestandMenu = new java.awt.Menu("File");
		exitMenuItem = new java.awt.MenuItem("Exit");
		bestandMenu.add(exitMenuItem);
		menuBalk.add(bestandMenu);
		setMenuBar(menuBalk);
        //Listeners
        SymWindow aSymWindow = new SymWindow();
		this.addWindowListener(aSymWindow);
		SymAction lSymAction = new SymAction();
		exitMenuItem.addActionListener(lSymAction);
		leesButton.addActionListener(lSymAction);
		pauzeButton.addActionListener(lSymAction);
		mapButton.addActionListener(lSymAction);
		
    }
    
    
    
   /* 
       Stuff needed for all kinds of things :) 
       Just plain java initializations etc, nothing fancy...
   -------------------------------------------------------------------------------    
   */   
    public void setVisible(boolean b)
	{
		if(b)
		{
			setLocation(50, 50);
		}
		super.setVisible(b);
	}
	
	

	static public void main(String args[])
	{
		(new DTSP()).setVisible(true);
		
		
	}
	
	class SymWindow extends java.awt.event.WindowAdapter
	{
		public void windowClosing(java.awt.event.WindowEvent event)
		{
			Object object = event.getSource();
			if (object == DTSP.this)
				Frame1_WindowClosing(event);
		}
	}
	
	void Frame1_WindowClosing(java.awt.event.WindowEvent event)
	{
		setVisible(false);
		this.dispose();
		System.exit(0);
	}

	class SymAction implements java.awt.event.ActionListener
	{
		public void actionPerformed(java.awt.event.ActionEvent event)
		{
			Object object = event.getSource();
			if (object == exitMenuItem)
				exitMenuItem_ActionPerformed(event);
			else if (object == leesButton)
			    leesButton_ActionPerformed(event);
			else if (object == pauzeButton)
			    pauzeButton_ActionPerformed(event);  
			else if (object == mapButton)
			    mapButton_ActionPerformed(event);        	
		}
	}
	
	void exitMenuItem_ActionPerformed(java.awt.event.ActionEvent event){
	    setVisible(false);
		this.dispose();
		System.exit(0);   
	}    
	
	//puts the parameters in AntsThread, ugly but effective
	public void setParams(AntsThread a){
	    
	    a.alfa = Integer.parseInt(alfabox.getText());
	    a.beta = Integer.parseInt(betabox.getText());
	    a.Q = Integer.parseInt(qbox.getText());
	    a.rho = Double.parseDouble(rhobox.getText());
	    a.e = Integer.parseInt(ebox.getText());
	    
	}    
	
  /*----------------End of "stuff". Things will happen now--------------*/	
  
  //leest gegevens over steden uit de invoerarea
  void leesButton_ActionPerformed(java.awt.event.ActionEvent event){
	    int x,y;
		String invoer = invoerArea.getText();
		StringTokenizer st = new StringTokenizer(invoer);
		while (st.hasMoreTokens()) {
             //uitvoerArea.append(st.nextToken()+"\n");
             x = Integer.parseInt(st.nextToken());
             if (st.hasMoreTokens()) {
             //Er zijn blijkbaar een x en een y gevonden 
                  y = Integer.parseInt(st.nextToken());
                  kaart.voegToe(new Stad(x,y));
                  uitvoerArea.append("Nieuwe stad: "+Integer.toString(x)+ " , " +Integer.toString(y)+"\n");           
             }
         }
	    kaart.initTabellen();
       //initiele kaart is nu opgebouwd	    
	    rekenThread = new AntsThread(this);
	    rekenThread.start();    
	}
  
  
    //Pauzeert/herstart de rekenThread
    void pauzeButton_ActionPerformed(java.awt.event.ActionEvent event){
        if (!rekenThread.isStopped()){
           rekenThread.stopThread();
           pauzeButton.setLabel("Ga door");
        } else {
           pauzeButton.setLabel("Pauze"); 
           rekenThread = new AntsThread(this);
	       rekenThread.start();
        }   
    } 
    
    
    //teken kaart
    void mapButton_ActionPerformed(java.awt.event.ActionEvent event){
		if (! mapExists){
            map = new Map(kaart);
            mapExists = true;		   	
	    } else {
	    	Tour route = rekenThread.getBestTour().kloon();
	    	map.setKaart(kaart);
	    	map.setRoute(route);
	    	map.setVisible(true);
	    }			

    }      
  
  
}