package bruthforth_tsp;

import java.awt.*;
import java.applet.Applet;

/**
 * TravelingSalemanApplet is a visual demonstration of solving (aproximating) the
 * traveling-salesman problem in the most naive way, using the Popcorn scheme.
 */
public class TSPApplet extends Applet implements ComputationObserver {

    static final String WORLD_MAP_FILE = "world.gif";

    Image mapImage;
    WorldMap map;
    Label statusLabel = new Label();
    TextArea window;
    Button startButton=new Button("Start");

    public void init() {
        setLayout(new PropertiesLayout());
        setBackground(Color.white);
        Font titleFont = new Font(getFont().getName(),12,Font.PLAIN);
        Label titleLabel = new Label("Traveling Salesman Problem");
        titleLabel.setFont(titleFont);
        add("name=l1,x=0.5,top=$1",titleLabel);
        mapImage = getImage(getDocumentBase(),WORLD_MAP_FILE);
        map = new WorldMap(mapImage);
        add("name=map,x=0.5,top=l1.bottom,q=$1,width=$851,height=$527",map);
        window=new TextArea();
        add("name=window,x=0.5,top=map.bottom,q=$1,width=map.width,height=$70",window);
        Font statusFont = new Font(getFont().getName(),10,Font.PLAIN);
        statusLabel.setFont(statusFont);
        add("name=status,x=0.5,top=window.bottom,q=$1,width=map.width",statusLabel);
        add("name=start,left=0.2,right=0.8,top=status.bottom,q=$3",startButton);

    }

    public void start() {
        //double[][] distanceMatrix=computeDistanceMatrix(map.getCities());
        //new TSPComputation(distanceMatrix,this).start();
    }

    public boolean action(Event e, Object arg) {
        if (e.target==startButton) {
            double[][] distanceMatrix=computeDistanceMatrix(map.getCities());
            new TSPComputation(distanceMatrix,this).start();
            startButton.enable(false);
            return true;
        }
        return false;
    }

    public void updateDisplay(int[] path) {
        map.setPath(path);
        map.drawingPathOn();
        printList(path);
    }

    private static final double KM_PER_PIXEL = 47.0;

    public void updateStatus(double min,int packetSent, int packetArrived) {
        String minString = "Minimal distance: "+((float)((int)(KM_PER_PIXEL*min*100))/100)+"km";
        String status = minString+"  Packet sent: "+packetSent+
            "  Packet arrived: "+packetArrived;
        statusLabel.setText(status);
    }

    void printList(int[] path) {
        City[] cities=map.getCities();
        String line="";
        int n=cities.length;
        for (int i=0; i<n; i++) {
            line+= cities[path[i]-1].getName()+"-->";
            if (i%10==9)
                line+="\n";
        }
        line+=cities[path[0]-1].getName()+"\n";
        window.setText(line);
    }

    double[][] computeDistanceMatrix(City[] cities) {
        int n = cities.length;
        double[][] map=new double[n][n];
        for (int i=0; i<n; i++)
            for(int j=0; j<n; j++)
                 map[i][j]=cities[i].distanceFrom(cities[j]);
        return map;
    }
}


