package com.google.sps.servlets;

import com.google.gson.Gson;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
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.datastore.*;
import java.io.PrintWriter;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.net.URLEncoder;
import java.net.URLDecoder;
import java.io.*; 
import java.util.*; 

@WebServlet("/topics")
public class TopicServlet extends HttpServlet{

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

        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        UserService userService = UserServiceFactory.getUserService(); 
        User user = userService.getCurrentUser();
        String userId = user.getUserId(); 
        Query query = 
        new Query("UserInfo")
        .setFilter(new Query.FilterPredicate("id", Query.FilterOperator.EQUAL, userId));
        PreparedQuery results = datastore.prepare(query); 
        Entity entity = results.asSingleEntity(); 
        String topics = (String) entity.getProperty("topics"); 
        System.out.println(topics);
        if (topics.equals("")){
            response.getWriter().println("{}");
            return;
        }

        while (topics.length() > 0 && topics.substring(0,1).equals(",")){
            try{
                if (topics.length() > 1){
                    topics = topics.substring(1);
                }
                else{
                    topics = "";
                    entity.setProperty("topics", topics);
                    datastore.put(entity); 
                    response.getWriter().println("{}");
                    return;
                }
            } catch (Exception e){
                topics = "";
                entity.setProperty("topics", topics);
                datastore.put(entity); 

                response.getWriter().println("{}");
                return;
            }

        }
        
        //topics += "," + (String) entity.getProperty("time"); 

        String [] listedTopics = topics.split(",");
        System.out.println(Arrays.toString(listedTopics));
        Gson gson = new Gson(); 
        String returnTopics = gson.toJson(listedTopics);
        response.setContentType("application/json;");
        response.getWriter().println(returnTopics);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        UserService userService = UserServiceFactory.getUserService(); 
        User user = userService.getCurrentUser();
        String userId = user.getUserId(); 
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Query query = 
        new Query("UserInfo")
        .setFilter(new Query.FilterPredicate("id", Query.FilterOperator.EQUAL, userId));
        PreparedQuery results = datastore.prepare(query); 
        Entity entity = results.asSingleEntity(); 
        if (entity == null){
            response.sendRedirect("/search.html");
        }

        String topic = request.getParameter("topic");
        String topics = (String) entity.getProperty("topics"); 
	ArrayList<String> urls = (ArrayList<String>) entity.getProperty("urls");

        if (topics.equals("")){
            entity.setProperty("topics", topic);
        }  else {
            topics += ",";
            topics += topic;
            entity.setProperty("topics", topics);
        }
	if(urls == null) {
          urls = new ArrayList<>();
        }
	String[] values = topics.split(",");
        urls = getSearch(topic, urls);
	System.out.println(urls);
	getInfo(urls);
	entity.setProperty("urls", urls);
	datastore.put(entity);
        response.sendRedirect("/search.html");
    }

  private ArrayList<String> getSearch(String topic, ArrayList<String> urls) throws IOException {
    String google = "https://www.google.com/search";
    int num = 5;
    String searchURL = google + "?q=" + topic + "&num=" + num;

    Document doc  = Jsoup.connect(searchURL).userAgent("Chrome").get();
    Elements results = doc.select("a[href]:has(span)").select("a[href]:not(:has(div))");

    for (Element result : results) {
	String linkHref = result.attr("href");
	String linkText = result.text();
	if (linkHref.contains("https")) {
          urls.add(linkHref.substring(7, linkHref.indexOf("&"))); 
	} 
    }
    return urls;
  }

  private void getInfo(ArrayList<String> urls) throws IOException {
    String url = urls.get(1);

    Document doc = Jsoup.connect(url).get();
    Elements results = doc.select("p");

    for(Element result : results) {
      System.out.println(result);
    }
  }

}

@@ -3412,32 +3412,103 @@
 l%22);%0A        %7D%0A%0A
+        String currentUrl = (String) entity.getProperty(%22currentUrl%22);%0A
         String t
@@ -3541,24 +3541,24 @@
 r(%22topic%22);%0A
-
         Stri
@@ -3679,25 +3679,98 @@
 ty(%22urls%22);%0A
+        %0A        if(currentUrl == null) %7B %0A          currentUrl = %220%22;%0A%09%7D
 %0A
-
         if (
@@ -4151,24 +4151,149 @@
 getInfo(urls
+, currentUrl);%0A%09currentUrl =  Integer.toString(Integer.parseInt(currentUrl)+1); %0A%09entity.setProperty(%22currentUrl%22, currentUrl
 );%0A%09entity.s
@@ -5071,32 +5071,51 @@
 ist%3CString%3E urls
+, String currentUrl
 ) throws IOExcep
@@ -5113,32 +5113,86 @@
 s IOException %7B%0A
+    int currentUrlNum = Integer.parseInt(currentUrl);%0A
     String url =
@@ -5201,17 +5201,29 @@
 rls.get(
-1
+currentUrlNum
 );%0A%0A    

Make it iterate through the Urls (currently everytime a new topic is submitted)