package xyz.jadonfowler.o;

import static spark.Spark.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArrayList;

public class WebIDE {
    public static void main(String[] a) {
        try {
            int port = Integer.valueOf(System.getenv("PORT"));
            System.out.println("Binding to port " + port);
            port(port);
        }
        catch (Exception e) {
            int port = 80;
            System.out.println("Binding to port " + port);
            port(port);
        }
        O instance = new O();
        O.instance = instance;
        instance.webIDE = true;
        get("/", (req, res) -> {
            return readFile("res/index.html");
        });
        post("/", (req, res) -> {
            instance.stack = new Stack(32 * 1024);
            instance.variables = new CopyOnWriteArrayList<Variable>();
            String code = req.queryParams("code");
            String input = req.queryParams("input");
            instance.inputs = input.split("\n");
            instance.inputPointer = 0;
            String s = "";
            for (char c : code.toCharArray()) {
                s += instance.parse(c);
            }
            s = s.replace("\n", "<br/>");
            String f = readFile("res/code.html");
            f = f.replace("${INPUT}", input);
            f = f.replace("${CODE}", code);
            f = f.replace("${OUTPUT}", s);
            f = f.replace("${STACK}", instance.stack.toString());
            return f;
        });
        get("/link/:code", (req, res) -> {
//            String r = req.params(":code");
//            String code = r.split("&")[0].split("=")[1];
//            String input = r.split("&")[1].split("=")[1];
            return readFile("res/link.html");
        });
        exception(Exception.class, (e, req, res) -> {
            String code = req.queryParams("code");
            String input = req.queryParams("input");
            String error = e.getMessage() + "<br>";
            error += e.getStackTrace()[0].toString();
            String f = "";
            try {
                f = readFile("res/error.html");
            }
            catch (Exception e1) {}
            f = f.replace("${INPUT}", input);
            f = f.replace("${CODE}", code);
            f = f.replace("${ERROR}", error);
            res.body(f);
        });
    }

    public static String readFile(String path) throws IOException {
        FileReader fileReader = new FileReader(new File(path));
        BufferedReader br = new BufferedReader(fileReader);
        String all = "";
        String line = null;
        while ((line = br.readLine()) != null)
            all += line;
        br.close();
        fileReader.close();
        return all;
    }
}
@@ -1649,16 +1649,18 @@
 nk/:code
+/*
 %22, (req,
@@ -1671,18 +1671,16 @@
 ) -%3E %7B%0D%0A
-//
         
@@ -1687,111 +1687,51 @@
     
-String r = req.params(%22:code%22);%0D%0A//            String code = r.split(%22&%22)%5B0%5D.split(%22=%22)%5B1%5D
+return readFile(%22res/link.html%22)
 ;%0D%0A
-//
         
     
@@ -1730,57 +1730,55 @@
     
-    String input = r.split(%22&%22)%5B1%5D.split(%22=%22)%5B1%5D;
+%7D);%0D%0A        get(%22/link/:code%22, (req, res) -%3E %7B
 %0D%0A  

fix forward slashes in code