package cn.ysk.cashier.utils;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.poi.ss.formula.functions.T;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author lyf
 */
public class ListUtil {
    private static ObjectMapper objectMapper = new ObjectMapper();

    public static JSONArray stringChangeList(String listString){
        // 使用Fastjson将JSON字符串转换为JSONArray对象
        return JSON.parseArray(listString);
    }

    public static JSONObject stringToJSONObject(String str){
        return JSONObject.parseObject(str);
    }
    public static <T> T stringToJson(String json, Class<T> valueType) {
        try {
            return objectMapper.readValue(json, valueType);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static String toString(Object json){
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.writeValueAsString(json);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static <T> List<?> toList(String json,List<T> list){
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.readValue(json,list.getClass());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String JSONArrayChangeString(JSONArray jsonArray){
        return jsonArray.toString();
    }

    public static List<String> stringChangeStringList(String listString){
        if (StringUtils.isEmpty(listString)){
            return new ArrayList<>();
        }
        // 使用Fastjson将JSON字符串转换为JSONArray对象
        listString = listString.replaceAll("\"", "").replaceAll("\\[", "").replaceAll("\\]", "");
        List<String> stringList = Stream.of(listString.split(","))
                .map(String::trim)
                .collect(Collectors.toList());
        return stringList;
    }

    public static List<Integer> stringChangeIntegerList(String listString){
        // 使用Fastjson将JSON字符串转换为JSONArray对象
        listString = listString.replaceAll("\"", "").replaceAll("\\[", "").replaceAll("\\]", "");
        if (StringUtils.isEmpty(listString)) {
            return new ArrayList<>();
        }
        List<Integer> integerList = Stream.of(listString.split(","))
                .map(Integer::valueOf)
                .collect(Collectors.toList());
        return integerList;
    }


    public static List<Integer> stringChangeIntegerList(List<String> listString){

        // method1 创建一个Integer类型的集合，循环遍历String类型的数组并把数据添加进集合
        List<Integer> integerList = new ArrayList<>();
        for (String s : listString) {
            if (!StringUtils.isEmpty(s)) {
                integerList.add(Integer.parseInt(s));
            }
        }
        return integerList;
    }


    public static<T> String listChangeString(List<T> listString){
        return listString.stream()
                .map(Object::toString)
                .collect(Collectors.joining(", "));
    }

    public static String mapToString(Map<String, String> map) {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
        }
        if (!map.isEmpty()) {
            sb.deleteCharAt(sb.length() - 1); // 删除最后一个"&"字符
        }
        return sb.toString();
    }
}
