package com.example.utils;

import org.json.JSONObject;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

public class Utils {
    /**
     * 判断字符串是否为空或null
     * @param txt
     *          字符串
     * @return
     *          true 空或null， false 非空
     */
    public static boolean isEmpty(String txt){
        if (null == txt){
            return true;
        }
        return "".equals(txt.trim());
    }

    /**
     * 获取文件扩展名
     * @param name
     *          文件名
     * @return
     *          扩展名
     */
    public static String getFileExt(String name, boolean... withPoint){
        if (isEmpty(name)){
            return "";
        }
        int i = name.lastIndexOf(".");
        if (i > 0){
            if (withPoint == null || withPoint.length == 0 || !withPoint[0]){
                return name.substring(i + 1);
            }else{
                return name.substring(i);
            }
        }
        return "";
    }

    /**
     * 创建文件夹
     * @param path
     *          文件夹路径
     * @return
     *          true 成功；false 失败
     */
    public static boolean mkdirs(String path){
        File f = new File(path);
        if (!f.exists()){
            f.mkdirs();
        }
        return true;
    }

    public static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
    public static SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public static SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
    public static SimpleDateFormat sdfGMT;

    public static <V> V getMapValue(Map map, Object key, V defaultValue) {
        if (map == null) {
            return defaultValue;
        } else {
            return map.containsKey(key) ? (V)map.get(key) : defaultValue;
        }
    }

    public static String toString(Object obj) {
        return obj != null && obj != JSONObject.NULL ? obj.toString() : "";
    }

    public static Boolean getBoolean(Object obj) {
        return getBoolean(obj, false);
    }

    public static Boolean getBoolean(Object obj, Boolean defaultValue) {
        Boolean result = defaultValue;
        Object tmp = obj;
        if (obj != null) {
            try {
                result = (Boolean)tmp;
            } catch (Exception var5) {
                tmp = obj.toString().toLowerCase();
                result = tmp.equals("1") || tmp.equals("true");
            }
        }

        return result;
    }

    public static Date getDate(Object obj) {
        return getDate(obj, (Date)null);
    }

    public static Date getDate(Object obj, Date defaultValue) {
        Date result = defaultValue;
        Object tmp = obj;
        if (obj != null) {
            if (obj instanceof Date) {
                result = (Date)obj;
            } else if (obj instanceof Long) {
                try {
                    result = new Date((Long)tmp);
                } catch (Exception var17) {
                }
            } else if (obj instanceof String) {
                String tmpString = (String)obj;

                try {
                    result = new Date(Long.parseLong(tmpString));
                } catch (Exception var16) {
                    try {
                        result = sdf1.parse(tmpString);
                    } catch (Exception var15) {
                        try {
                            result = sdfGMT.parse(tmpString);
                        } catch (Exception var14) {
                            try {
                                result = sdf3.parse(tmpString);
                            } catch (Exception var13) {
                                try {
                                    result = sdf4.parse(tmpString);
                                } catch (Exception var12) {
                                    try {
                                        result = sdf2.parse(tmpString);
                                    } catch (Exception var11) {
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        return result;
    }

    public static Integer getInteger(Object obj) {
        return getInteger(obj, (Integer)null);
    }

    public static Integer getInteger(Object obj, Integer defaultValue) {
        Object tmp = obj;
        Integer result;
        if (obj instanceof Integer) {
            result = (Integer)obj;
        } else {
            result = defaultValue;

            try {
                String tmpString = tmp.toString();
                result = Integer.parseInt(tmpString);
            } catch (Exception var5) {
            }
        }

        return result;
    }

    public static Long getLong(Object obj) {
        return getLong(obj, (Long)null);
    }

    public static Long getLong(Object obj, Long defaultValue) {
        Object tmp = obj;
        Long result;
        if (obj instanceof Long) {
            result = (Long)obj;
        } else {
            result = defaultValue;

            try {
                String tmpString = tmp.toString();
                result = Long.parseLong(tmpString);
            } catch (Exception var5) {
            }
        }

        return result;
    }

    public static Double getDouble(Object obj) {
        return getDouble(obj, (Double)null);
    }

    public static Double getDouble(Object obj, Double defaultValue) {
        Object tmp = obj;
        Double result;
        if (obj instanceof Double) {
            result = (Double)obj;
        } else {
            result = defaultValue;

            try {
                String tmpString = tmp.toString();
                result = Double.parseDouble(tmpString);
            } catch (Exception var5) {
            }
        }

        return result;
    }

    public static Float getFloat(Object obj) {
        return getFloat(obj, (Float)null);
    }

    public static Float getFloat(Object obj, Float defaultValue) {
        Object tmp = obj;
        Float result;
        if (obj instanceof Float) {
            result = (Float)obj;
        } else {
            result = defaultValue;

            try {
                String tmpString = tmp.toString();
                result = Float.parseFloat(tmpString);
            } catch (Exception var5) {
            }
        }

        return result;
    }

    static {
        sdfGMT = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.ENGLISH);
    }
}
