package com.td.server;

import com.td.bktree.BKTree;
import org.json.JSONException;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class SpellCheckService extends HttpServlet {
    private ConcurrentHashMap<Long, BKTree> spellCheckMap = new ConcurrentHashMap<Long, BKTree>();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);

        Map<String, String[]> params = request.getParameterMap();
        if (!params.containsKey("id")) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getWriter().println("param id not found");
            return;
        }
        if (!params.containsKey("word")) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getWriter().println("param word not found");
            return;
        }

        if (spellCheckMap.containsKey(params.get("id")[0])) {
            long id = 0;
            try {
                id = Long.getLong(params.get("id")[0]);
            } catch (Exception e) {
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                response.getWriter().println("id not type long");
            }
            int editDistance = 1;
            if (params.containsKey("edit_distance")) {
                try {
                    int value = Integer.getInteger(params.get("edit_distance")[0]);
                    editDistance = value > 0 ? value : 1;
                } catch (Exception e) {

                }
            }
            if (spellCheckMap.contains(id)) {
                List<String> results = spellCheckMap.get(id).search(params.get("word")[0], editDistance);
                try {
                    response.getWriter().println(new Response(results).toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                    response.getWriter().println("Something is wrong with the service right now...");
                }
            } else {
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                response.getWriter().println("id not found");
            }

        } else {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getWriter().println("id not found");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);

        Map<String, String[]> params = request.getParameterMap();
        if (!params.containsKey("words")) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getWriter().println("param words not found");
            return;
        }

        if (params.containsKey("id")) {
            long id = 0;
            try {
                id = Long.getLong(params.get("id")[0]);
            } catch (Exception e) {
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                response.getWriter().println("id not type long");
            }
            if (spellCheckMap.contains(id)) {
                BKTree bkTree = spellCheckMap.get(id);
                bkTree.add(Arrays.asList(params.get("words")));
                response.getWriter().println("new words added");
            } else {
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                response.getWriter().println("id not found");
            }
        } else {
            long id = new Date().getTime();
            BKTree bkTree = new BKTree();
            bkTree.add(Arrays.asList(params.get("words")));
            JSONObject object = new JSONObject();
            try {
                object.put("id", id);
                response.getWriter().println(object.toString());
            } catch (JSONException e) {
                e.printStackTrace();
                response.getWriter().println("Something is wrong with the service right now...");
            }
        }
    }
}

@@ -1245,80 +1245,17 @@
  %7D%0A%0A
-        if (spellCheckMap.containsKey(params.get(%22id%22)%5B0%5D)) %7B%0A  
+%0A
         
   lo
@@ -1242,34 +1242,32 @@
     %7D%0A%0A%0A        
-  
 long id = 0;%0A   
@@ -1267,38 +1267,30 @@
  0;%0A        
-    
 try %7B%0A
-    
             
@@ -1291,35 +1291,37 @@
       id = Long.
-get
+parse
 Long(params.get(
@@ -1323,36 +1323,32 @@
 .get(%22id%22)%5B0%5D);%0A
-    
         %7D catch 
@@ -1367,36 +1367,32 @@
 ) %7B%0A            
-    
 response.setStat
@@ -1435,36 +1435,32 @@
 T);%0A            
-    
 response.getWrit
@@ -1497,34 +1497,26 @@
 %22);%0A        
-   
- 
 %7D%0A
-    
         int 
@@ -1529,28 +1529,24 @@
 stance = 1;%0A
-    
         if (
@@ -1592,38 +1592,30 @@
             
- 
-   
 try %7B%0A
-    
             
@@ -1674,36 +1674,32 @@
 distance%22)%5B0%5D);%0A
-    
                 
@@ -1740,36 +1740,32 @@
  1;%0A            
-    
 %7D catch (Excepti
@@ -1789,32 +1789,20 @@
     
-    %7D%0A            
 %7D%0A    
+    %7D%0A
     
@@ -1827,32 +1827,35 @@
 Map.contains
+Key
 (id)) %7B%0A
             
@@ -1834,36 +1834,32 @@
 tainsKey(id)) %7B%0A
-    
             List
@@ -1952,38 +1952,30 @@
             
-    
 try %7B%0A
-    
             
@@ -2046,36 +2046,32 @@
 ));%0A            
-    
 %7D catch (JSONExc
@@ -2090,36 +2090,32 @@
                 
-    
 e.printStackTrac
@@ -2111,36 +2111,32 @@
 ntStackTrace();%0A
-    
                 
@@ -2221,181 +2221,8 @@
 %22);%0A
-                %7D%0A            %7D else %7B%0A                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);%0A                response.getWriter().println(%22id not found%22);%0A
     
@@ -2223,33 +2223,32 @@
 ;%0A            %7D%0A
-%0A
         %7D else %7B
@@ -2380,24 +2380,25 @@
 ;%0A        %7D%0A
+%0A
     %7D%0A%0A    @
@@ -3784,32 +3784,46 @@
 ams.get(%22words%22)
+%5B0%5D.split(%22,%22)
 ));%0A            
@@ -3973,32 +3973,79 @@
 ct.toString());%0A
+                spellCheckMap.put(id, bkTree);%0A
             %7D ca

fix some problems in spellcheck service