package fr.unice.polytech.al.trafficlight.central.provider;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import fr.unice.polytech.al.trafficlight.central.data.*;
import fr.unice.polytech.al.trafficlight.central.data.TrafficLight;
import fr.unice.polytech.al.trafficlight.utils.RuleGroup;
import fr.unice.polytech.al.trafficlight.utils.Scenario;
import fr.unice.polytech.al.trafficlight.utils.enums.TrafficLightId;

import javax.ws.rs.*;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tom dall'agnol on 27/10/16.
 */
@Path("crossroad")
public class CrossRoadInformationService {
    private GsonBuilder builder = new GsonBuilder();

    /**
     * Retrieves all the existing crossroads
     * in the db. You can have more informations
     * about the crossroad doing a GET request
     * on the ressource crossroad/[CrossRoad name]
     *
     * @return a Response containing all the crossroads names
     */
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response retrieveCrossRoads(){
        Gson gson = builder.create();

        List<String> crossRoads = new ArrayList<>();
        crossRoads.add("carrefour_du_casino");
        crossRoads.add("carrefour_des_pins");
        return Response.ok()
                .entity(gson.toJson(crossRoads))
                .build()
                ;
    }

    @GET
    @Path("/{crossRoadName}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response retrieveSpecifcCrossRoad(@PathParam("crossRoadName") String crossRoadName){
        Gson gson = builder.create();
        CrossRoad crossRoad = new CrossRoad("carrefour_du_casino", "url");
        crossRoad.addRoad("avenue du tapis vert");
        crossRoad.addRoad("avenue des orangers");

        TrafficLight trafficLight1 = new TrafficLight("feu de l'avenue des orangers");
        trafficLight1.addRoad("avenue des orangers");
        TrafficLight trafficLight2 = new TrafficLight("feu de l'avenue du tapis vert");
        trafficLight1.addRoad("avenue du tapis vert");

        crossRoad.addTrafficLight(trafficLight1);
        crossRoad.addTrafficLight(trafficLight2);

        Scenario scenar = new Scenario("basicScenario");
        RuleGroup group1 = new RuleGroup("group1", 20);
        RuleGroup group2 = new RuleGroup("group2", 40);
        TrafficLightId id1 = new TrafficLightId("feu de l'avenue des orangers");
        TrafficLightId id2 = new TrafficLightId("feu de l'avenue du tapis vert");
        group1.addTrafficLight(id1);
        group1.addTrafficLight(id2);
        group2.addTrafficLight(id2);

        scenar.addRuleGroupList(0, group1);
        scenar.addRuleGroupList(1, group2);
        scenar.setTransitionTime(5);

        crossRoad.setScenario(scenar);
        return Response.ok().entity(gson.toJson(crossRoad)).build();
    }


    @PUT
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response receiveScenario(Scenario scenario) {
        Gson gson = builder.create();
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target("https://inria-crossing.herokuapp.com");
        target = target.path("crossingmodule/starter");
        String input = "{\"youhou\":\"BITE\"}";
        Response response = target.request(MediaType.APPLICATION_JSON).put(Entity.entity(gson.toJson(scenario),MediaType.APPLICATION_JSON),Response.class);
        System.out.println("RECEIVE OKI");
        return null;
    }
}

@@ -3194,23 +3194,21 @@
 enario(S
-cenario
+tring
  scenari
@@ -3452,45 +3452,48 @@
    S
-tring input = %22%7B%5C%22youhou%5C%22:%5C%22BITE%5C%22%7D%22
+ystem.out.println(gson.toJson(scenario))
 ;%0A  
@@ -3579,36 +3579,24 @@
 .entity(
-gson.toJson(
 scenario
 ),MediaT
@@ -3587,17 +3587,16 @@
 scenario
-)
 ,MediaTy
@@ -3695,12 +3695,46 @@
 urn 
-null
+Response.ok().entity(scenario).build()
 ;%0A  

Send good data in request