package com.mints.library.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * 作者：孟崔广
 * 时间：2018/1/10 17:51
 * 邮箱：mengcga@163.com
 */
public class AITextUtil {
    public static final String TAG = AITextUtil.class.getSimpleName();

    public AITextUtil() {
    }

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

    public static boolean isEmpty(Map map) {
        return null == map || map.isEmpty();
    }

    public static boolean isEmpty(Object[] objs) {
        return null == objs || objs.length <= 0;
    }

    public static boolean isEmpty(int[] objs) {
        return null == objs || objs.length <= 0;
    }

    public static boolean isEmpty(CharSequence charSequence) {
        return null == charSequence || charSequence.length() <= 0;
    }

    public static boolean isBlank(CharSequence charSequence) {
        return null == charSequence || charSequence.toString().trim().length() <= 0;
    }

    public static boolean isLeast(Object[] objs, int count) {
        return null != objs && objs.length >= count;
    }

    public static boolean isLeast(int[] objs, int count) {
        return null != objs && objs.length >= count;
    }

    public static boolean isEquals(Object a, Object b) {
        return a == null?b == null:a.equals(b);
    }

    public static String trim(CharSequence charSequence) {
        return null == charSequence?null:charSequence.toString().trim();
    }

    public static String pickFirstNotNull(CharSequence... options) {
        if(isEmpty((Object[])options)) {
            return null;
        } else {
            String result = null;
            CharSequence[] var2 = options;
            int var3 = options.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                CharSequence cs = var2[var4];
                if(null != cs) {
                    result = cs.toString();
                    break;
                }
            }

            return result;
        }
    }

//    @SafeVarargs
//    public static <T> T pickFirstNotNull(Class<T> clazz, T... options) {
//        if(isEmpty(options)) {
//            return null;
//        } else {
//            Object result = null;
//            Object[] var3 = options;
//            int var4 = options.length;
//
//            for(int var5 = 0; var5 < var4; ++var5) {
//                Object obj = var3[var5];
//                if(null != obj) {
//                    result = obj;
//                    break;
//                }
//            }
//
//            return result;
//        }
//    }

//    public static SpannableString replaceImageSpan(CharSequence charSequence, String regPattern, Drawable drawable) {
//        SpannableString ss = charSequence instanceof SpannableString?(SpannableString)charSequence:new SpannableString(charSequence);
//
//        try {
//            ImageSpan ex = new ImageSpan(drawable);
//            Pattern pattern = Pattern.compile(regPattern);
//            Matcher matcher = pattern.matcher(ss);
//
//            while(matcher.find()) {
//                String key = matcher.group();
//                ss.setSpan(ex, matcher.start(), matcher.start() + key.length(), 17);
//            }
//        } catch (Exception var8) {
//            Logger.e(TAG, var8);
//        }
//
//        return ss;
//    }

    public static String compress(String str) throws IOException {
        if(str != null && str.length() != 0) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPOutputStream gzip = new GZIPOutputStream(out);
            gzip.write(str.getBytes());
            gzip.close();
            return out.toString("ISO-8859-1");
        } else {
            return str;
        }
    }

    public static String uncompress(String str) throws IOException {
        if(str != null && str.length() != 0) {
            ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("UTF-8"));
            return uncompress((InputStream)in);
        } else {
            return str;
        }
    }

    public static String uncompress(InputStream inputStream) throws IOException {
        if(inputStream == null) {
            return null;
        } else {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPInputStream gunzip = new GZIPInputStream(inputStream);
            byte[] buffer = new byte[256];

            int n;
            while((n = gunzip.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }

            return out.toString();
        }
    }

    public static String inputStream2String(InputStream in) throws IOException {
        StringBuffer out = new StringBuffer();
        byte[] b = new byte[4096];

        int n;
        while((n = in.read(b)) != -1) {
            out.append(new String(b, 0, n));
        }

        return out.toString();
    }

//    public static String inputStream2StringFromGZIP(InputStream is) {
//        StringBuilder resultSb = new StringBuilder();
//        BufferedInputStream bis = null;
//        InputStreamReader reader = null;
//
//        try {
//            bis = new BufferedInputStream((InputStream)is);
//            bis.mark(2);
//            byte[] e = new byte[2];
//            int result = bis.read(e);
//            bis.reset();
//            int headerData = getShort(e);
//            if(result != -1 && headerData == 8075) {
//                is = new GZIPInputStream(bis);
//            } else {
//                is = bis;
//            }
//
//            reader = new InputStreamReader((InputStream)is, "utf-8");
//            char[] data = new char[100];
//
//            int readSize;
//            while((readSize = reader.read(data)) > 0) {
//                resultSb.append(data, 0, readSize);
//            }
//        } catch (Exception var12) {
//            Logger.e(TAG, var12);
//        } finally {
//            ABIOUtil.closeIO(new Closeable[]{(Closeable)is, bis, reader});
//        }
//
//        return resultSb.toString();
//    }

    private static int getShort(byte[] data) {
        return data[0] << 8 | data[1] & 255;
    }


}
