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("/search")
public class SearchServlet extends HttpServlet {
  
  //Gets the current days information for each topic 
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType("application/json;");
    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");
    
    //If the user has not searched any topics yet, print that they have no topics
    if (topics.trim().equals("")) {
      Gson gson = new Gson();
      ArrayList<String> topicsInfo = new ArrayList<>();
      topicsInfo.add("<h2>No topics yet! Search something you want to know more about to get info</h2>");
      response.getWriter().println(gson.toJson(topicsInfo));
      return;
    }

    ArrayList<String> topicsInfo = new ArrayList<>();
    String[] topicsArray = topics.split(",");

    //Goes through each topic and gets that days info, 
    //then adds it to the arraylist that stores the info to print
    for (String topic : topicsArray) {
      String topicName = topic+"topic";
      String iteratorName = topic+"iterator";
      String iterator = (String) entity.getProperty(iteratorName);
      ArrayList<String> urls = (ArrayList<String>) entity.getProperty(topicName);
      int iteratorNum = Integer.parseInt(iterator);
      Boolean advanced = (Boolean)entity.getProperty("advanced"+topic);
      
      //If there are no more urls for this topic, 
      //Move to an advanced search
      //If there are no more urls in the advanced search 
      //print that there is no more info for the topic 
      if (iteratorNum >= urls.size()) {
	if (advanced) {
          topicsInfo.add(0, "No more info for this topic!");
	  topicsInfo.add(0, "<h1>"+topic+":</h1>");
	  continue;
	} else {
          System.out.println("advanced");
          String advancedTopic = "advanced"+topic;
	  urls = getSearch(advancedTopic);
	  iterator = "0";
	  iteratorNum = 0;
	  entity.setProperty(advancedTopic, true);
	  entity.setProperty(topicName, urls);
	}
	System.out.println(urls);
      }

      String info = getInfo(urls, iterator);
      topicsInfo.add(0, info);
      topicsInfo.add(0, "<h1>"+topic+":</h1>");

      //Increment iterator so that the next day they get new info 
      iterator = Integer.toString(Integer.parseInt(iterator)+1);
      entity.setProperty(iteratorName, iterator);
    }
    datastore.put(entity);
    Gson gson = new Gson();
    response.getWriter().println(gson.toJson(topicsInfo));

  }
  
  //Gets the info off a page for a given url 
  private String getInfo(ArrayList<String> urls, String currentUrl) throws IOException {
    String info = "";
    int currentUrlNum = Integer.parseInt(currentUrl);
    String url = urls.get(currentUrlNum);
    System.out.println(url);

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

    for(Element result : results) {
      info = info + result.toString();
    }
    return info;
  }

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

    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; 
  }
}

@@ -3229,53 +3229,147 @@
   %7D%0A
-%0A      String info = getInfo(urls, iterator);
+      String url = urls.get(iteratorNum);%0A      String info = %22%3Ciframe src=%5C%22%22 + url + %22%5C%22 style=%5C%22height:1000px;width:100%25;%5C%22%3E%3C/iframe%3E%22; 
 %0A   

implement iframe