package com.test.basd.smartjunkcleaner.view

import android.app.Activity
import android.view.Gravity
import android.view.ViewGroup
import android.widget.PopupWindow
import androidx.core.view.updatePadding
import com.blankj.utilcode.util.BarUtils
import com.test.basd.smartjunkcleaner.databinding.PopupLayoutAdPreparBinding
import kotlin.random.Random

class AdPreparationPop(private val activity: Activity, val onDismiss: () -> Unit) : PopupWindow() {

    init {
        width = ViewGroup.LayoutParams.MATCH_PARENT
        height = ViewGroup.LayoutParams.MATCH_PARENT

        isOutsideTouchable = false
        isFocusable = true
        isClippingEnabled = false
    }

    private val binding by lazy {
        PopupLayoutAdPreparBinding.inflate(activity.layoutInflater)
    }

    fun show() {
        contentView = binding.root
        activity.window.decorView.post {
            binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
            BarUtils.setStatusBarLightMode(activity, false)
            showAtLocation(activity.window.decorView, Gravity.CENTER, 0, 0)
        }
        binding.root.postDelayed({
            dismiss()
            onDismiss.invoke()
        }, Random.nextLong(2500, 4000))

    }


    override fun dismiss() {
        super.dismiss()
        BarUtils.setStatusBarLightMode(activity, true)
    }
}