package com.bddinaction.chapter2.jbehave.steps;

import com.bddinaction.chapter2.utilities.JsonBuilder;
import com.fasterxml.jackson.core.type.TypeReference;
import net.serenitybdd.rest.SerenityRest;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import org.joda.time.LocalTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public class OptimalItinerarySteps {
    final Logger logger = LoggerFactory.getLogger(OptimalItinerarySteps.class);
    String protocol;
    String host;
    String port;

    public OptimalItinerarySteps() {
        protocol = System.getProperty("protocol");
        logger.info("Reading config property: protocol = {}", protocol);

        host = System.getProperty("host");
        logger.info("Reading config property: host = {}", host);

        port = System.getProperty("port");
        logger.info("Reading config property: port = {}", port);
    }

    @Given("$line line trains from $lineStart leave $departure for $destination at $departureTimes")
    public void givenArrivingTrains(String line, String lineStart, String departure, String destination, List<LocalTime> departureTimes) {
    }

    @When("I want to travel from $departure to $destination at $startTime")
    public void whenIWantToTravel(String departure, String destination, LocalTime startTime) throws IOException {
        String uri = String.format("%s://%s:%s/train-timetables/itinerary/departuretimes/from/%s/to/%s/at/%s", protocol, host, port, departure, destination, startTime);

        logger.info("URI: {}", uri);

        SerenityRest.when()
                        .get(uri);
    }

    @Then("I should be told about the trains at: $expectedTrainTimes")
    public void shouldBeInformedAbout(List<LocalTime> expectedTrainTimes) throws IOException {
        String response = SerenityRest.then().extract().response().asString();

        assertThat(new JsonBuilder().<List<LocalTime>>build(response, new TypeReference<List<LocalTime>>() {})).isEqualTo(expectedTrainTimes);
    }
}


@@ -1530,20 +1530,56 @@
    S
-tring uri = 
+erenityRest.when()%0A                        .get(
 Stri
@@ -1724,109 +1724,8 @@
 ime)
-;%0A%0A        logger.info(%22URI: %7B%7D%22, uri);%0A%0A        SerenityRest.when()%0A                        .get(uri
 );%0A 
@@ -1900,88 +1900,8 @@
 n %7B%0A
-        String response = SerenityRest.then().extract().response().asString();%0A%0A
     
@@ -1960,16 +1960,59 @@
 ild(
-response
+SerenityRest.then().extract().response().asString()
 , ne

update serenity version