package com.google.vinet.servlets;

import com.google.appengine.api.datastore.*;
import com.google.appengine.api.users.*;
import com.google.gson.*;
import com.google.vinet.data.*;
import java.io.*;
import java.util.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;

@WebServlet("/fetch-requests")
public class IsolateRequestServlet  extends HttpServlet{
  public DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
  public UserService userService = UserServiceFactory.getUserService();
  public RegistrationServlet registrationServlet = new RegistrationServlet();

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    if (response == null) {
      throw new IllegalArgumentException("response must not be null");
    }

    if (request == null) {
      throw new IllegalArgumentException("request must not be null");
    }

    if (!this.userService.isUserLoggedIn()) {
      response.sendError(
          HttpServletResponse.SC_UNAUTHORIZED, "user must be logged in to fetch a request");
      return;
    }

    final boolean registered = registrationServlet.isUserRegistered(this.userService);

    if (!registered) {
      response.sendError(
          HttpServletResponse.SC_UNAUTHORIZED, "user must be registered to fetch a request");
      return;
    }

    final String userId = userService.getCurrentUser().getUserId();

    final Query query = new Query(IsolateTimeSlot.ISOLATE_TIME_SLOT_TABLE_NAME);
    final Query.Filter filter = new Query.FilterPredicate("isolateId", Query.FilterOperator.EQUAL, userId);
    query.setFilter(filter).addSort("date", Query.SortDirection.DESCENDING);

    final PreparedQuery results = datastore.prepare(query);

    List<Match> isolateRequests = new LinkedList<>();

    Gson gson = new Gson();

    try{
      for (Entity entity : results.asIterable()){
        final String date = (String) entity.getProperty("date");
        final String start = ((String) entity.getProperty("startTime")).split("T")[0];
        final String end = ((String) entity.getProperty("endTime")).split("T")[0];

        final Key ticketKey = KeyFactory.stringToKey((String) entity.getProperty("ticketKey"));
        final Entity ticket = datastore.get(ticketKey);

        final String[] subjects = gson.fromJson((String) ticket.getProperty("subjects"), String[].class);
        final String[] details = gson.fromJson((String) ticket.getProperty("details"), String[].class);

        final Match isolateRequest = new Match(date, start, end, null, null, subjects, details);
        isolateRequests.add(isolateRequest);
      }
    } catch (EntityNotFoundException exception) {
      response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
      /* Since an EntityNotFoundException implies the system has stored an isolate's request, but failed to link
       * that request to the correct Ticket entity in Datastore, the exception must be thrown to allow Google
       * Cloud Console to observe and log it. It should not be caught by the caller. */
      throw new RuntimeException(exception);
    }

    try {
      response.getWriter().println(isolateRequests);
    } catch (Exception exception) {
      response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
  }
}

@@ -187,24 +187,71 @@
  java.io.*;%0A
+import java.time.*;%0Aimport java.time.format.*;%0A
 import java.
@@ -2040,16 +2040,36 @@
 start = 
+OffsetDateTime.parse
 ((String
@@ -2103,29 +2103,56 @@
 Time%22)).
-split(%22T%22)%5B0%5D
+format(DateTimeFormatter.ISO_LOCAL_TIME)
 ;%0A      
@@ -2172,16 +2172,36 @@
 g end = 
+OffsetDateTime.parse
 ((String
@@ -2237,21 +2237,48 @@
 %22)).
-split(%22T%22)%5B0%5D
+format(DateTimeFormatter.ISO_LOCAL_TIME)
 ;%0A%0A 
@@ -2709,18 +2709,14 @@
 nd, 
-null, null
+%22%22, %22%22
 , su
@@ -3316,16 +3316,28 @@
 println(
+gson.toJson(
 isolateR
@@ -3343,16 +3343,41 @@
 Requests
+.toArray(new Match%5B%5D %7B%7D))
 );%0A    %7D

update time formatting