package com.ruoyi.web.core.config;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ruoyi.common.core.domain.BaseEntity;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

public final class SwaggerDocUtils
{

    public static String ConvJSTypeName(Class<?> clazz)
    {
        if(CharSequence.class.isAssignableFrom(clazz))
            return "string";
        if(Map.class.isAssignableFrom(clazz))
            return "object";
        if(Collection.class.isAssignableFrom(clazz))
            return "Array";
        if(Float.class.equals(clazz)
                || float.class.equals(clazz)
                || Double.class.equals(clazz)
                || double.class.equals(clazz)
                || BigDecimal.class.equals(clazz)
        )
            return "number(浮点型)";
        if(Integer.class.equals(clazz)
                || int.class.equals(clazz)
                || Long.class.equals(clazz)
                || long.class.equals(clazz)
                || Short.class.equals(clazz)
                || short.class.equals(clazz)
                || Byte.class.equals(clazz)
                || byte.class.equals(clazz)
                || BigInteger.class.equals(clazz)
        )
            return "number(整型)";
        if(Boolean.class.equals(clazz)
                || boolean.class.equals(clazz)
        )
            return "boolean";
        if(clazz.isArray())
            return "Array";
        if(BaseEntity.class.isAssignableFrom(clazz))
            return "object(业务实体对象 " + clazz.getSimpleName() + ")";
        if(Date.class.isAssignableFrom(clazz))
            return "Date";
        if(Number.class.isAssignableFrom(clazz))
            return "number";
        if(MultipartFile.class.equals(clazz))
            return "File";
        return "object(" + clazz.getSimpleName() + ")";
    }

    public static boolean IgnoreClass(Class<?> clazz)
    {
        if(null == clazz)
            return true;
        if(Object.class.equals(clazz)
                || boolean.class.equals(clazz)
                || byte.class.equals(clazz)
                || char.class.equals(clazz)
                || long.class.equals(clazz)
                || short.class.equals(clazz)
                || int.class.equals(clazz)
                || float.class.equals(clazz)
                || double.class.equals(clazz)
        )
            return true;
        Package p = clazz.getPackage();
        if(null != p && p.getName().startsWith("java"))
            return true;
        return false;
    }

    public static boolean IgnoreField(Field field)
    {
        int modifiers = field.getModifiers();
        if(Modifier.isStatic(modifiers))
            return true; // 排除静态变量

        if(null != field.getAnnotation(JsonIgnore.class))
            return true; // 忽略

        return false;
    }

    public static String ToVisualJson(Object obj)
    {
        return JSON.toJSONString(obj,
                JSONWriter.Feature.WriteNulls, // 包含null字段
                JSONWriter.Feature.PrettyFormat // 可读格式
        )
                ;
    }

    private SwaggerDocUtils() {}
}
