package com.base.scanqr.ads

import android.animation.ObjectAnimator
import android.animation.ValueAnimator.INFINITE
import android.app.AlertDialog
import android.content.Context
import android.view.LayoutInflater
import android.view.animation.LinearInterpolator
import com.base.scanqr.databinding.DialogAdPreparingBinding
import com.base.scanqr.utils.DensityUtils
import com.base.scanqr.utils.LogEx


object AdDialog {

    private val TAG = "AdDialog"
    fun Context.showAdPreparingDialog(where: Int = 0): AlertDialog {
        LogEx.logDebug(TAG, "where=$where")
        val binding = DialogAdPreparingBinding.inflate(LayoutInflater.from(this))
        val dialog = AlertDialog.Builder(this).create()
        dialog.setView(binding.root)
        dialog.setCancelable(false)
        dialog.setCanceledOnTouchOutside(false)

        dialog.show()
        val params = dialog.window?.attributes
        params?.width = DensityUtils.dip2px(200f)
        params?.height = DensityUtils.dip2px(146f)
        dialog.window?.attributes = params
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)

        // 创建一个旋转动画
        val rotateAnimator = ObjectAnimator.ofFloat(binding.iv, "rotation", 0f, -360f)
        rotateAnimator.setDuration(1000) // 设置动画持续时间为1000毫秒
        rotateAnimator.repeatCount = INFINITE
        rotateAnimator.interpolator = LinearInterpolator() // 设置插值器为线性插值
        rotateAnimator.start()

        return dialog
    }
}