package ch.epfl.isochrone;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import ch.epfl.isochrone.timetable.Date;
import ch.epfl.isochrone.timetable.FastestPathTree;
import ch.epfl.isochrone.timetable.Graph;
import ch.epfl.isochrone.timetable.SecondsPastMidnight;
import ch.epfl.isochrone.timetable.Stop;
import ch.epfl.isochrone.timetable.TimeTable;
import ch.epfl.isochrone.timetable.TimeTableReader;

/**
 * Classe Servant à tester le programme et à les affichés les premières heures
 * d'arrivée.
 * 
 * @author Charles Thiebaut (238935)
 * @author Arnaud Delamare (234841)
 * 
 */
public class TimeTableSearch {

    public static void main(String[] args) throws IOException {
        String nomArret = args[0];
        Stop departure = null;

        String[] champs1 = args[1].split("-");
        String[] champs2 = args[2].split(":");
        Date date = new Date(Integer.parseInt(champs1[2]),
                Integer.parseInt(champs1[1]), Integer.parseInt(champs1[0]));
        int heure = SecondsPastMidnight.fromHMS(Integer.parseInt(champs2[0]),
                Integer.parseInt(champs2[1]), Integer.parseInt(champs2[2]));

        TimeTableReader ttr = new TimeTableReader("/time-table/");
        TimeTable tt = ttr.readTimeTable();

        for (Stop s : tt.stops()) {
            if (s.name().equals(nomArret)) {
                departure = s;
            }
        }

        Graph g = ttr.readGraphForServices(tt.stops(),
                tt.servicesForDate(date), 300, 1.25);

        FastestPathTree f = g.fastestPaths(departure, heure);

        List<String> h = new ArrayList<String>();

        for (Stop s : tt.stops()) {
            if (f.arrivalTime(s) < SecondsPastMidnight.INFINITE) {
                h.add("Arrivée à " + s.name() + " : "
                        + SecondsPastMidnight.toString(f.arrivalTime(s)) + "\n"
                        + "via : " + f.pathTo(s));
            }
        }
        Collections.sort(h);

        for (String b : h) {
            System.out.println(b);
        }
    }
}
