// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import java.util.Arrays;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.sps.CalendarManager;
import com.google.sps.User;

@WebServlet("/cal")
public class CalendarServlet extends HttpServlet {

  private static final String REDIRECT_PAGE = "/ChatButton?request-type=request-type-match";
  private static final String REDIRECT_PAGE_ERROR = "/chat.jsp";
  private static final String HOST_USER_NOT_AUTHENTICATED =
      "You have not authenticated with Google Calendar yet. "
      + "Please click \"Authroize access to Google Calendar\" before sending a Calendar invite.";

  @Override
  public void init() {}

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {}

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String monthString = request.getParameter("month");
    String dayString = request.getParameter("day");
    String yearString = request.getParameter("year");
    String hourString = request.getParameter("hour");
    String minuteString = request.getParameter("minute");
    String guestName = request.getParameter("guestName");

    if(monthString == null|| dayString == null || yearString == null 
        || hourString == null || minuteString == null || guestName == null) {
      System.out.println("one or more values are null");
    } else {

      int monthInt = Integer.parseInt(monthString);
      int dayInt = Integer.parseInt(dayString);
      int yearInt = Integer.parseInt(yearString);
      int hourInt = Integer.parseInt(hourString);
      int minuteInt = Integer.parseInt(minuteString);

      User hostUser = new User(UserServiceFactory.getUserService().getCurrentUser().getUserId());
      if(!hostUser.isAuthenticated()) {
        AlertManager.setAlert(HOST_USER_NOT_AUTHENTICATED, REDIRECT_PAGE_ERROR, response);
        return;
      }

      User guestUser = null;
      for(User user : hostUser.getMatches()) {
        if(user.getName().equals(guestName)) {
          guestUser = user;
        }
      }

      if(guestUser == null) {
        System.out.println("Unable to find the guest user, unable to create Google Calendar event.");
      } else {
        CalendarManager.createMatchEvent(hostUser, guestUser, 
                                          yearInt, monthInt, dayInt, hourInt, minuteInt);
      }
    }

    response.sendRedirect(REDIRECT_PAGE);
  }

}

@@ -1197,16 +1197,22 @@
  String 
+ALERT_
 HOST_USE
@@ -1392,16 +1392,251 @@
 nvite.%22;
+%0A  private static final String ALERT_EVENT_CREATED = %22Your calendar invite has been sent!%22;%0A  private static final String ALERT_EVENT_NOT_CREATED =%0A      %22There was an error while creating the Google Calendar event. Please try again.%22;
 %0A%0A  @Ove
@@ -2866,16 +2866,22 @@
 etAlert(
+ALERT_
 HOST_USE
@@ -3266,25 +3266,128 @@
 ;%0A      
-%7D else %7B%0A
+  AlertManager.setAlert(ALERT_EVENT_NOT_CREATED, REDIRECT_PAGE_ERROR, response);%0A      %7D else %7B%0A        try %7B%0A  
         
@@ -3483,16 +3483,18 @@
         
+  
 yearInt,
@@ -3543,58 +3543,257 @@
     
-%7D%0A
     
-%7D%0A%0A    response.sendRedirect(REDIRECT_PAGE);
+AlertManager.setAlert(ALERT_EVENT_CREATED, REDIRECT_PAGE, response);%0A        %7D catch (Exception e) %7B%0A          e.printStackTrace();%0A          AlertManager.setAlert(ALERT_EVENT_NOT_CREATED, REDIRECT_PAGE_ERROR, response);%0A        %7D%0A      %7D%0A    %7D%0A
 %0A  %7D

Add alerts for when a calendar event is or isn't created