package com.base.pdfreaderallpdfreader.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.pdfreaderallpdfreader.R
import com.base.pdfreaderallpdfreader.databinding.DialogAdPreparingBinding


object AdDialog {
    fun Context.showAdPreparingDialog(): AlertDialog {
        val binding = DialogAdPreparingBinding.inflate(LayoutInflater.from(this))
        val dialog = AlertDialog.Builder(this).create()
        dialog.setView(binding.root)
        dialog.setCancelable(false)
        dialog.setCanceledOnTouchOutside(true)

        dialog.show()
        val params = dialog.window?.attributes
        params?.width = resources.getDimensionPixelOffset(R.dimen.dp_200)
        params?.height = resources.getDimensionPixelOffset(R.dimen.dp_146)
        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
    }
}