package com.reactlibrary.util;

import android.content.Context;
import android.util.Log;

import com.facebook.react.ReactApplication;
import com.facebook.react.bridge.JSBundleLoader;
import com.facebook.react.bridge.JSCJavaScriptExecutorFactory;
import com.facebook.react.bridge.JavaScriptExecutorFactory;
import com.reactlibrary.bundle.DecompressionInitBundleZipUtil;
import com.reactlibrary.constant.Constant;
import com.reactlibrary.data.ApkInfoSp;
import com.reactlibrary.data.BundleArchiveSp;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by Administrator on 2018/10/12.
 */

public class BundleInfoUtil {
    /*
    * 获取bundle的加载路径
    * */
    public static String getBundlePath(Context context){
        String fileStr = BundleInfoSp.getBundleValue(context,"fileStr");
        String bundleName = BundleInfoSp.getBundleValue(context,"bundleName");
        String bundle = fileStr + "/" + bundleName + "/index.android.bundle";
        return  bundle;
    }

    /*
    * 第一次进入APP，解压bundle
    * */
    public static void unzipBundle(Context context,String bundleNmae) {
        Constant.BUNDLE_NAME = bundleNmae;
        //验证当前的两个apk的版本是否一致，true是需要解压，flase是不需要解压
        boolean isDecompression = (ApkInfoSp.getCurrentApkVersionCode(context).equals(VersionCodeUtil.getVersionName(context)));
        if (!BundleArchiveSp.isArchiveBundleZip(context) || !(isDecompression)) {
            Log.i("------","开始解压。。。。");
            DecompressionInitBundleZipUtil.decompressionInitBundleZip(context);
            BundleArchiveSp.saveIsArchiveBundleZip(context,true);
            ApkInfoSp.saveCurrentApkVersionCode(context,VersionCodeUtil.getVersionName(context));
        }
    }

    /*bundle合并后重启APP*/
    public static void bundleUpdateDone(final Context context) {
        AppManager.getAppManager().currentActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String fileStr = BundleInfoSp.getBundleValue(context,"fileStr");
                String initBundleFile = BundleInfoSp.getBundleValue(context,"initBundleFile");
                String oldPath = fileStr + "/" + initBundleFile + "/index.android.bundle";
                onJSBundleLoadedFromServer(context,oldPath);
            }
        });
    }

    private static void onJSBundleLoadedFromServer(Context context,String file) {
        Log.i("-----","download success, reload js bundle");
        try {
            ReactApplication application = (ReactApplication) AppManager.getAppManager().currentActivity().getApplication();
            Class<?> RIManagerClazz = application.getReactNativeHost().getReactInstanceManager().getClass();
            Method method = RIManagerClazz.getDeclaredMethod("recreateReactContextInBackground",
                    JavaScriptExecutorFactory.class, JSBundleLoader.class);
            method.setAccessible(true);
            method.invoke(application.getReactNativeHost().getReactInstanceManager(),
                    new JSCJavaScriptExecutorFactory(AppUtil.getAppName(context), ""),
                    JSBundleLoader.createFileLoader(file));
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
    }
}
