package com.trust.ywx

import android.app.Application
import android.widget.Toast
import com.facebook.react.PackageList
import com.facebook.react.ReactApplication
import com.facebook.react.ReactHost
import com.facebook.react.ReactNativeApplicationEntryPoint.loadReactNative
import com.facebook.react.ReactNativeHost
import com.facebook.react.ReactPackage
import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost
import com.facebook.react.defaults.DefaultReactNativeHost
import com.trust.ywx.event.BundleUpdateEvent
import com.trust.ywx.specs.navigation.NavigationPackage
import com.trust.ywx.specs.update.UpdatePackage
import com.trust.ywx.utils.BUNDLE_VERSION_CODE
import com.trust.ywx.utils.FileHelper
import com.trust.ywx.utils.SHARED_PREFERENCES_NAME
import org.greenrobot.eventbus.EventBus
import java.io.File
import androidx.core.content.edit

class MainApplication : Application(), ReactApplication {

    override val reactNativeHost: ReactNativeHost = object : DefaultReactNativeHost(this) {
        override fun getPackages(): List<ReactPackage> = PackageList(this).packages.apply {
            // Packages that cannot be autolinked yet can be added manually here, for example:
            add(NavigationPackage())
            add(UpdatePackage())
        }

        override fun getJSMainModuleName(): String =
            if (getUseDeveloperSupport()) "index" else "commom"

        override fun getBundleAssetName(): String? =
            if (getUseDeveloperSupport()) "index.android.bundle" else null

        override fun getJSBundleFile(): String? = if (getUseDeveloperSupport()) {
            null
        } else {
            FileHelper.getFilePath("common.android.bundle", this@MainApplication, "bundles")
        }

        override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG

        override val isNewArchEnabled: Boolean = true
        override val isHermesEnabled: Boolean = true
    }

    override val reactHost: ReactHost
        get() = getDefaultReactHost(applicationContext, reactNativeHost)

    companion object {
        var isInit = false
    }

    override fun onCreate() {
        super.onCreate()
        if (!BuildConfig.DEBUG)
            createOrUpdateBundle()
        else {
            isInit = true
        }
        loadReactNative(this)
    }

    private fun createOrUpdateBundle() {
        // 使用中的bundle版本
        val sharedPref = getSharedPreferences(SHARED_PREFERENCES_NAME, MODE_PRIVATE)
        val bundleVersion = sharedPref.getInt("bundle_version", 0)

        // apk携带的bundle版本
        val localBundleVersion = BUNDLE_VERSION_CODE
        // 本地bundle目录
        val f = File(FileHelper.getDirPath(this, "bundles"))

        // 使用中的bundle比apk携带的小，则直接复制解压
        if (bundleVersion < localBundleVersion || !f.exists() || !f.isDirectory) {
            // bundle.zip在本地的路径
            val bf = File(FileHelper.getFilePath("bundle.zip", this, "bundles"))
            // 果断点，直接删除重建
            if (f.exists() && f.isDirectory) {
                f.delete()
            }
            f.mkdirs()
            // 拷贝bundle.zip
            FileHelper.copyAssetFileToInternalStorage(this, "android.zip", bf)
            if (bf.exists()) {
                // 复制成功后，直接解压
                FileHelper.unzip(bf, f)
                isInit = true
                sharedPref.edit { putInt("bundle_version", localBundleVersion) }
                EventBus.getDefault().post(BundleUpdateEvent())
            }
        } else {
            isInit = true
        }

    }
}
