package com.conveyal.gtfs.model;

import com.conveyal.gtfs.GTFSFeed;
import com.conveyal.gtfs.util.GeoUtils;
import com.google.common.base.Joiner;
import com.vividsolutions.jts.awt.PointShapeFactory;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.linearref.LinearLocation;
import com.vividsolutions.jts.linearref.LocationIndexedLine;
import org.geotools.referencing.GeodeticCalculator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.conveyal.gtfs.util.GeoUtils.getCoordDistances;
import static com.conveyal.gtfs.util.GeoUtils.pointFromStop;

/**
 *
 */
public class Pattern extends Entity {
    public static final long serialVersionUID = 1L;
    private static final Logger LOG = LoggerFactory.getLogger(Pattern.class);

    // A unique ID for this journey pattern / stop pattern
    public String pattern_id;

    // The segment of the pattern's geometry (which is always a LineString) on which each stop in the sequence falls.
    public int[] segmentIndex;

    // The percentage in [0..1] along the line segment at which each stop in the sequence falls.
    public double[] segmentFraction;

    public List<String> orderedStops;
    // Contains exemplar information about stops the pattern visits.
    public List<PatternStop> patternStops;
    // TODO: change list of trips to set
    public List<String> associatedTrips;
    // TODO: add set of shapes
    public Set<String> associatedShapes;
    public LineString geometry;
    public String name;
    public String route_id;
    public static Joiner joiner = Joiner.on("-").skipNulls();
    public String feed_id;

    // TODO: Should a Pattern be generated for a single trip or a set of trips that share the same ordered stop list?

    /**
     *
     * @param orderedStops
     * @param trips the first trip will serve as an exemplar for all the others.
     * @param patternGeometry
     */
    public Pattern (List<String> orderedStops, Collection<Trip> trips, LineString patternGeometry){

        // Temporarily make a random ID for the pattern, which might be overwritten in a later step ?
        this.pattern_id = UUID.randomUUID().toString();

        // Assign ordered list of stop IDs to be the key of this pattern.
        // FIXME what about pickup / dropoff type?
        this.orderedStops = orderedStops;

        // Save the string IDs of the trips on this pattern.
        this.associatedTrips = trips.stream().map(t -> t.trip_id).collect(Collectors.toList());

        // In theory all trips could take different paths and be on different routes.
        // Here we're using only the first one as an exemplar.
        String trip_id = associatedTrips.get(0);

        Trip exemplarTrip = trips.iterator().next();
        this.geometry = patternGeometry;

        // feed.getTripGeometry(exemplarTrip.trip_id);

        // Patterns have one and only one route.
        // FIXME are we certain we're only passing in trips on one route? or are we losing information here?
        this.route_id = exemplarTrip.route_id;

        // A name is assigned to this pattern based on the headsign, short name, direction ID or stop IDs.
        // This is not at all guaranteed to be unique, it's just to help identify the pattern.
        if (exemplarTrip.trip_headsign != null){
            name = exemplarTrip.trip_headsign;
        }
        else if (exemplarTrip.trip_short_name != null) {
            name = exemplarTrip.trip_short_name;
        }
        else if (exemplarTrip.direction_id >= 0){
            name = String.valueOf(exemplarTrip.direction_id);
        }
        else{
            name = joiner.join(orderedStops);
        }

        // TODO: Implement segmentIndex using JTS to segment out LineString by stops.

        // TODO: Implement segmentFraction using JTS to segment out LineString by stops.

    }
}

@@ -42,305 +42,33 @@
 com.
-conveyal.gtfs.GTFSFeed;%0Aimport com.conveyal.gtfs.util.GeoUtils;%0Aimport com.google.common.base.Joiner;%0Aimport com.vividsolutions.jts.awt.PointShapeFactory;%0Aimport com.vividsolutions.jts.geom.Coordinate;%0Aimport com.vividsolutions.jts.geom.Geometry;%0Aimport com.vividsolutions.jts.geom.GeometryFactory
+google.common.base.Joiner
 ;%0Aim
@@ -117,37 +117,8 @@
 g;%0A%0A
-import java.io.Serializable;%0A
 impo
@@ -181,438 +181,56 @@
 ort 
-com.vividsolutions.jts.geom.Point;%0Aimport com.vividsolutions.jts.geom.Polygon;%0Aimport com.vividsolutions.jts.linearref.LinearLocation;%0Aimport com.vividsolutions.jts.linearref.LocationIndexedLine;%0Aimport org.geotools.referencing.GeodeticCalculator;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport static com.conveyal.gtfs.util.GeoUtils.getCoordDistances;%0Aimport static com.conveyal.gtfs.util.GeoUtils.pointFromStop
+org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
 ;%0A%0A/

remove unused imports