package com.google.sps.search.servlets;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.sps.search.Trie;
import com.google.sps.servlets.AuthenticatedServlet;
import com.google.sps.servlets.ServletHelper;

@WebServlet("/name-data")
public class NameData extends AuthenticatedServlet {

  Trie firstNameTrie;
  String trieFile = "../../data/trie";

  @Override
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
    try {
      FileInputStream fileInput = new FileInputStream(new File(trieFile));
      ObjectInputStream objectInput = new ObjectInputStream(fileInput);
      firstNameTrie = (Trie) objectInput.readObject();

      fileInput.close();
      objectInput.close();
      return;
    } catch (FileNotFoundException e) {
      System.err.println("File does not exist");
    } catch (IOException e) {
      System.err.println("Cannot read from file");
    } catch (NumberFormatException e) {
      System.err.println("Cannot parse to integer");
    } catch (ClassNotFoundException e) {
      System.err.println("Class not found");
    }
    System.out.println("initializing new trie");
    firstNameTrie = new Trie();
  }

  @Override
  public void destroy() {
    System.out.println("destroying");
    this.saveState();
  }

  public void saveState() {
    System.out.println("saving state");
    FileOutputStream fileOutputStream;
    try {
      fileOutputStream = new FileOutputStream(new File(trieFile));
      ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
      objectOutputStream.writeObject(firstNameTrie);

      objectOutputStream.close();
      fileOutputStream.close();
      System.out.println("successfully written to file");
    } catch (FileNotFoundException e1) {
      System.err.println("File does not exist");
    } catch (IOException e) {
      System.err.println("Cannot write to file");
    }
  }

  @Override
  public void doGet(String userId, HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    firstNameTrie.incrementCounter();
    ServletHelper.write(response, firstNameTrie, "application/json");
    System.out.println("This servlet has been accessed " + firstNameTrie.getCount() + " times");
  }

  @Override
  public void doPost(String userId, HttpServletRequest request, HttpServletResponse response) {}
}

@@ -254,16 +254,67 @@
 tStream;
+%0Aimport java.util.ArrayList;%0Aimport java.util.List;
 %0A%0Aimport
@@ -757,16 +757,37 @@
 meTrie;%0A
+  Trie lastNameTrie;%0A
   String
@@ -1101,20 +1101,27 @@
 Trie = (
+(List%3C
 Trie
+%3E
 ) object
@@ -1138,16 +1138,93 @@
 Object()
+).get(0);%0A      lastNameTrie = ((List%3CTrie%3E) objectInput.readObject()).get(1)
 ;%0A%0A     
@@ -1458,101 +1458,8 @@
 %22);%0A
-    %7D catch (NumberFormatException e) %7B%0A      System.err.println(%22Cannot parse to integer%22);%0A
     
@@ -1550,57 +1550,8 @@
   %7D%0A
-    System.out.println(%22initializing new trie%22);%0A
     
@@ -1574,24 +1574,55 @@
 new Trie();%0A
+    lastNameTrie = new Trie();%0A
   %7D%0A%0A  @Over
@@ -1656,46 +1656,8 @@
 ) %7B%0A
-    System.out.println(%22destroying%22);%0A
     
@@ -1711,48 +1711,8 @@
 ) %7B%0A
-    System.out.println(%22saving state%22);%0A
     
@@ -1911,16 +1911,139 @@
 tream);%0A
+      List%3CTrie%3E triesList = new ArrayList%3CTrie%3E();%0A      triesList.add(firstNameTrie);%0A      triesList.add(lastNameTrie);%0A
       ob
@@ -2071,29 +2071,25 @@
 eObject(
-firstNameTrie
+triesList
 );%0A%0A    

put first and last name trie in list and then save to file