package com.cheng.blzb.utils

import androidx.fragment.app.Fragment
import com.cheng.blzb.BuildConfig
import com.cheng.blzb.R
import com.cheng.blzb.bean.VersionEntity
import com.cheng.blzb.manager.UserConfigManager
import com.cheng.blzb.ui.dialog.UpdateVersionDialog
import com.example.base.extensions.toast

object AppUpdateUtils {

    fun checkUpdate(fragment: Fragment, isManual: Boolean, clickFun: () -> Unit?) {

        if (isManual) {
            toast(fragment.getString(R.string.check_version_ing))
        }

        val result: VersionEntity? = UserConfigManager.userConfig?.config?.versionEntity
        result?.apply {
            if (UIUtils.checkVersion(version, BuildConfig.VERSION_NAME)) {
                if (last_version_force == BuildConfig.VERSION_NAME) {
                    force = true
                } else if (UIUtils.checkVersion(last_version_force, BuildConfig.VERSION_NAME)) {
                    force = true
                }
                clickFun.invoke()
                startUpdate(result, fragment)
            } else {
                if (isManual) {
                    toast(fragment.getString(R.string.curr_new_version))
                }
            }
        }
    }

    private fun startUpdate(versionEntity: VersionEntity, fragment: Fragment) {
        UpdateVersionDialog.newInstance(versionEntity).show(fragment.childFragmentManager, null)
    }
}