package cn.fateverse.query;

import cn.fateverse.common.code.config.JavaCodeProperties;
import cn.fateverse.common.code.engine.JavaCodeEngine;
import cn.fateverse.common.code.engine.JavaScriptEngine;
import cn.fateverse.common.code.model.EngineResult;

import java.util.concurrent.FutureTask;


/**
 * @author Clay
 * @date 2024/4/22 16:04
 */
public class JavaScriptTest {
    public static void main(String[] args) throws Exception {
        String code = "function execute(data) {\n" +
                "    console.log(data)\n" +
                "    console.log(\"test console!\")\n" +
                "    let add = (a, b) => a + b; // 使用箭头函数\n" +
                "    console.log(\"Adding 5 and 3:\", add(5, 3));\n\n" +
                "    for (let i = 0; i < 1000; i++) {\n" +
                "        console.log(i)\n" +
                "    }" +
                "    return \"success\";\n" +
                "}";


        JavaCodeProperties properties = new JavaCodeProperties();
        properties.setClassPath("C:\\home\\clay\\code\\");
        JavaCodeEngine javaCodeEngine = new JavaCodeEngine(properties);


//        EngineResult<Object> executeScript = JavaScriptEngine.executeScript(code, "execute", "测试控制台输出");

        String finalCode = "function execute(data) {\n" +
                "    console.log(data)\n" +
                "    console.log(\"test console!\")\n" +
                "    let add = (a, b) => a + b; // 使用箭头函数\n" +
                "    console.log(\"Adding 5 and 3:\", add(5, 3));\n\n" +
                "    for (let i = 0; i < 1000; i++) {\n" +
                "        console.log(i)\n" +
                "    }" +
                "    return \"success\";\n" +
                "}";
        FutureTask<EngineResult> engineResultFutureTask = new FutureTask<>(() -> {
            return JavaScriptEngine.execute(finalCode, "execute", false, "测试控制台输出");
        });


        String finalCode1 = "import java.util.*;\n" +
                "import java.util.stream.*;\n" +
                "\n" +
                "public class DataAdapter100 {" +
                "\n" +
                "        public static Object execute(String data) {\n" +
                "        for (int i = 0; i < 1000; i++) {\n" +
                "            System.out.println(i);\n" +
                "        }\n" +
                "        return \"qwewe\";\n" +
                "    }" +
                "}\n";
        FutureTask<EngineResult> engineResultFutureTask1 = new FutureTask<>(() -> {
            return javaCodeEngine.execute(finalCode1, "DataAdapter100", "execute", new Class[]{String.class}, new Object[]{"test"}, true);
        });
        new Thread(engineResultFutureTask).start();
        new Thread(engineResultFutureTask1).start();
//        EngineResult<Object> execute = javaCodeEngine.execute(code, "DataAdapter100", "execute", new Class[]{String.class}, new Object[]{"test"}, true);


        EngineResult x = engineResultFutureTask.get();
        EngineResult x1 = engineResultFutureTask1.get();
        System.out.println(x);
        System.out.println(x1);


//        EngineResult<Object> capture = MultiThreadedCapture.capture(() -> {
//            ScriptEngine engine = new ScriptEngineManager().getEngineByName("graal.js");
//            Invocable inv = (Invocable) engine;
//            try {
//                engine.eval(finalCode);
//                return inv.invokeFunction("execute", "args");
//            } catch (ScriptException | NoSuchMethodException e) {
//                throw new RuntimeException(e);
//            }
//        });
//        System.out.println(capture);
//        EngineResult<Object> objectEngineResult = JavaScriptEngine.executeScript(finalCode, "execute", "测试控制台输出");
//
//        ByteArrayOutputStream baos = new ByteArrayOutputStream();
//        PrintStream oldOut = System.out;
//        System.setOut(new PrintStream(baos));


//        EngineResult<Object> capture = MultiThreadedCapture.capture(() -> {
//            int someNumber = 42;
//            System.out.println(someNumber);
//            return null;
//        });


        // 执行产生数值输出的代码
//        System.out.println(objectEngineResult);
//        ScriptEngine engine = JavaScriptEngine.getEngine();
//        Invocable inv = (Invocable) engine;
//        engine.eval(finalCode);
//        inv.invokeFunction("execute", "args");
//        System.out.println(capture);

//        MultiThreadedCapture.executor.shutdown();
        // 恢复标准输出流
//        System.setOut(oldOut);
//
//        System.setOut(oldOut);
//        // 从捕获的字节数组输出流中获取打印的文本
//        String capturedOutput = baos.toString();
//        System.out.println("Captured output: " + capturedOutput);

    }


}
