package com.base.filerecoveryrecyclebin.ads

import android.animation.ObjectAnimator
import android.app.Activity
import android.app.Dialog
import android.os.Handler
import android.os.Looper
import android.view.View
import android.view.Window
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.base.filerecoveryrecyclebin.R
import com.base.filerecoveryrecyclebin.activity.splash.Splash2Activity
import com.base.filerecoveryrecyclebin.utils.LogEx

object CountdownDialog {

    private val TAG = "CountdownDialog"

    fun dismissLoadingDialog() {
        kotlin.runCatching {
            loadingDialog?.dismiss()
            loadingDialog = null
        }
    }

    /**
     * 显示倒计时弹窗
     */
    fun showLoadingScreen(activity: Activity, callBack: (() -> Unit)?) {

        if (loadingDialog == null) {
            loadingDialog = Dialog(activity).apply {
                requestWindowFeature(Window.FEATURE_NO_TITLE)
                setContentView(R.layout.dialog_loading)
                setCancelable(false)
                window?.setBackgroundDrawable(ContextCompat.getDrawable(activity, R.drawable.bg_loadingads))
            }
        }

        val imageView = loadingDialog?.findViewById<ImageView>(R.id.loading_image)
        val countdownTextView = loadingDialog?.findViewById<TextView>(R.id.countdown_text)

        imageView?.let { startRotateAnimation(it) } // 开始旋转动画

        // 控制倒计时文本是否显示
        countdownTextView?.visibility = View.VISIBLE

        loadingDialog?.setOnDismissListener {
            loadingDialog = null
        }

        if (!activity.isFinishing && !activity.isDestroyed) {
            loadingDialog?.show()
        }
        callBack?.invoke()
    }

    private var countdown = 3 // 初始倒计时
    private var countdownHandler: Handler? = null
    private var loadingDialog: Dialog? = null

    /**
     *  倒计时3秒，如果显示了弹窗跟弹窗
     */
    fun startCountdownAndShowAd(callBack: (() -> Unit)?) {
        countdown = 3

        updateCountdownText(countdown)  // 显示倒计时文本

        countdownHandler?.removeCallbacksAndMessages(null)
        countdownHandler = Handler(Looper.getMainLooper())
        countdownHandler?.post(object : Runnable {
            override fun run() {
                if (countdown <= 0) {
                    loadingDialog?.dismiss()
                    Splash2Activity.progressCallBack = null
                    callBack?.invoke()
                } else {
                    Splash2Activity.progressCallBack?.invoke()
                    updateCountdownText(countdown)  // 更新倒计时文本
                    countdown--
                    LogEx.logDebug(TAG, "startCountdownAndShowAd countdown=$countdown")
                    countdownHandler?.postDelayed(this, 1000)
                }
            }
        })
    }

    // 更新倒计时文本并控制是否显示括号和数字
    private fun updateCountdownText(count: Int) {
        val countdownTextView = loadingDialog?.findViewById<TextView>(R.id.countdown_text)
//        LogEx.logDebug(AdmobOpenUtils.TAG, "updateCountdownText $count countdownTextView=$countdownTextView")
        countdownTextView?.text = "Ads are about to be shown($count)"
        countdownTextView?.visibility = View.VISIBLE
    }

    private fun startRotateAnimation(imageView: ImageView) {
        ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f).apply {
            duration = 3000 // 持续 3 秒
            repeatCount = ObjectAnimator.INFINITE // 无限循环
            start()
        }
    }
}