package com.maniu.skin_lib;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;

import java.lang.reflect.Method;

/**
 * 去加载皮肤资源包  然后获取里面的资源
 */
public class SkinManager {
    private static SkinManager skinManager = new SkinManager();
    //上下问
    private Context context;
    //资源包中的资源对象
    private Resources resources;
    //获取到资源包的包名
    private String packageName;

    private SkinManager(){}

    public static SkinManager getInstance(){
        return skinManager;
    }

    public void setContext(Context context){
        this.context = context;
    }

    /**
     * 根据传进来的路径去加载皮肤资源包
     * @param path
     */
    public void loadSkinApk(String path){
        PackageManager packageManager = context.getPackageManager();
        //去加载皮肤资源包
        PackageInfo packageArchiveInfo = packageManager.getPackageArchiveInfo(path,
                PackageManager.GET_ACTIVITIES);
        //获取到资源包的包名
        packageName = packageArchiveInfo.packageName;
        try {
            //创建一个对象
            AssetManager assetManager = AssetManager.class.newInstance();
            //获取到它的addAssetPath(的方法
            Method addAssetPath = assetManager.getClass().getDeclaredMethod("addAssetPath",
                    String.class);
            addAssetPath.invoke(assetManager,path);
            resources = new Resources(assetManager,
                    context.getResources().getDisplayMetrics(),context.getResources().getConfiguration());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 用来去资源包中的资源对象中去匹配
     * @param resId 在当前APP中的资源ID
     * @return
     */
    public int getColor(int resId) {
        if(resourceIsNull()){
            //返回本身
            return resId;
        }
        //去匹配
        //得到资源名字和类型
        //获取到属性的值的名字 colorAccent
        String resourceEntryName = context.getResources().getResourceEntryName(resId);
        //获取到属性的值的类型@color
        String resourceTypeName = context.getResources().getResourceTypeName(resId);
        //去匹配的API
        int identifier = resources.getIdentifier(resourceEntryName, resourceTypeName, packageName);
        //没有匹配到
        if(identifier == 0){
            return resId;
        }
        return resources.getColor(identifier);
    }

    /**
     * 从资源包中拿到drawable的资源id
     */
    public Drawable getDrawable(int id){
        if(resourceIsNull()){
            //获取到这个id在当前应用中的Drawable对象
            return ContextCompat.getDrawable(context,id);
        }
        //获取到资源id的类型
        String resourceTypeName = context.getResources().getResourceTypeName(id);
        //获取到的就是资源id的名字
        String resourceEntryName = context.getResources().getResourceEntryName(id);
        //就是colorAccent这个资源在外置APK中的id
        int identifier = resources.getIdentifier(resourceEntryName, resourceTypeName, packageName);
        if(identifier == 0){
            return ContextCompat.getDrawable(context,id);
        }
        return resources.getDrawable(identifier);
    }

    /**
     * 从资源包中拿到drawable的资源id
     */
    public int getDrawableId(int id){
        if(resourceIsNull()){
            return id;
        }
        //获取到资源id的类型
        String resourceTypeName = context.getResources().getResourceTypeName(id);
        //获取到的就是资源id的名字
        String resourceEntryName = context.getResources().getResourceEntryName(id);
        //就是colorAccent这个资源在外置APK中的id
        int identifier = resources.getIdentifier(resourceEntryName, resourceTypeName, packageName);
        if(identifier == 0){
            return id;
        }
        return identifier;
    }


    /**
     * 判断资源包的资源对象是否为空
     * @return
     */
    public boolean resourceIsNull(){
        if(resources ==null){
            return true;
        }
        return false;
    }


}
