package com.google.sps.servlets;

import com.google.appengine.api.users.UserServiceFactory;
import com.google.cloud.Date;
import com.google.gson.Gson;
import com.google.sps.data.Event;
import com.google.sps.data.User;
import com.google.sps.utilities.DatabaseWrapper;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/create-event")
public class EventCreationServlet extends HttpServlet {
  /** Returns event details from database */
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String eventId = request.getParameter("eventId");
    DatabaseWrapper dbWrapper = new DatabaseWrapper("step-188-instance", "event-organizer-db");
    Event event = dbWrapper.getEventById(eventId).get().toBuilder().setId(eventId).build();

    response.setContentType("text/html;");
    response.getWriter().println(new Gson().toJson(event));
  }

  /** Posts new created event to database and redirects to page with created event details*/
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String name = request.getParameter("name");

    // TO DO: throw exception in frontend, get from not hardcoded
    Date date = Date.fromYearMonthDay(2016, 9, 15);
    // try {
    //     date = new SimpleDateFormat("dd/MM/yyyy").parse(request.getParameter("date"));
    // } catch (ParseException e) {
    //     e.printStackTrace();
    // }

    String description = request.getParameter("description");
    String location = request.getParameter("location");
    Set<String> labels = Collections.unmodifiableSet(new HashSet<>(
        Arrays.asList("None"))); // hardcoded for now, we need to create label pool first

    /** TO DO: Replace with current logged in user after PR #43 pushed*/
    String NAME = "Bob Smith";
    String EMAIL = "bobsmith@example.com";
    User host = new User.Builder(NAME, EMAIL).build();
    Event event = new Event.Builder(name, description, labels, location, date, host).build();
    DatabaseWrapper dbWrapper = new DatabaseWrapper("step-188-instance", "event-organizer-db");
    dbWrapper.insertorUpdateEvent(event);

    String redirectUrl = "/event-details.html?eventId=" + event.getId();
    response.sendRedirect(redirectUrl);
  }
}
@@ -1109,27 +1109,45 @@
 %22);%0A    
+Optional%3C
 Event
+%3E
  event
+Optional
  = dbWra
@@ -1172,16 +1172,306 @@
 eventId)
+;%0A    Event event;%0A%0A    // If event DNE, should never happen because backend passes Ids%0A    if (!eventOptional.isPresent()) %7B%0A        event = new Event.Builder(%22ERROR: This event does not exist%22, %22%22, new HashSet%3CString%3E(), %22%22, null, null).build();%0A    %7D else %7B%0A        event = eventOptional
 .get().t
@@ -1505,16 +1505,26 @@
 uild();%0A
+    %7D%0A    
 %0A    res
@@ -1881,307 +1881,212 @@
 %22);%0A
-%0A
     
-// TO DO: throw exception in frontend, get from not hardcoded%0A    Date date = Date.fromYearMonthDay(2016, 9, 15);%0A    // try %7B%0A    //     date = new SimpleDateFormat(%22dd/MM/yyyy%22).parse(request.getParameter(%22date%22));%0A    // %7D catch (ParseException e) %7B%0A    //     e.printStackTrace();%0A    // %7D%0A
+String%5B%5D parsedDate = request.getParameter(%22date%22).split(%22/%22);%0A    Date date = Date.fromYearMonthDay(Integer.parseInt(parsedDate%5B2%5D), Integer.parseInt(parsedDate%5B1%5D), Integer.parseInt(parsedDate%5B0%5D));
 %0A   

check optional exists