package com.easy.clean.ui.dialog

import android.app.Activity
import android.view.View
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatDialog
import com.easy.clean.R
import com.easy.clean.bean.config.AdConfigBean
import com.easy.clean.business.ads.AdsMgr
import com.easy.clean.databinding.DialogExitBinding

class ExitDialog(val activity: Activity) {

    val dialog = AppCompatDialog(activity)
    val binding = DialogExitBinding.inflate(activity.layoutInflater)
    var clean: (() -> Unit)? = null
    var ad: (() -> Unit)? = null

    fun init() {
        dialog.setContentView(binding.root)
        dialog.setCanceledOnTouchOutside(true)
        val params = dialog.window?.attributes
        dialog.window?.attributes = params
        params?.width = LinearLayout.LayoutParams.MATCH_PARENT
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
        initView()
    }

    private fun initView() {
        binding.ivClose.setOnClickListener {
            dialog.dismiss()
        }
        binding.tvExit.setOnClickListener {
            dialog.dismiss()
            activity.finish()
        }
        binding.tvClean.setOnClickListener {
            dialog.dismiss()
            clean?.invoke()
        }

    }

    fun show() {
        dialog.show()
        if (AdConfigBean.adsConfigBean.isAdShow) {
            AdsMgr.showNative(binding.flAd, R.layout.layout_admob_native_custom_r16)
        } else {
            binding.clAd.visibility = View.GONE
        }
    }

    fun hide() {
        dialog.hide()
    }
}