package com.abbidot.tracker.dialog

import android.content.Context
import android.view.View
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseDialog
import com.abbidot.tracker.databinding.DialogDfuNewBinding

/**
 *Created  by .yzq on 2024/10/30/030.
 * @link
 * @description:
 */
class DFUNewDialog(context: Context) :
    BaseDialog<DialogDfuNewBinding>(DialogDfuNewBinding::inflate, context) {

    private var mVersion = ""

    override fun initView() {
//        setCanceledOnTouchOutside(false)
        setCancelable(false)
        mViewBinding.apply {
            btnDialogDfuNewUpgradeNow.setChangeAlphaWhenPress(true)
            setOnClickListenerViews(tvDialogDfuNewLaterBtn, btnDialogDfuNewDone)
        }
    }

    fun startDfuState(version: String, onClick: () -> Unit) {
        mVersion = "\t$version"
        val ver = String.format(mContext.getString(R.string.txt_about_version), mVersion)
        mViewBinding.apply {
            rlDialogDfuNewAfterLayout.visibility = View.GONE
            llDialogDfuNewBeforeLayout.visibility = View.VISIBLE
            tvDialogDfuNewTitle.text = ver
            btnDialogDfuNewUpgradeNow.text = mContext.getString(R.string.txt_upgrade_now)
            btnDialogDfuNewUpgradeNow.setOnClickListener {
                inDFUState()
                onClick()
            }
        }
    }

    private fun inDFUState() {
        val ver = String.format(mContext.getString(R.string.txt_about_version), " $mVersion")
        mViewBinding.apply {
            rlDialogDfuNewAfterLayout.visibility = View.VISIBLE
            llDialogDfuNewBeforeLayout.visibility = View.GONE
            tvDialogDfuNewProgressText.text = "0"
            tvDialogDfuNewBottomVersion.text = ver
            tvDialogDfuNewBottomVersion.visibility = View.VISIBLE
            lavDialogDfuNewAnim.visibility = View.VISIBLE
            tvDialogDfuNewTopVersion.text = ver
            tvDialogDfuNewTopVersion.visibility = View.GONE
            btnDialogDfuNewDone.visibility = View.GONE
            lavDialogDfuNewAnim.setAnimation("lottie/dfu_loading.json")
            lavDialogDfuNewAnim.playAnimation()
        }
    }

    fun setDFUProgress(progress: Int) {
        if (progress == 0) return
        mViewBinding.apply {
            tvDialogDfuNewProgressText.text = "$progress"
            if (progress == 100) {
                lavDialogDfuNewAnim.cancelAnimation()
                lavDialogDfuNewAnim.visibility = View.INVISIBLE
                tvDialogDfuNewBottomVersion.visibility = View.GONE
                tvDialogDfuNewTopVersion.visibility = View.VISIBLE
                btnDialogDfuNewDone.visibility = View.VISIBLE
            }
        }
    }

    fun setDFUFail(onClick: () -> Unit) {
        mViewBinding.apply {
            llDialogDfuNewBeforeLayout.visibility = View.VISIBLE
            rlDialogDfuNewAfterLayout.visibility = View.GONE
            tvDialogDfuNewTitle.text = mContext.getString(R.string.txt_upgrade_fail)
            btnDialogDfuNewUpgradeNow.text =
                mContext.getString(R.string.txt_pay_order_payment_again)
            btnDialogDfuNewUpgradeNow.setOnClickListener {
                inDFUState()
                onClick()
            }
        }
    }

    override fun onClick(v: View) {
        mViewBinding.apply {
            when (v) {
                tvDialogDfuNewLaterBtn, btnDialogDfuNewDone -> dismiss()
            }
        }
    }
}