package com.conveyal.gtfs.model;

import com.conveyal.gtfs.GTFSFeed;
import com.google.common.base.Joiner;
import com.vividsolutions.jts.geom.LineString;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by landon on 2/5/16.
 */
public class Pattern implements Serializable {
    public static final long serialVersionUID = 1L;

    public String pattern_id;
    public int[] segmentIndex;
    public double[] segmentFraction;
    public List<String> orderedStops;
    public List<String> associatedTrips;
    public Set<String> associatedRoutes;
    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?
    public Pattern (GTFSFeed feed, List<String> orderedStops, List<String> trips){
        this.feed_id = feed.feedId;

        this.pattern_id = UUID.randomUUID().toString();

        // Assign ordered stops to key of tripsForStopPattern
        this.orderedStops = orderedStops;

        // Assign associated trips to value of tripsForStopPattern
        this.associatedTrips = trips;
        this.associatedRoutes = new HashSet<>();
        this.associatedTrips.forEach((id) -> {
            this.associatedRoutes.add(feed.trips.get(id).route_id);
        });
//        for (String tripId : this.associatedTrips){
//            this.associatedRoutes.add(feed.trips.get(tripId).route);
//        }
        // Get geometry for first trip in list of associated trips
        String trip_id = associatedTrips.get(0);
        Trip trip;

        trip = feed.trips.get(trip_id);
        this.geometry = feed.getTripGeometry(trip.trip_id);

        // patterns are now on one and only one route
        this.route_id = trip.route_id;

        if (trip.trip_headsign != null){
            name = trip.trip_headsign;
        }
        else if (trip.trip_short_name != null) {
            name = trip.trip_short_name;
        }
        else if (trip.direction_id >= 0){
            name = String.valueOf(trip.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.

    }
}

@@ -577,49 +577,8 @@
 ps;%0A
-    public Set%3CString%3E associatedRoutes;%0A
     
@@ -1256,320 +1256,8 @@
 ps;%0A
-        this.associatedRoutes = new HashSet%3C%3E();%0A        this.associatedTrips.forEach((id) -%3E %7B%0A            this.associatedRoutes.add(feed.trips.get(id).route_id);%0A        %7D);%0A//        for (String tripId : this.associatedTrips)%7B%0A//            this.associatedRoutes.add(feed.trips.get(tripId).route);%0A//        %7D
 %0A   

remove associatedRoutes (to align with r5 Pattern model)