package com.conveyal.r5.analyst.scenario;

import com.conveyal.r5.transit.TransportNetwork;
import com.conveyal.r5.transit.TripPattern;
import com.conveyal.r5.transit.TripSchedule;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;

import static com.conveyal.r5.analyst.scenario.FakeGraph.buildNetwork;
import static com.conveyal.r5.analyst.scenario.FakeGraph.set;
import static org.junit.Assert.assertEquals;

/**
 * Test adding stops (rerouting)
 */
public class AddStopsTest {
    public TransportNetwork network;
    public long checksum;

    @Before
    public void setUp () {
        network = buildNetwork(FakeGraph.TransitNetwork.SINGLE_LINE);
        checksum = network.checksum();
    }

    /** Test rerouting a route in the middle */
    @Test
    public void testRerouteInMiddle () {
        AddStops as = new AddStops();
        // skip s3, insert s5
        as.fromStop = "SINGLE_LINE:s2";
        as.toStop = "SINGLE_LINE:s4";
        as.stops = Arrays.asList(new StopSpec("SINGLE_LINE:s5"));
        as.dwellTimes = new int[] { 40 };
        as.hopTimes = new int[] { 60, 70 };
        as.routes = set("SINGLE_LINE:route");

        Scenario scenario = new Scenario();
        scenario.modifications = Arrays.asList(as);

        TransportNetwork mod = scenario.applyToTransportNetwork(network);

        assertEquals(1, mod.transitLayer.tripPatterns.size());

        TripPattern pattern = mod.transitLayer.tripPatterns.get(0);

        // make sure the stops are in the right order
        assertEquals(4, pattern.stops.length);
        assertEquals("SINGLE_LINE:s1", mod.transitLayer.stopIdForIndex.get(pattern.stops[0]));
        assertEquals("SINGLE_LINE:s2", mod.transitLayer.stopIdForIndex.get(pattern.stops[1]));
        assertEquals("SINGLE_LINE:s5", mod.transitLayer.stopIdForIndex.get(pattern.stops[2]));
        assertEquals("SINGLE_LINE:s4", mod.transitLayer.stopIdForIndex.get(pattern.stops[3]));

        for (TripSchedule schedule : pattern.tripSchedules) {
            // confirm the times are correct
            int[] a = schedule.arrivals;
            int[] d = schedule.departures;
            assertEquals(d[1] + 60, a[2]);
            assertEquals(a[2] + 40, d[2]);
            assertEquals(d[2] + 70, a[3]);
        }
    }

    @After
    public void tearDown () {
        network = null;
    }
}

@@ -2304,24 +2304,1608 @@
    %7D%0A    %7D%0A%0A
+    /** Test extending a route, not removing any stops */%0A    @Test%0A    public void testExtendRouteAtEnd () %7B%0A        AddStops as = new AddStops();%0A        // add s5 at end%0A        as.fromStop = %22SINGLE_LINE:s4%22;%0A        as.stops = Arrays.asList(new StopSpec(%22SINGLE_LINE:s5%22));%0A        as.dwellTimes = new int%5B%5D %7B 40 %7D;%0A        as.hopTimes = new int%5B%5D %7B 60 %7D;%0A        as.routes = set(%22SINGLE_LINE:route%22);%0A%0A        Scenario scenario = new Scenario();%0A        scenario.modifications = Arrays.asList(as);%0A%0A        TransportNetwork mod = scenario.applyToTransportNetwork(network);%0A%0A        assertEquals(1, mod.transitLayer.tripPatterns.size());%0A%0A        TripPattern pattern = mod.transitLayer.tripPatterns.get(0);%0A%0A        // make sure the stops are in the right order%0A        assertEquals(5, pattern.stops.length);%0A        assertEquals(%22SINGLE_LINE:s1%22, mod.transitLayer.stopIdForIndex.get(pattern.stops%5B0%5D));%0A        assertEquals(%22SINGLE_LINE:s2%22, mod.transitLayer.stopIdForIndex.get(pattern.stops%5B1%5D));%0A        assertEquals(%22SINGLE_LINE:s3%22, mod.transitLayer.stopIdForIndex.get(pattern.stops%5B2%5D));%0A        assertEquals(%22SINGLE_LINE:s4%22, mod.transitLayer.stopIdForIndex.get(pattern.stops%5B3%5D));%0A        assertEquals(%22SINGLE_LINE:s5%22, mod.transitLayer.stopIdForIndex.get(pattern.stops%5B4%5D));%0A%0A        for (TripSchedule schedule : pattern.tripSchedules) %7B%0A            // confirm the times are correct%0A            int%5B%5D a = schedule.arrivals;%0A            int%5B%5D d = schedule.departures;%0A            assertEquals(d%5B3%5D + 60, a%5B4%5D);%0A            assertEquals(a%5B4%5D + 40, d%5B4%5D);%0A        %7D%0A    %7D%0A%0A
     @After%0A 

test for extending line at end.