import java.sql.*;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Map;
import java.net.URI;
import java.net.URISyntaxException;


import static spark.Spark.*;
import spark.template.freemarker.FreeMarkerEngine;
import spark.ModelAndView;
import static spark.Spark.get;


import com.heroku.sdk.jdbc.DatabaseUrl;


import edu.pitt.siying.servertest.DeleteStringSolution;
import freemarker.core.ReturnInstruction.Return;
import static javax.measure.unit.SI.KILOGRAM;


import javax.measure.quantity.Mass;


import javolution.util.Index;


import org.jscience.physics.model.RelativisticModel;
import org.jscience.physics.amount.Amount;

public class Main {

  public static void main(String[] args) {

    port(Integer.valueOf(System.getenv("PORT")));
    staticFileLocation("/public");

    //main page 
    get("/", (request, response) -> {
            Map<String, Object> attributes = new HashMap<>();
            attributes.put("message", "Wordrumb - Help you memorize unfamiliar words!");

            return new ModelAndView(attributes, "index.ftl");
        }, new FreeMarkerEngine());
    
    //login page
    get("/login", (req, res) -> {
    	Map<String, Object> attributes = new HashMap<>();
      attributes.put("message", "Wordrumb - Login/out");

    	return new ModelAndView(attributes, "login.ftl");
    }, new FreeMarkerEngine());
    
    //login action page
    get("/login_action", (req, res) -> {
      Map<String, Object> attributes = new HashMap<>();
      String login_username = req.queryParams("login_username");
      String login_password = req.queryParams("login_password");

      attributes.put("message", "login action test page.");
      attributes.put("login_username", login_username);
      attributes.put("login_password", login_password);

      return new ModelAndView(attributes, "login_action.ftl");
    }, new FreeMarkerEngine());

    //Task 1: Delete the substring
    DeleteStringSolution solution = new DeleteStringSolution();
    get("/task1", (req, res) -> {
    	Map<String, Object> attributes = new HashMap<>();
    	attributes.put("message", "To find out how many substrings there.");
      
    	return new ModelAndView(attributes, "task1.ftl");
    }, new FreeMarkerEngine());
    
    get("/task1_workout", (req, res) -> {
      Map<String, Object> attributes = new HashMap<>();
      String string1 = req.queryParams("string1");
      String substring = req.queryParams("substring");

      int count = solution.deleteSubstring(string1, substring);

      attributes.put("string1", string1);
      attributes.put("count", count);
      attributes.put("substring", substring);

      return new ModelAndView(attributes, "task1_workout.ftl");
    }, new FreeMarkerEngine());

    //Database operation
    get("/db", (req, res) -> {
      Connection connection = null;
      Map<String, Object> attributes = new HashMap<>();
      try {
        connection = DatabaseUrl.extract().getConnection();

        Statement stmt = connection.createStatement();
        stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)");
        stmt.executeUpdate("INSERT INTO ticks VALUES (now())");
        ResultSet rs = stmt.executeQuery("SELECT tick FROM ticks");

        ArrayList<String> output = new ArrayList<String>();
        while (rs.next()) {
          output.add( "Read from DB: " + rs.getTimestamp("tick"));
        }

        attributes.put("results", output);
        return new ModelAndView(attributes, "db.ftl");
      } catch (Exception e) {
        attributes.put("message", "There was an error: " + e);
        return new ModelAndView(attributes, "error.ftl");
      } finally {
        if (connection != null) try{connection.close();} catch(SQLException e){}
      }
    }, new FreeMarkerEngine());

  }

}

@@ -1926,72 +1926,8 @@
 ing%0A
-    DeleteStringSolution solution = new DeleteStringSolution();%0A
     
@@ -1948,32 +1948,32 @@
 (req, res) -%3E %7B%0A
+
     %09Map%3CString,
@@ -2089,15 +2089,9 @@
 %22);%0A
-      
 %0A
+
     
@@ -2174,24 +2174,90 @@
 ne());%0A    %0A
+    //DeleteStringSolution solution = new DeleteStringSolution();%0A
     get(%22/ta
@@ -2445,24 +2445,24 @@
 bstring%22);%0A%0A
+
       int co
@@ -2471,17 +2471,8 @@
 t = 
-solution.
 dele
@@ -3779,10 +3779,557 @@
 ;%0A%0A  %7D%0A%0A
+  public static int deleteSubstring(String s, String t) %7B%0A    %0A    //The target string has no length%0A    if(t.length() == 0) %7B%0A      return 0;%0A    %7D%0A%0A    int count = 0;%0A    int preIndex = 0;%0A%0A    while(s.length()%3E=t.length()) %7B%0A      int index = s.indexOf(t, preIndex-t.length());%0A%0A      //no substring any more%0A      if(index == -1) %7B%0A        break;%0A      %7D else %7B%0A        //remove this substring%0A        s = s.substring(0, index) + s.substring(index+t.length());%0A        count ++;%0A        preIndex = index;%0A      %7D%0A    %7D%0A%0A    return count;%0A  %7D%0A%0A
 %7D%0A

test for task1