let fs = require("fs")
let {popen} = require("./popen.node")
//taken from another project
let langs = [
  {
    "names": ["javascript", "nodejs", "node", "js"],
    "default": null,
    "match": null,
    "extension": "js",
    "compile": null,
    "run": "node {{file}}"
  },
  {
    "names": ["cpp", "c++"],
    "default": "int main(int argc, char **argv) {\n{{script}}\nreturn 0;}",
    "match": "main(",
    "headers": "#",
    "extension": "cpp",
    "compile": "g++ {{file}} -o {{output}}",
    "run": "{{output}}"
  },
  {
    "names": ["python", "py"],
    "default": null,
    "match": null,
    "headers": "import",
    "extension": "py",
    "compile": null,
    "run": "python3 {{file}}"
  },
  {
    "names": ["haskell", "hs"],
    "default": null,
    "match": null,
    "headers": null,
    "extension": "hs",
    "compile": "ghc {{file}} -o {{output}} 1>/dev/null",
    "run": "{{output}}"
  },
  {
    "names": ["java"],
    "headers": "import",
    "match":" public static void main(String[] args) {",
    "default": "import java.io.*;\npublic class temp\n{\n  public static void main(String[] args) {\n{{script}}\n}\n}",
    "extension": "java",
    "compile": "javac -d temp {{file}}",
    "run": "cd temp && java temp"
  },
  {
    "names": ["ruby", "rb"],
    "default": null,
    "match": null,
    "headers": "require",
    "extension": "rb",
    "compile": null,
    "run": "ruby {{file}}"
  },
  {
    "names": ["swift"],
    "default": "import Foundation\n{{script}}",
    "match": "import Foundation",
    "headers": "import",
    "extension": "swift",
    "compile": "swiftc {{file}} -o {{output}} 1>/dev/null",
    "run": "{{output}}"
  },
  {
    "names": ["c"],
    "default": "int main(int argc, char **argv) {\n{{script}}\nreturn 0;}",
    "match": "main(",
    "headers": "#",
    "extension": "c",
    "compile": "gcc {{file}} -o {{output}}",
    "run": "{{output}}"
  },
  {
    "names": ["c#", "cs"],
    "headers": "using",
    "match":"void Main() {",
    "default": "using System;\npublic class temp{\npublic static void Main(){\n{{script}}\n}\n}",
    "extension": "cs",
    "compile": "mcs -out:{{output}} {{file}}",
    "run": "mono {{output}}"
  },
  {
    "names": ["perl", "pl"],
    "default": null,
    "match": null,
    "headers": "use",
    "extension": "pl",
    "compile": null,
    "run": "perl {{file}}"
  },
  {
    "names": ["rust", "rs"],
    "default": "fn main() {\n{{script}}\n}",
    "match": "fn main()",
    "headers": "using",
    "extension": "rs",
    "compile": "rustc {{file}} -o {{output}}",
    "run": "{{output}}"
  },
  {
    "names": ["lua"],
    "default": null,
    "match": null,
    "headers": "require",
    "extension": "lua",
    "compile": null,
    "run": "lua {{file}}"
  },
  {
    "names": ["bash", "sh"],
    "default": null,
    "match": null,
    "headers": null,
    "extension": "sh",
    "compile": null,
    "run": "bash {{file}}"
  },
  {
    "names": ["05ab1e", "osabie"],
    "default": null,
    "match": null,
    "headers": null,
    "extension": "abe",
    "compile": null,
    "run": "./languages/osabie {{file}} | tail -2"
  },
  {
    "names": ["elixir", "ex", "exs"],
    "default": null,
    "match": null,
    "headers": null,
    "extension": "ex",
    "compile": null,
    "run": "elixir {{file}} | tail -2"
  },
  {
    "names": ["kagl"],
    "default": null,
    "match": null,
    "headers": null,
    "extension": "kagl",
    "compile": null,
    "run": "./languages/kagl {{file}}"
  },
  {
    "names": ["d"],
    "default": "void main() {\n{{script}}\n}",
    "match": "main(",
    "headers": "import",
    "extension": "d",
    "compile": "gdc {{file}} -o {{output}}",
    "run": "{{output}}"
  },
  {
    "names": ["go"],
    "default": "func main() {\n{{script}}\n}",
    "match": "func main(",
    "headers": "import",
    "extension": "go",
    "compile": "bash ./languages/go.sh {{file}} {{output}}",
    "run": "{{output}}"
  },
  {
    "names": ["kotlin", "kt"],
    "default": "fun main(args: Array<String>) {\n{{script}}\n}",
    "match": "fun main(",
    "headers": null,
    "extension": "kt",
    "compile": "kotlinc {{file}} -include-runtime -d {{output}}.jar",
    "run": "java -jar {{output}}.jar"
  },
  {
  "names": ["><>", "fish"],
  "default":null,
  "headers": null,
  "match": null,
  "extension": "fish",
  "compile": null,
  "run": "python3 ./languages/fish {{file}}"
  },
  {
    "names":["golfscript"],
    "extension": "golfscript",
    "run": "ruby ./languages/golfscript.rb {{file}}"
  }
]
;
exports.langs = langs;
exports.search = (query) => {
  for (i in langs) {
    if (langs[i].names.includes(query)) return langs[i]
  }
  return false
}
exports.run = (lang, code, message, _stdout) => {
message.channel.send(`Compiling${lang.names.includes("kt")? " (Kotlin may take a while) " : ""}<a:spinny:822587419449622539>`).then(m=>{
    let _c = runCode(code, lang.names[0]);
    m.edit("", {embed:_stdout(_c, false)});
    // return _stdout(_c);
  
})
}


function runCode(code, lang) {
  let _check = false;
  for(l in langs) { if(langs[l].names.includes(lang)) _check = l; }
  if(!_check) return "Invalid lang";
  
let lang_to_run = langs[_check];

if(lang_to_run.default && !code.includes(lang_to_run.match)) {
  code = lang_to_run.default.replace("{{script}}", code)
}
if(lang_to_run.headers) {
  code = code.split("\n");
  let headers = [];
  let _code = [];
  code.forEach(line=>{
    if(line.startsWith(lang_to_run.headers)) {
      headers.push(line);
    }
    else {
      _code.push(line);
    }
  })
  code = headers.join("\n") + "\n" + _code.join("\n");

}
fs.writeFileSync(__dirname + "/../temp/temp." + lang_to_run.extension, code);
let output = "";
if(lang_to_run.compile) {
  let compiler_output = popen(lang_to_run.compile.replace("{{file}}", __dirname + "/../temp/temp." + lang_to_run.extension).replace("{{output}}", __dirname + "/../temp/exe_" + lang_to_run.extension));
  compiler_output = compiler_output.split("\n").filter(e=>!e.startsWith("/nix/store/")).join("\n");
  output += compiler_output;
  popen("chmod 777 ../temp/exe_" + lang_to_run.extension)
}
output += popen(lang_to_run.run.replace("{{file}}", __dirname + "/../temp/temp." + lang_to_run.extension).replace("{{output}}", __dirname + "/../temp/exe_" + lang_to_run.extension));
return output;
}