package com.heyu.api.data.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lz.mybatis.plugins.interceptor.utils.PNumberUtils;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@Slf4j
public class MapUtils {


    public static <T> T getByExpr(Map<String, Object> map, String expression) {
        Object value = null;
        try {
            String expressions[] = expression.split("\\.");
            value = null;
            for (int i = 0; i < expressions.length; i++) {
                String expr = expressions[i];
                if (expr.startsWith("[")) {
                    if (value != null) {
                        Integer exprVal = NumberUtil.objToIntDefault(expr.substring(1, expr.length() - 1), -1);
                        if (exprVal == -1) {
                            return null;
                        }

                        List<Object> list = JSONObject.parseArray(value.toString(), Object.class);
                        if (list.size() < exprVal || list.get(exprVal) == null) {
                            return null;
                        }

                        if (i < expressions.length - 1) {
                            map = JSONObject.parseObject(list.get(exprVal).toString(), Map.class);
                        } else {
                            value = list.get(exprVal);
                        }

                    }
                }
                Object object = map.get(expr);
                if (object != null) {
                    if (i < expressions.length - 1) {
                        if (expressions[i + 1].contains("[")) {
                            value = object;
                        } else {
                            map = JSONObject.parseObject(object.toString(), Map.class);
                        }
                    } else {
                        value = object;
                    }
                }

            }
        } catch (Exception e) {
            log.error("getByExpr error map:" + JSON.toJSONString(map) + ",expression：" + expression, e);
        }
        if(value == null){
            return null;
        }
        return (T)( value + "");
    }




    public static Object parseBasicTypeWrapper(Class<?> parameterType, Object value) {
        if (Number.class.isAssignableFrom(parameterType)) {
            if (parameterType == Integer.class || parameterType == int.class) {
                return PNumberUtils.objToIntDefault(value, null);
            } else if (parameterType == Short.class || parameterType == short.class) {
                return PNumberUtils.objToShortDefault(value, null);
            } else if (parameterType == Long.class || parameterType == long.class) {
                return PNumberUtils.objToLongDefault(value, null);
            } else if (parameterType == Float.class || parameterType == float.class) {
                return PNumberUtils.objToFloatDefault(value, null);
            } else if (parameterType == Double.class || parameterType == double.class) {
                return PNumberUtils.objToDoubleDefault(value, null);
            } else if (parameterType == Byte.class || parameterType == byte.class) {
                return PNumberUtils.objToByteDefault(value, null);
            }   else if (parameterType == BigDecimal.class) {
                return PNumberUtils.objToBigDecimalDefault(value, null);
            }
        } else if (parameterType == Boolean.class || parameterType == boolean.class) {
            return PNumberUtils.objToBooleanDefault(value, null);
        } else if (parameterType == Character.class || parameterType == char.class) {
            return PNumberUtils.objToCharacterDefault(value, null);
        } else if (parameterType == String.class) {
            return value !=null ? value.toString() : null ;
        }else if (parameterType == Date.class && value instanceof Date){
            return value;
        } else if (parameterType == Date.class && value instanceof String) {
            Object target = parseStr2Date((String) value, YYYY_MM_DD_HH_MM_SS);
            if (target == null) {
                target = parseStr2Date((String) value, YYYY_MM_DD);
                if(target == null){
                    target = parseStr2Date((String) value, YYYY);
                    if(target == null){
                        target = parseStr2Date((String) value, YYYY_MM);
                        if(target == null){
                            target = parseStr2Date((String) value, YYYY_MM_DD_HH);
                            if(target == null){
                                target = parseStr2Date((String) value, YYYY_MM_DD_HH_MM);
                            }
                        }
                    }
                }
            }
            return target;
        }
        return null;
    }

    public static final Date parseStr2Date(String value, final String format) {
        if (Objects.isNull(value)) {
            return null;
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(value);
        } catch (ParseException e) {

        }
        return null;
    }

    public static String YYYY = "yyyy";
    public static String YYYY_MM = "yyyy-MM";
    public static String YYYY_MM_DD = "yyyy-MM-dd";
    public static String YYYY_MM_DD_HH = "yyyy-MM-dd HH";
    public static String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";


    public static String upperCaseFirst(String v) {
        String a = v.substring(0, 1).toUpperCase();
        String b = v.substring(1);
        return a + b;
    }

    public static boolean isUpperCase(char c) {
        if (c >= 'A' && c <= 'Z') {
            return true;
        }
        return false;
    }



    public static void main(String[] args) {
        String a = "{\n" +
                "    \"words_result\": {\n" +
                "        \"账号\": {\n" +
                "            \"word\": [\n" +
                "                \"254653912299\"\n" +
                "            ]\n" +
                "        },\n" +
                "        \"公司名称\": {\n" +
                "            \"word\": [\n" +
                "                \"河南专开机械设备有限公司\"\n" +
                "            ]\n" +
                "        },\n" +
                "        \"核准号\": {\n" +
                "            \"word\": [\n" +
                "                \"J4910057237101\"\n" +
                "            ]\n" +
                "        },\n" +
                "        \"法人\": {\n" +
                "            \"word\": [\n" +
                "                \"郭娟\"\n" +
                "            ]\n" +
                "        },\n" +
                "        \"编号\": {\n" +
                "            \"word\": [\n" +
                "                \"4910-02691732\"\n" +
                "            ]\n" +
                "        },\n" +
                "        \"开户银行\": {\n" +
                "            \"word\": [\n" +
                "                \"中国银行股份有限公司郑州高新技术开发区支行\"\n" +
                "            ]\n" +
                "        }\n" +
                "    },\n" +
                "    \"words_result_num\": 6,\n" +
                "    \"log_id\": 1754840007553369396\n" +
                "}";

        Map data = JSONObject.parseObject(a, Map.class);
        System.out.println(getByExpr(data, "words_result.账号.word.[0]"));
        System.out.println(getByExpr(data, "words_result.公司名称.word.[0]"));
        System.out.println(getByExpr(data, "words_result.核准号.word.[0]"));
        System.out.println(getByExpr(data, "words_result.法人.word.[0]"));
        System.out.println(getByExpr(data, "words_result.编号.word.[0]"));
        System.out.println(getByExpr(data, "words_result.开户银行.word.[0]"));
    }
}
