package org.mtransit.parser.ca_montreal_amt_bus;

import org.mtransit.parser.DefaultAgencyTools;
import org.mtransit.parser.Utils;
import org.mtransit.parser.gtfs.data.GRoute;
import org.mtransit.parser.gtfs.data.GStop;
import org.mtransit.parser.gtfs.data.GTrip;
import org.mtransit.parser.mt.data.MTrip;

// http://www.amt.qc.ca/developers/
// http://www.amt.qc.ca/xdata/express/google_transit.zip
public class MontrealAMTBusAgencyTools extends DefaultAgencyTools {

	public static final String ROUTE_TYPE_FILTER = "3"; // bus only

	public static void main(String[] args) {
		if (args == null || args.length == 0) {
			args = new String[3];
			args[0] = "input/gtfs.zip";
			args[1] = "../ca-montreal-amt-bus-android/res/raw/";
			args[2] = ""; // files-prefix
		}
		new MontrealAMTBusAgencyTools().start(args);
	}

	@Override
	public void start(String[] args) {
		System.out.printf("Generating AMT bus data...\n");
		long start = System.currentTimeMillis();
		super.start(args);
		System.out.printf("Generating AMT bus data... DONE in %s.\n", Utils.getPrettyDuration(System.currentTimeMillis() - start));
	}

	@Override
	public boolean excludeRoute(GRoute gRoute) {
		if (ROUTE_TYPE_FILTER != null && !gRoute.route_type.equals(ROUTE_TYPE_FILTER)) {
			return true;
		}
		return super.excludeRoute(gRoute);
	}

	@Override
	public void setTripHeadsign(MTrip mTrip, GTrip gTrip) {
		String stationName = cleanTripHeadsign(gTrip.trip_headsign);
		int directionId = Integer.valueOf(gTrip.direction_id);
		mTrip.setHeadsignString(stationName, directionId);
	}

	private static final String DIRECTION = "Direction ";

	private String cleanTripHeadsign(String gTripHeading) {
		return cleanStopName(gTripHeading.substring(DIRECTION.length()));
	}

	public static final String PLACE_CHAR_DE = "de ";
	public static final String PLACE_CHAR_RUE = "rue ";

	@Override
	public String cleanStopName(String gStopName) {
		String result = gStopName;
		if (result.contains(PLACE_CHAR_DE)) {
			result = result.replace(PLACE_CHAR_DE, "");
		}
		if (result.contains(PLACE_CHAR_RUE)) {
			result = result.replace(PLACE_CHAR_RUE, "");
		}
		return super.cleanStopName(result);
	}

	@Override
	public int getStopId(GStop gStop) {
		return Integer.valueOf(getStopCode(gStop)); // using stop code as stop ID
	}

}

@@ -257,16 +257,58 @@
 .GTrip;%0A
+import org.mtransit.parser.mt.data.MSpec;%0A
 import o
@@ -1716,29 +1716,32 @@
 %09return 
-cleanStopName
+MSpec.cleanLabel
 (gTripHe
@@ -1786,414 +1786,109 @@
 %09%7D%0A%0A
-%09public static final String PLACE_CHAR_DE = %22de %22;%0A%09public static final String PLACE_CHAR_RUE = %22rue %22;%0A%0A%09@Override%0A%09public String cleanStopName(String gStopName) %7B%0A%09%09String result = gStopName;%0A%09%09if (result.contains(PLACE_CHAR_DE)) %7B%0A%09%09%09result = result.replace(PLACE_CHAR_DE, %22%22);%0A%09%09%7D%0A%09%09if (result.contains(PLACE_CHAR_RUE)) %7B%0A%09%09%09result = result.replace(PLACE_CHAR_RUE, %22%22);%0A%09%09%7D%0A%09%09return super.clean
+%0A%09@Override%0A%09public String cleanStopName(String gStopName) %7B%0A%09%09return super.cleanStopNameFR(g
 StopName
 (res
@@ -1887,15 +1887,8 @@
 Name
-(result
 );%0A%09

Improve label cleanup