package com.ch.jedge.jbot;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.changhong.jedge.JMgbusUtil;
import com.changhong.qlib.intf.QIData;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

public class JedgeLlmUtil {
    public static QIData simpleOk() {
        return JMgbusUtil.MgbusResult(200,"ok");
    }

    public static QIData simpleFail(String formatStr, Object... vals) {
        return JMgbusUtil.MgbusResult(200,String.format(formatStr, vals));
    }

    public static String getFirstRandomKey(Set<String> keySet) {
        if(keySet!=null)
            for(String key : keySet)
                return key;
        return null;
    }

    public static String unescapeString(String input) {
        if (input == null) {
            return null;
        }

        StringBuilder result = new StringBuilder();
        int length = input.length();
        boolean isEscaped = false;

        for (int i = 0; i < length; i++) {
            char currentChar = input.charAt(i);

            if (isEscaped) {
                switch (currentChar) {
                    case 'n':
                        result.append('\n');
                        break;
                    case 'r':
                        result.append('\r');
                        break;
                    case 't':
                        result.append('\t');
                        break;
                    case 'b':
                        result.append('\b');
                        break;
                    case 'f':
                        result.append('\f');
                        break;
                    case '\'':
                        result.append('\'');
                        break;
                    case '\"':
                        result.append('\"');
                        break;
                    case '\\':
                        result.append('\\');
                        break;
                    default:
                        result.append(currentChar);
                        break;
                }
                isEscaped = false;
            } else {
                if (currentChar == '\\') {
                    isEscaped = true;
                } else {
                    result.append(currentChar);
                }
            }
        }

        return result.toString();
    }

    public static JSONArray JsonArrayFromStringArray(String[] split) {
        JSONArray re = new JSONArray();
        re.addAll(Arrays.asList(split));
        return re;
    }

    public static JSONArray JsonArrayFromJsonString(String initVal) {
        try {
            Object target = JSON.parse(initVal, Feature.OrderedField);
            if (target instanceof JSONObject) {
                JSONArray re = new JSONArray();
                re.add(target);
                return re;
            } else if(target instanceof JSONArray){
                return (JSONArray) target;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JSONArray();
    }

    public static JSONArray JsonArrayFromStringList(List<String> items) {
        JSONArray re = new JSONArray();
        re.addAll(items);
        return re;
    }

    public static String StringFromStringJSONArray(JSONArray ds) {
        if(ds!=null) {
            StringBuilder r=new StringBuilder();
            for(Object d : ds) {
                if(d instanceof String)
                    if(r.length()>0) r.append(",");
                r.append(d);
            }
            return r.toString();
        }
        return null;
    }
}
