package com.rn.sh3h.menu;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.Base64;

import java.io.ByteArrayOutputStream;


public class IconUtils {

    public static IconSource getIcon(String packageName, String icon,Context context) {
        IconSource iconSource = new IconSource();
        Drawable drawable = null;
        try {
            if (icon.contains(".png")) {
                int index = icon.indexOf(".png");
                icon = icon.substring(0, index);
            }

            PackageManager pm = context.getPackageManager();
            PackageInfo info = pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
            if (info != null) {
                ApplicationInfo appInfo = info.applicationInfo;
                Resources resources = pm.getResourcesForApplication(packageName);
                int id = resources.getIdentifier(icon, "mipmap", packageName);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    iconSource.setSuccess(true);
                    drawable = resources.getDrawable(id, null);
                } else {
                    iconSource.setSuccess(true);
                    drawable = resources.getDrawable(id);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            drawable = null;
        }

        if (drawable == null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                iconSource.setSuccess(false);
                drawable = context.getResources().getDrawable(R.mipmap.ic_launcher, null);
            } else {
                iconSource.setSuccess(false);
                drawable = context.getResources().getDrawable(R.mipmap.ic_launcher);
            }
        }

        iconSource.setBase64(getBitmapStrBase64(drawableToBitmap(drawable)));
        return iconSource;
    }

    private static Bitmap drawableToBitmap(Drawable drawable) {

        Bitmap bitmap = Bitmap
                .createBitmap(
                        drawable.getIntrinsicWidth(),
                        drawable.getIntrinsicHeight(),
                        drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                                : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        //canvas.setBitmap(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }

    /**
     * Bitmap 通过Base64 转换为字符串
     *
     * @param bitmap
     * @return
     */
    private static String getBitmapStrBase64(Bitmap bitmap) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
        byte[] bytes = bos.toByteArray();
        String base64 = Base64.encodeToString(bytes, Base64.DEFAULT);
        String result = base64.replace("\n", "");
        return "data:image/jpeg;base64," + result;
    }

    public static class IconSource{
        private boolean isSuccess;
        private String base64;

        public boolean isSuccess() {
            return isSuccess;
        }

        void setSuccess(boolean success) {
            isSuccess = success;
        }

        public String getBase64() {
            return base64;
        }

        void setBase64(String base64) {
            this.base64 = base64;
        }
    }
}
