package com.xty.base.dialog

import android.app.Activity
import android.view.View
import com.azhon.appupdate.utils.ApkUtil
import com.azhon.appupdate.utils.Constant
import com.hjq.toast.ToastUtils
import com.xty.base.R
import com.xty.base.databinding.DialogUpdataBinding
import com.xty.base.utils.AppUpdate
import com.xty.common.Const
import com.xty.common.LogUtils
import com.xty.common.MMkvHelper
import java.io.File

/**
 *更新弹窗
 */
class AppUpdateDialog(
    var context: Activity,
    private var close: (isUpdate: Boolean) -> Unit
) : BaseDialog(context) {

    private val binding by lazy { DialogUpdataBinding.inflate(layoutInflater) }
    override fun setLayout() = binding.root

    private val uploadManager by lazy { AppUpdate(context) }
    var apk: File? = null
    private var loadPath = ""
    private var isUpdate = false

    override fun initView() {
        super.initView()
        setDialogPadding(60, 0, 60, 0)
        setCancelable(false)
        setCanceledOnTouchOutside(false)
        binding.mToast.text = context.getString(R.string.check_version)

        binding.mLoadApk.setOnClickListener {
            if (apk == null) {
                uploadManager.manager.download()
                binding.mLoadApk.text = context.getString(R.string.loading)
                binding.mLoadApk.isEnabled = false
                binding.mToast.text = context.getString(R.string.loading_update)
            } else {
                binding.mLoadApk.text = context.getString(R.string.complete)
                binding.mLoadApk.isEnabled = true
                ApkUtil.installApk(context, Constant.AUTHORITIES, apk)
            }
        }

        binding.mCancelUpdate.setOnClickListener {
            closeUploadDialog()
        }

        binding.mClose.setOnClickListener {
            closeUploadDialog()
        }

    }

    fun showUploadDialog(
        url: String,
        log: String?,
        versionName:String,
        updateFlag: Int,
        isShow: (show: Boolean) -> Unit
    ) {
        LogUtils.d(
            "uploadDialog",
            "url:$url;;;log:${log};;;; updateFlag:$updateFlag;;; localUpdateUrl:${MMkvHelper.getString(Const.IS_UPDATE_URL_FLAG)}"
        )
        //0:非强制更新 1：强制更新
        isUpdate = (updateFlag == 1)
        binding.mProgress.progress = 0
                //如果当前地址用户忽略取消过并且当前版本非强制更新，则不弹出dialog
        if (MMkvHelper.getString(Const.IS_UPDATE_URL_FLAG) == url && !isUpdate) {
            isShow.invoke(false)
            return
        }
     //   binding.mClose.visibility = if (isUpdate) View.GONE else View.VISIBLE
        isShow.invoke(true)
        loadPath = url
        show()

        if (!log.isNullOrEmpty()) {
            binding.mTvLog.visibility = View.VISIBLE
            binding.mTvLog.text = log
        }

        binding.mVersionName.text ="V${versionName}"
        uploadManager.uploadApk(loadPath, {
            binding.mProgress.progress = it
            binding.mTvProgress.text = "${it}/100"
        }, { apk ->
            this.apk = apk
            apk?.let {
                binding.mToast.text = context.getString(R.string.load_end)
                if (it.exists()) {
                    binding.mLoadApk.text = context.getString(R.string.complete)
                    binding.mLoadApk.isEnabled = true
                    ApkUtil.installApk(context, Constant.AUTHORITIES, it)
                }
            }
        }) {
            ToastUtils.show("下载出错")
            binding.mLoadApk.text = context.getString(R.string.update)
            binding.mLoadApk.isEnabled = true
        }
    }

    private fun closeUploadDialog() {
        dismiss()
        //保存当前URL地址
        MMkvHelper.put(Const.IS_UPDATE_URL_FLAG, loadPath)
        uploadManager.manager.cancel()
        close.invoke(isUpdate)
    }

}