package com.base.scanqrclear.ui.widget

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.os.Build
import android.provider.Settings
import android.view.Gravity
import android.view.LayoutInflater
import com.base.scanqrclear.R
import com.base.scanqrclear.databinding.DialogNotificationTurnOnBinding
import com.base.scanqrclear.utils.ActivityLauncher

object NotificationDialog {

    @SuppressLint("SetTextI18n")
    fun Context.showNotificationTurnOn(launcher: ActivityLauncher) {
        val dialog = AlertDialog.Builder(this).create()
        val binding = DialogNotificationTurnOnBinding.inflate(LayoutInflater.from(this))
        dialog.setView(binding.root)
        dialog.setCanceledOnTouchOutside(true)

        dialog.show()

        val params = dialog.window?.attributes
//        params?.width = resources.getDimensionPixelOffset(R.dimen.dp_320)
//        params?.height = LinearLayout.LayoutParams.WRAP_CONTENT
        params?.gravity = Gravity.BOTTOM
//        params?.y = 50

        dialog.window?.attributes = params
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)

        val appName = resources.getString(R.string.app_name)
        binding.tvTip.text = "Never miss important $appName notification reminders"
        binding.ivClose.setOnClickListener {
            dialog.dismiss()
        }
        binding.tvTurnOn.setOnClickListener {
            dialog.dismiss()
            val intent = Intent()
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                intent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
                intent.putExtra(Settings.EXTRA_APP_PACKAGE, this.packageName)
            } else {
                intent.action = "android.settings.APP_NOTIFICATION_SETTINGS"
                intent.putExtra("app_package", this.packageName)
            }
            launcher.launch(intent)
        }
    }
}