// Copyright 2021 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.

package com.google.sps.servlets;

import com.google.gson.Gson;
import com.google.sps.data.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/competitionInfo")
public class CompetitionInfoServlet extends HttpServlet {

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    Gson gson = new Gson();

    response.setContentType("application/json");

    response.getWriter().println(gson.toJson(getUserCompetitions()));
  }

  /**
   * Return all competitions that the user is in. (Hard-coded for now)
   * @return -- all Competitions object that user is in.
   */
  private List<Competition> getUserCompetitions() {
    List<Competition> usersCompetitions = new ArrayList<>();
    List<CompetitorInfo> competitors = new ArrayList<>();

    competitors.add(CompetitorInfo.create(1, 1, "Bob", "bobk@", 1000, 500));
    competitors.add(CompetitorInfo.create(2, 2, "Jack", "jackm@", 800, 450));
    Competition compObj = Competition.create(1895, "Google-Clouds-Comp", "Emma Hogan", "emmahogan@",
        new Date(2021, 1, 1).getTime(), new Date(2021, 1, 10).getTime());

    List<CompetitorInfo> competitors2 = new ArrayList<>();
    competitors2.add(CompetitorInfo.create(1, 1, "Mary", "maryk@", 1000, 500));
    competitors2.add(CompetitorInfo.create(2, 2, "Bell", "bellm@", 800, 450));
    competitors2.add(CompetitorInfo.create(3, 3, "Bob", "bobk@", 700, 400));
    Competition compObj2 = Competition.create(1896, "Tide Pod", "Mercury Lin", "mercurylin@",
        new Date(2021, 1, 1).getTime(), new Date(2021, 1, 10).getTime());

    usersCompetitions.add(compObj);
    usersCompetitions.add(compObj2);

    return usersCompetitions;
  }
}

@@ -1238,76 +1238,229 @@
 %22);%0A
-%0A
     
-response.getWriter().println(gson.toJson(getUserCompetitions())
+String competitor = gson.toJson(getCompetitorInfo());%0A    String competitions = gson.toJson(getUserCompetitions());%0A    String allInfo = combineJSON(competitor, competitions);%0A%0A    response.getWriter().println(allInfo
 );%0A 
@@ -1655,16 +1655,103 @@
 ons() %7B%0A
+    //data for each of the user's competitions, including info about other competitors%0A
     List
@@ -2764,11 +2764,419 @@
 ons;%0A  %7D
+%0A%0A  private CompetitorInfo getCompetitorInfo() %7B%0A    //hard coded example for now. This is data specific to the logged in user%0A    return CompetitorInfo.create(1, 1, %22Emma%22, %22emmahogan@%22, 100000, 10);%0A  %7D%0A%0A  private String combineJSON(String competitorInfo, String competitions) %7B%0A    return %22%7B%22 + %0A      %22%5C%22user%5C%22 : %22 + competitorInfo + %22, %22 +%0A      %22%5C%22competitionInfo%5C%22 : %22 + competitions + %0A      %22%7D%22;%0A  %7D
 %0A%7D%0A

Update JSON return to include user's competitor info separately to competitions info