package com.tgv.malware.ui.malware

import android.app.Activity
import android.view.LayoutInflater
import androidx.appcompat.app.AlertDialog
import com.tgv.malware.databinding.DialogMalwareTipBinding
import com.tgv.malware.utils.AppPreferences

//是否已经同意
var malwareTipAgree = false
    get() {
        return AppPreferences.getInstance().getBoolean("malwareTipAgree", field)
    }
    set(value) {
        field = value
        AppPreferences.getInstance().put("malwareTipAgree", value, true)
    }

class MalwareDialog(
    val activity: Activity
) {

    val dialog = AlertDialog.Builder(activity).create()
    val binding = DialogMalwareTipBinding.inflate(LayoutInflater.from(activity))

    var action: ((flag: Boolean) -> Unit)? = null

    fun showDialog() {

        if (malwareTipAgree) {
            action?.invoke(true)
            return
        }

        dialog.setView(binding.root)
        dialog.setCanceledOnTouchOutside(false)
        dialog.show()

        val params = dialog.window?.attributes
//        params?.width = ConstraintLayout.LayoutParams.MATCH_PARENT
//        params?.width = activity.resources.getDimensionPixelSize(R.dimen.dp_295)
//        params?.height = ConstraintLayout.LayoutParams.WRAP_CONTENT
//        params?.gravity = Gravity.BOTTOM
        dialog.window?.attributes = params
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)

        var flag = false

        binding.tvCancel.setOnClickListener {
            dialog.dismiss()
        }
        binding.tvSure.setOnClickListener {
            dialog.dismiss()
            flag = true
            malwareTipAgree = true
            action?.invoke(true)
        }

        dialog.setOnDismissListener {
            action?.invoke(flag)
        }
    }
}