package com.google.sps.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
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;

/**
 * Loads news
 */
@WebServlet("/news")
public class NewsServlet extends HttpServlet {

  private List<String> keywords = new ArrayList<String>();
  private List<Article> articles = new ArrayList<Article>(
      List.of(
        new ArticleBuilder("Bolton says Trump turned a blind eye to the coronavirus pandemic")
          .withLink("https://www.cnn.com/2020/06/24/politics/john-bolton-interview-cnntv/index.html")
          .build(),
        new ArticleBuilder("Hearing goes off the rails when lawmaker keeps banging table")
          .withLink("https://www.cnn.com/videos/politics/2020/06/24/louie-gohmert-bangs-table-judiciary-hearing-vpx.cnn")
          .build()
      )   
  );

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String json = makeNewsJson();
    System.out.println(json); // test
    response.getWriter().println(json);
  }

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    /*
    Article firstArticle = new ArticleBuilder("Bolton says Trump turned 'a blind eye' to the coronavirus pandemic")
    .withLink("https://www.cnn.com/2020/06/24/politics/john-bolton-interview-cnntv/index.html")
    .build();
    Article secondArticle = new ArticleBuilder("Hearing goes off the rails when lawmaker keeps banging table")
    .withLink("https://www.cnn.com/videos/politics/2020/06/24/louie-gohmert-bangs-table-judiciary-hearing-vpx.cnn")
    .build();
    articles.add(firstArticle);
    articles.add(secondArticle);
    response.sendRedirect("/news.html");
    */
  }

  private String makeNewsJson() {
    Gson gson = new Gson();
    String json = gson.toJson(articles);
    json = "{\"articles\": " + json + "}";  
    return json;
  }

  private class Article {

    private String title;
    private String link;

    private Article(String title, String link) {
      this.title = title;
      this.link = link;
    }
  }

  private class ArticleBuilder {

    private String title;
    private String link;

    private ArticleBuilder(String title) {
      this.title = title;
    }

    private ArticleBuilder withLink(String link) {
      this.link = link;
      return this;
    }
  
    private Article build() {
      return new Article(this.title, this.link);
    }
  }  

}

@@ -348,18 +348,59 @@
 %0A * 
-Loads news
+Servlet for generating news articles from keywords.
 %0A */
@@ -527,16 +527,120 @@
 ring%3E();
+%0A%0A  // Hardcoded list of articles. %0A  // In future implementation, will be generated based on key words.
 %0A  priva
@@ -726,32 +726,34 @@
 ArticleBuilder(%22
+%22%22
 Bolton says Trum
@@ -772,32 +772,41 @@
 lind eye to the 
+%0A        
 coronavirus pand
@@ -806,24 +806,26 @@
 us pandemic%22
+%22%22
 )%0A          
@@ -827,32 +827,34 @@
      .withLink(%22
+%22%22
 https://www.cnn.
@@ -869,32 +869,43 @@
 /06/24/politics/
+%0A          
 john-bolton-inte
@@ -923,24 +923,26 @@
 /index.html%22
+%22%22
 )%0A          
@@ -971,32 +971,34 @@
 ArticleBuilder(%22
+%22%22
 Hearing goes off
@@ -1013,32 +1013,42 @@
 ls when lawmaker
+%0A         
  keeps banging t
@@ -1114,32 +1114,43 @@
 tics/2020/06/24/
+%0A          
 louie-gohmert-ba
@@ -1330,24 +1330,51 @@
 Exception %7B%0A
+    getArticles(keywords);%0A
     String j
@@ -1594,595 +1594,8 @@
 n %7B%0A
-    /*%0A    Article firstArticle = new ArticleBuilder(%22Bolton says Trump turned 'a blind eye' to the coronavirus pandemic%22)%0A    .withLink(%22https://www.cnn.com/2020/06/24/politics/john-bolton-interview-cnntv/index.html%22)%0A    .build();%0A    Article secondArticle = new ArticleBuilder(%22Hearing goes off the rails when lawmaker keeps banging table%22)%0A    .withLink(%22https://www.cnn.com/videos/politics/2020/06/24/louie-gohmert-bangs-table-judiciary-hearing-vpx.cnn%22)%0A    .build();%0A    articles.add(firstArticle);%0A    articles.add(secondArticle);%0A    response.sendRedirect(%22/news.html%22);%0A    */%0A
   %7D%0A

Clean up and add necessary comments.