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 {
  private static final DatabaseWrapper dbWrapper = new DatabaseWrapper("step-188-instance", "event-organizer-db");

  /** Returns event details from database */
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String eventId = request.getParameter("eventId");
    Optional<Event> eventOptional = dbWrapper.getEventById(eventId);
    Event event;

    // If event DNE, should never happen because backend passes Ids
    if (!eventOptional.isPresent()) {
        event = new Event.Builder("ERROR: This event does not exist", "", new HashSet<String>(), "", null, null).build();
    } else {
        event = eventOptional.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");
    String[] parsedDate = request.getParameter("date").split("/");
    Date date = Date.fromYearMonthDay(Integer.parseInt(parsedDate[2]), Integer.parseInt(parsedDate[1]), Integer.parseInt(parsedDate[0]));
    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);
  }
}
@@ -1197,25 +1197,8 @@
 Id);
-%0A    Event event;
 %0A%0A  
@@ -1221,53 +1221,32 @@
 E, s
-hould never happen because backend passes Ids
+ends 404 ERR to frontend
 %0A   
@@ -1292,119 +1292,59 @@
     
-event = new Event.Builder(%22ERROR: This event does not exist%22, %22%22, new HashSet%3CString%3E(), %22%22, null, null).build(
+response.sendError(HttpServletResponse.SC_NOT_FOUND
 );%0A 
@@ -1363,16 +1363,22 @@
         
+Event 
 event = 
@@ -1441,15 +1441,8 @@
     
-%7D%0A    %0A
     
@@ -1476,24 +1476,28 @@
 xt/html;%22);%0A
+    
     response
@@ -1544,16 +1544,24 @@
 vent));%0A
+    %7D  %0A
   %7D%0A%0A  /

change optional dne to 404