package com.zj365.health

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.zj365.health.R
import java.io.File

class DownloadFile(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_009)
            dialogProgressBarColor = ContextCompat.getColor(context, R.color.col_009)
            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?) {
                    error.invoke()
                }
            })
        }

        manager.setApkName("ay_android.pdf")
            .setApkUrl(url)
            .setSmallIcon(R.mipmap.ic_upload).configuration = configuration

    }

}