package com.xty.base.utils

import android.app.Activity
import androidx.core.content.ContextCompat
import com.azhon.appupdate.config.UpdateConfiguration
import com.azhon.appupdate.listener.OnDownloadListener
import com.azhon.appupdate.manager.DownloadManager
import com.hjq.toast.ToastUtils
import com.xty.base.R
import java.io.File

class AppUpdate(var context: Activity) {
    var manager:DownloadManager = DownloadManager.getInstance(context)

    fun uploadApk(
        url: String,
        downIng: (progress: Int) -> Unit,
        complete: (apk: File?) -> Unit,
        error:()->Unit
    ) {

        var configuration = UpdateConfiguration().apply {
            isForcedUpgrade = true  //强制更新
            isShowNotification = false
            isShowBgdToast = false
            dialogImage = R.drawable.shape_bg_health_round
            dialogButtonColor = ContextCompat.getColor(context, R.color.col_02c)
            dialogProgressBarColor = ContextCompat.getColor(context, R.color.col_02c)
            setButtonClickListener {

            }
            setOnDownloadListener(object : OnDownloadListener {
                override fun start() {
                }

                override fun downloading(max: Int, progress: Int) {
                    val curr = (progress / max.toDouble() * 100.0).toInt()
                    downIng.invoke(curr)
                }

                override fun done(apk: File?) {
                    complete.invoke(apk)

                }

                override fun cancel() {
                }

                override fun error(e: Exception?) {
                    ToastUtils.show(e.toString())
                    error.invoke()

                }
            })
        }

        manager.setApkName("zj365_android.apk")
            .setApkUrl(url)
            .setSmallIcon(R.mipmap.ic_upload).configuration = configuration

    }

}