// 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 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 java.io.IOException;
import java.util.ArrayList;
import com.google.gson.Gson;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** Servlet that returns some example content. TODO: modify this file to handle comments data */
@WebServlet("/data")
public class DataServlet extends HttpServlet {
    /* doGet is called by the fetch instruction in the JS function 
                called by the html body after loading the page */
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        PreparedQuery data_query = datastore.prepare(new Query("Task"));

        // Get the specified no of comments to be displayed.
        int comm_limit = 0;
        try {
            comm_limit = Integer.parseInt(request.getParameter("com-limit"));
        } catch (NumberFormatException e) {
            response.setContentType("text/html;");
            response.getWriter().println("Invalid argument for no of comments.");
            return ;
        }

        ArrayList<String> comments = new ArrayList<String>();
        for (Entity com_entity : data_query.asIterable()) {
            if (comments.size() == comm_limit)
                break;
            comments.add((String) com_entity.getProperty("comment"));
        }

        response.setContentType("application/json;");
        response.getWriter().println(toJsonUsingGson(comments));
    }

    /* doPost is called when the user clicks the submit button */
    @Override
    public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException {
        String txt = request.getParameter("comment-input");
        if (txt.length() == 0) {
            response.setContentType("text/html;");
            response.getWriter().println("Enter a non-empty comment.");
            return ;
        }

        // Create an entity for the comment and store it
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Entity ent = new Entity("Task");
        ent.setProperty("comment", txt);

        datastore.put(ent);
        response.sendRedirect("/index.html");
    }

    private String toJsonUsingGson (Object obj) {
        Gson g = new Gson();
        String json = g.toJson(obj);
        return json;
    }
}

@@ -616,16 +616,137 @@
 vlets;%0A%0A
+import java.text.DateFormat;  %0Aimport java.text.SimpleDateFormat;  %0Aimport java.util.Date;  %0Aimport java.util.Calendar;%0A%0A
 import c
@@ -797,16 +797,16 @@
 ervice;%0A
-
 import c
@@ -1850,16 +1850,37 @@
 (%22Task%22)
+.addSort(%22timestamp%22)
 );%0A%0A    
@@ -2524,16 +2524,114 @@
 omment%22)
+%0A                        + %22%5CnSubmitted at: %22 + (String) com_entity.getProperty(%22submission-time%22)
 );%0A     
@@ -3371,24 +3371,24 @@
 ty(%22Task%22);%0A
-
         ent.
@@ -3413,24 +3413,342 @@
 nt%22, txt);%0A%0A
+        DateFormat dateFormat = new SimpleDateFormat(%22yyyy-MM-dd 'at' HH:mm:ss z%22); %0A        long current_time = System.currentTimeMillis(); %0A        String strDate = dateFormat.format(new Date(current_time));  %0A        ent.setProperty(%22timestamp%22, current_time);%0A        ent.setProperty(%22submission-time%22, strDate);%0A%0A
         data

Save and show timestamps for comments.