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

package com.google.sps.servlets;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.common.flogger.FluentLogger;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.HashMap;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;


/** Servlet that creates a question form for the user to fill out.
* A user can add a question to whichever test they want.
* @author Klaudia Obieglo
*/
@WebServlet("/questionForm")
public class QuestionFormServlet extends HttpServlet {
  private static final FluentLogger logger = FluentLogger.forEnclosingClass();
  Configuration cfg;
  //set up the configuration once
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
    cfg = new Configuration(Configuration.VERSION_2_3_30);
    String path =getServletContext().getRealPath("/WEB-INF/templates/");
    try {
      cfg.setDirectoryForTemplateLoading(new File(path));
    //   cfg.setDirectoryForTemplateLoading(file);
    } catch (IOException e) {
      System.out.println(e);
    }
    // Recommended settings for new projects:
    cfg.setDefaultEncoding("UTF-8");
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    cfg.setLogTemplateExceptions(false);
    cfg.setWrapUncheckedExceptions(true);
    cfg.setFallbackOnNullLoopVariable(false);
  }
  @Override
  public void doGet(final HttpServletRequest request,
        final HttpServletResponse response) throws IOException {
    /*Returns all the questions that the user has created */
    UserService userService = UserServiceFactory.getUserService();
    if (!userService.isUserLoggedIn()) {
      logger.atWarning().log("User is not logged in.");
      response.sendError(HttpServletResponse.SC_UNAUTHORIZED,
        "You are not authorised to view this page");
      return;
    }
    logger.atInfo().log("User=%s is logged in", userService.getCurrentUser());
    String ownerID = userService.getCurrentUser().getEmail();
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    Template template = cfg.getTemplate("QuestionForm.ftl");
    System.out.println(template +"TEMPLATE");
    PrintWriter out = response.getWriter();
    Map referenceData = new HashMap();
    Map<Long,String> testMap = new HashMap<Long,String>();
    try {
      Query queryExams = new Query("Exam").setFilter(new FilterPredicate(
          "ownerID", FilterOperator.EQUAL, ownerID)).addSort("date",
          SortDirection.DESCENDING);
      PreparedQuery listExams = datastore.prepare(queryExams);
      for (Entity entity : listExams.asIterable()) {
        long examID = entity.getKey().getId();
        String name = (String) entity.getProperty("name");
        testMap.put(examID,name);
        referenceData.put("tests", testMap);
      }
    } catch (Exception e) {
      logger.atWarning().log("There was a problem with retrieving the exams %s",
          e);
      response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
          "Internal Error occurred when trying to find your tests");
      return;
    }
    try {
        template.process(referenceData, out);
    } catch ( TemplateException e){
        System.out.println(e);
    }
    logger.atInfo().log("Question form was displayed correctly for the User:"
      + "%s", userService.getCurrentUser());
  }
}
@@ -1007,16 +1007,85 @@
 Entity;%0A
+import com.google.appengine.api.datastore.DatastoreFailureException;%0A
 import c
@@ -3815,54 +3815,8 @@
 %22);%0A
-    System.out.println(template +%22TEMPLATE%22);%0A
     
@@ -3863,25 +3863,21 @@
     Map 
-reference
+tests
 Data = n
@@ -4403,25 +4403,21 @@
         
-reference
+tests
 Data.put
@@ -4457,16 +4457,32 @@
  catch (
+DatastoreFailure
 Exceptio
@@ -4759,18 +4759,16 @@
 %7B%0A      
-  
 template
@@ -4780,17 +4780,13 @@
 ess(
-reference
+tests
 Data
@@ -4799,79 +4799,8 @@
 ;%0A  
-  %7D catch ( TemplateException e)%7B%0A        System.out.println(e);%0A    %7D%0A
     
@@ -4918,13 +4918,298 @@
 ser());%0A
+    %7D catch (TemplateException e) %7B%0A      logger.atWarning().log(%22There was a problem with processing the template %25s%22, e);%0A      response.sendError((HttpServletResponse.SC_INTERNAL_SERVER_ERROR,%0A          %22Internal Error occurred when trying to find your tests%22);%0A      return;%0A    %7D%0A
   %7D%0A%7D

Fix identation and print statements