package com.zhangmeng.model.base.baseUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class CommonUtil {

    public static boolean isNotNull(Object object) {
        return object != null && !"".equals(object);
    }

    @SuppressWarnings("unchecked")
    public static <T, K, V> T map_get_value(Map<K, V> map, T t) {
        return (T) map.get(t);
    }

    public static boolean isNumeric_JDK(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public static boolean isNumeric_ASCII(String str) {
        for (int i = str.length(); --i >= 0; ) {
            int chr = str.charAt(i);
            if (chr < 48 || chr > 57)
                return false;
        }
        return true;
    }

    public static boolean isNumeric_exception(String str) {
        try {
            Integer.parseInt(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    public static Long parseLong(String number) {
        if (isNumeric_ASCII(number)) {
            return Long.parseLong(number);
        }
        return null;
    }

    /**
     * 去掉收尾逗号
     */
    public static String firstLastComma(String string) {
        StringBuilder stringBuilder = new StringBuilder();
        String[] split = string.trim().split(",");
        if (split.length > 0) {
            for (int i = 0; i < split.length; i++) {
                if (i == split.length - 1) {
                    stringBuilder.append(split[i]);
                } else {
                    if (!split[i].equals("")) {
                        stringBuilder.append(split[i]).append(",");
                    }
                }
            }
        }
        return stringBuilder.toString();
    }

    private static final String TOKEN_KEY = "token";

    private static String YYYY = "yyyy";

    private static String YYYY_MM = "yyyy-MM";

    private static String YYYY_MM_DD = "yyyy-MM-dd";

    private static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    private static Integer pageNum = 1;

    private static Integer pageSize = 10;

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    public enum Format {

        YYYY("yyyy"),
        YYYY_MM("yyyy-MM"),
        YYYY_MM_DD("yyyy-MM-dd"),
        YYYY_MM_DD_HH_MM_SS("yyyy-MM-dd HH:mm:ss");

        Format(String desc) {
            this.desc = desc;
        }

        private String desc;

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }
    }

    /**
     * 日期格式
     *
     * @param date 日期
     * @return String
     */
    public static String data_format(Date date, Format format) {
        String format_string = null;
        switch (format) {
            case YYYY:
                format_string = YYYY;
                break;
            case YYYY_MM:
                format_string = YYYY_MM;
                break;
            case YYYY_MM_DD:
                format_string = YYYY_MM_DD;
                break;
            case YYYY_MM_DD_HH_MM_SS:
                format_string = YYYY_MM_DD_HH_MM_SS;
                break;
        }
        return new SimpleDateFormat(format_string).format(date);
    }


    /**
     * 日期格式
     *
     * @param date 日期
     * @return String
     */
    public static Date data_format(String date, Format format) {
        String format_string = null;
        switch (format) {
            case YYYY:
                format_string = YYYY;
                break;
            case YYYY_MM:
                format_string = YYYY_MM;
                break;
            case YYYY_MM_DD:
                format_string = YYYY_MM_DD;
                break;
            case YYYY_MM_DD_HH_MM_SS:
                format_string = YYYY_MM_DD_HH_MM_SS;
                break;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format_string);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T extends Enum<T>> T getEnum(String str, Class<T> enumClass) {
        if (isNotNull(str)) {
            return Enum.valueOf(enumClass, str);
        }
        return null;
    }

    /**
     * 模拟手机验证码
     *
     * @param size 验证码的个数
     * @return String
     */
    public static String mobileVerifierCode(int size) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 1; i <= size; i++) {
            //产生0~9的随机数
            int d = (int) (Math.random() * 9 + 1);
            stringBuilder.append(d);
        }
        return stringBuilder.toString();
    }
}
