package com.ruoyi.ucp.service;

import com.ruoyi.ucp.util.AdapterUtil;
import org.springframework.stereotype.Service;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class TestService {
    public static void main(String[] args) {
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
        String k = "var point = \"NEM_SXJSFW.SX_XXXX_XXX_X_XX_XX_XXXX_FCC_CI134\";\n" +
                "var startTs = 1706166626000;\n" +
                "var endTs = 1706168006283;\n" +
                "var interval = 30;\n" +
                "var result = getHistorySnap(point, startTs, endTs, interval);";
        String s = "function getHistorySnap(point, startTs, endTs, interval) {\n" +
                "    var result = adapterUtil.getHistorySnap(point, startTs, endTs, interval);\n" +
                "    return result;\n" +
                "}";
        try {
            AdapterUtil adapterUtil = new AdapterUtil();
            engine.put("adapterUtil", adapterUtil);
            engine.eval(s);
            engine.eval(k);
            Object eval = engine.eval("result");
            //String o = convertObjectToString(eval, "string");
            System.out.println();
        } catch (ScriptException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> T convertObjectToString(Object object, String typeStr) {
        if (object == null) return null;
        Type type = null;
        if ("int".equals(typeStr)) {
            type = Integer.class;
        } else if ("long".equals(typeStr)) {
            type = Long.class;
        } else if ("float".equals(typeStr)) {
            type = Float.class;
        } else if ("double".equals(typeStr)) {
            type = Double.class;
        } else if ("boolean".equals(typeStr)) {
            type = Boolean.class;
        } else if ("date".equals(typeStr)) {
            type = Date.class;
        } else if ("string".equals(typeStr)) {
            type = String.class;
        }
        try {
            if (type != null) {
                if (type == Integer.class) {
                    return (T) Integer.valueOf((int) object);
                } else if (type == Long.class) {
                    return (T) Long.valueOf((long) object);
                } else if (type == Float.class) {
                    return (T) Float.valueOf((float) object);
                } else if (type == Double.class) {
                    return (T) Double.valueOf((double) object);
                } else if (type == Boolean.class) {
                    return (T) Boolean.valueOf((boolean) object);
                } else if (type == Date.class) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    return (T) sdf.format((Date) object);
                } else if (type == String.class) {
                    return (T) String.valueOf(object);
                } else {
                    System.out.println("Invalid type: " + type);
                }
            } else {
                System.out.println("Invalid type: " + type);
            }
        } catch (ClassCastException e) {
            e.printStackTrace();
        }
        return (T) object;
    }
}
