package org.jeecg.modules.bytedance.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.jeecg.modules.bytedance.advertise.vo.IndustryVo;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Slf4j
public class Check {
    private Check() {
    }

    public static boolean isNull(List<?> list) {
        return list == null || list.isEmpty();
    }

    public static boolean isNull(Long l) {
        return l == null || l == 0;
    }

    public static boolean isNull(Collection<?> collection) {
        return collection == null || collection.isEmpty();
    }

    public static boolean isNull(JSONArray jsonArray) {
        return jsonArray == null || jsonArray.isEmpty();
    }


    public static boolean isNull(String str) {
        return str == null || str.isEmpty();
    }

    public static boolean isNull(byte[] bytes) {
        return bytes == null || bytes.length == 0;
    }

    public static boolean hasNull(Object... objects) {
        for (Object obj : objects) {
            if (obj == null) {
                return true;
            }
        }
        return false;
    }

    public static boolean allEmpty(String... strs) {
        for (String str : strs) {
            if (StringUtils.isNotEmpty(str)) {
                return false;
            }
        }
        return true;
    }

    public static boolean isNull(Object object) {
        return object == null;
    }

    public static boolean isNullMap(Map<?, ?> map) {
        return map == null || map.isEmpty();
    }


    /**
     * 移除 JSONObject 中 value 为空的 key
     * @param
     * @return
     */
    public static JSONObject jsonRemoveEmpty(JSONObject jsonObject){
        JSONObject jsonResult = new JSONObject();
        for (Object key :jsonObject.keySet() ) {
            if (!Check.isNull(jsonObject.get(key)) && !jsonObject.get(key).equals("")){
                jsonResult.put(key.toString(), jsonObject.get(key));
            }
        }
        return jsonResult;
    }



    /**
     * 将 Map对象转化为JavaBean
     * @author loulan
     * @param map
     * @return Object对象
     */
    public static <T> T convertMap2Bean(Map map, Class T) throws Exception {
        if(map==null || map.size()==0){
            return null;
        }
        BeanInfo beanInfo = Introspector.getBeanInfo(T);
        T bean = (T)T.newInstance();
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (int i = 0, n = propertyDescriptors.length; i <n ; i++) {
            PropertyDescriptor descriptor = propertyDescriptors[i];
            String propertyName = descriptor.getName();
            //将字母大写，这里先注掉，因为本例子直接读取名字匹配即可。
            //String upperPropertyName = propertyName.toUpperCase();
            if (map.containsKey(propertyName)) {
                Object value = map.get(propertyName);
                //这个方法不会报参数类型不匹配的错误。
                BeanUtils.copyProperty(bean, propertyName, value);
                //用这个方法对int等类型会报参数类型不匹配错误，需要我们手动判断类型进行转换，比较麻烦。
                //descriptor.getWriteMethod().invoke(bean, value);
                //用这个方法对时间等类型会报参数类型不匹配错误，也需要我们手动判断类型进行转换，比较麻烦。
                //BeanUtils.setProperty(bean, propertyName, value);
            }
        }
        return bean;
    }

    /**
     * 将 List<Map>对象转化为List<JavaBean>
     * @author loulan

     * @return Object对象
     */
    public static <T> List<T> convertListMap2ListBean(List<Map<String,Object>> listMap, Class T) throws Exception {
        List<T> beanList = new ArrayList<T>();
        for(int i=0, n=listMap.size(); i<n; i++){
            Map<String,Object> map = listMap.get(i);
            T bean = convertMap2Bean(map,T);
            beanList.add(bean);
        }
        return beanList;
    }


    public static void main(String[] args) {
        JSONObject params = new JSONObject();
        params.put("advertiser_id", "123");
        params.put("campaign_id", "123");
        params.put("modify_time", null);
        System.out.println(jsonRemoveEmpty(params));
    }



}
