/** @param args */
  public static void main(String[] args) {
    Header.printHeader(ExtractVehiclesForSollyFrans.class.toString(), args);

    String path = "/home/jwjoubert/Documents/data/Digicore/ByMonth/201403/SollyFrans/xml/";
    String output = "/home/jwjoubert/Documents/data/Digicore/ByMonth/201403/SollyFrans/chains.csv";
    List<File> list =
        FileUtils.sampleFiles(new File(path), 100, FileUtils.getFileFilter(".xml.gz"));

    CoordinateTransformation ct =
        TransformationFactory.getCoordinateTransformation("WGS84_SA_Albers", "WGS84");

    BufferedWriter bw = IOUtils.getBufferedWriter(output);
    try {
      bw.write("Vehicle,Chain,Activity,X,Y,Long,Lat,Start,End");
      bw.newLine();

      for (File f : list) {
        DigicoreVehicleReader_v1 dvr = new DigicoreVehicleReader_v1();
        dvr.readFile(f.getAbsolutePath());
        DigicoreVehicle v = dvr.getVehicle();
        bw.write(v.getId().toString());
        bw.write(",");

        int chain = 1;
        for (DigicoreChain c : v.getChains()) {
          int act = 1;
          for (DigicoreActivity a : c.getAllActivities()) {
            Coord coord = a.getCoord();
            Coord coordWgs84 = ct.transform(coord);

            String s =
                String.format(
                    "%s,%d,%d,%.0f,%.0f,%.6f,%.6f,%s,%s\n",
                    v.getId().toString(),
                    chain,
                    act,
                    coord.getX(),
                    coord.getY(),
                    coordWgs84.getX(),
                    coordWgs84.getY(),
                    getNiceDate(a.getStartTimeGregorianCalendar()),
                    getNiceDate(a.getEndTimeGregorianCalendar()));
            bw.write(s);
            act++;
          }
          chain++;
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
      throw new RuntimeException("Cannot write to " + output);
    } finally {
      try {
        bw.close();
      } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Cannot close " + output);
      }
    }

    Header.printFooter();
  }
  /**
   * the following little graph is used:
   *
   * <p>2 -------> 3 ^ / / | w:2 / | / w:1 w:1 / / | / / |/<------- 1 <--- w:3 ---- 4
   */
  private DigicoreNetwork buildSmallNetwork() {
    DigicoreNetwork dn = new DigicoreNetwork();

    DigicoreActivity da1 =
        new DigicoreActivity("test", TimeZone.getTimeZone("GMT+2"), new Locale("en"));
    da1.setCoord(new CoordImpl(0.0, 0.0));
    da1.setFacilityId(Id.create(1, ActivityFacility.class));

    DigicoreActivity da2 =
        new DigicoreActivity("test", TimeZone.getTimeZone("GMT+2"), new Locale("en"));
    da2.setCoord(new CoordImpl(0.0, 1.0));
    da2.setFacilityId(Id.create(2, ActivityFacility.class));

    DigicoreActivity da3 =
        new DigicoreActivity("test", TimeZone.getTimeZone("GMT+2"), new Locale("en"));
    da3.setCoord(new CoordImpl(1.0, 1.0));
    da3.setFacilityId(Id.create(3, ActivityFacility.class));

    DigicoreActivity da4 =
        new DigicoreActivity("test", TimeZone.getTimeZone("GMT+2"), new Locale("en"));
    da4.setCoord(new CoordImpl(1.0, 0.0));
    da4.setFacilityId(Id.create(4, ActivityFacility.class));

    dn.addArc(da1, da2);
    dn.addArc(da1, da3);
    dn.addArc(da1, da3);
    dn.addArc(da3, da1);
    dn.addArc(da4, da1);
    dn.addArc(da4, da1);
    dn.addArc(da4, da1);

    return dn;
  }