package com.google.sps.servlets;

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 java.util.Scanner;
import java.util.Random;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import com.google.gson.Gson;
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.EmbeddedEntity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import java.io.PrintWriter;
import com.google.sps.Objects.User;
import com.google.sps.Objects.Post;
import com.google.sps.Objects.Comment;
import com.google.sps.Objects.Group;
import com.google.sps.Objects.response.PostResponse;
import com.google.sps.servlets.ServletHelper;
import error.ErrorHandler;

@WebServlet("/parse-group-data")
public class ParseGroupDataServlet extends HttpServlet {

  DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

  @Override
  public void init() {
    Scanner scanner = new Scanner(
      getServletContext().getResourceAsStream("/WEB-INF/group-test-data.tsv"));

    while (scanner.hasNextLine()) {
      String line = scanner.nextLine();
      String[] fields = line.split("\t");
      createGroupEntity(fields);
    }

    scanner.close();
  }

  /*
   * Creates a Group entity given a row of the TSV and adds to Datastore.
   */
  private void createGroupEntity(String[] fields) {
    String groupName = String.valueOf(fields[0]);
    ArrayList<String> memberIds = getMembers(fields[1]);
    ArrayList<Long> postIds = getPosts(fields, memberIds);
     
    Entity groupEntity = createGroupEntity(groupName, memberIds, postIds);
    datastore.put(groupEntity);
  }

  /**
   * Creates a list of postIds for a Group.
   */
  private ArrayList<Long> getPosts(String[] fields, ArrayList<String> members) {
    ArrayList<Long> postIds = new ArrayList<>();
    for (int i = 2; i <= 6; i++) {
      long postId = createPost(fields[i], getRandomMember(members));
      postIds.add(postId);
    }
    return postIds;
  }

  /**
   * Creates a Post object and adds it to the datastore given information from TSV file.
   */
  private long createPost(String postText, String author) {
    String authorName = author;
    String challengeName = "Challenge Name";
    String img = null;
    HashSet<String> likes = new HashSet<>();
    ArrayList<Comment> comments = new ArrayList<>();

    // Creates entity with submitted data and add to database
    Post post = new Post(0, authorName, postText, comments, challengeName, System.currentTimeMillis(), img, likes);
    Entity postEntity = post.toEntity();
    datastore.put(postEntity);
    return postEntity.getKey().getId();
  }

  /**
   * Creates and returns a groupEntity given information from TSV file.
   */
  private Entity createGroupEntity(String groupName, ArrayList<String> members, 
      ArrayList<Long> posts) {
    Entity groupEntity = new Entity("Group");
    groupEntity.setProperty("memberIds", members);
    groupEntity.setProperty("challenges", new ArrayList<Long>());
    groupEntity.setProperty("posts", posts);
    groupEntity.setProperty("options", new ArrayList<Long>());
    groupEntity.setProperty("groupName", groupName);
    groupEntity.setProperty("headerImg", "");
    return groupEntity;
  }

  /**
   * Gets the memberIds of a group.
   */
  private ArrayList<String> getMembers(String list) {
    // Trim any whitespace immediately following a comma.
    list = list.replaceAll("(\\s*,\\s*)(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", ",");

    // Convert the input to an array list.
    String[] membersArray = list.split(",");
    ArrayList<String> membersList = new ArrayList<String>();
    membersList.addAll(Arrays.asList(membersArray));

    return membersList;
  }

  /**
   * Returns a random member from a list of members.
   */
  private String getRandomMember(ArrayList<String> members) {
    Random rand = new Random();
    String member = members.get(rand.nextInt(members.size()));
    return member;
  }
}
@@ -1119,16 +1119,122 @@
 ndler;%0A%0A
+/**%0A * Parses a TSV file of fake group data and populates the Datastore with Group and Post entities.%0A */%0A
 @WebServ
@@ -1734,16 +1734,17 @@
  %7D%0A%0A  /*
+*
 %0A   * Cr
@@ -1747,17 +1747,16 @@
 * Create
-s
  a Group
@@ -1793,17 +1793,16 @@
  and add
-s
  to Data
@@ -2167,17 +2167,16 @@
 * Create
-s
  a list 
@@ -2514,17 +2514,16 @@
 * Create
-s
  a Post 
@@ -2536,17 +2536,16 @@
  and add
-s
  it to t
@@ -3167,17 +3167,16 @@
 * Create
-s
  and ret
@@ -3178,17 +3178,16 @@
 d return
-s
  a group
@@ -3757,17 +3757,16 @@
    * Get
-s
  the mem
@@ -4217,24 +4217,24 @@
 %0A  %7D%0A%0A  /**%0A
+
    * Returns
@@ -4232,17 +4232,16 @@
 * Return
-s
  a rando

Comment functions