package com.base.scanqrclear.ui.whatsappqr

import android.annotation.SuppressLint
import android.app.Activity
import android.app.AlertDialog
import android.content.Intent
import android.text.Editable
import android.view.Gravity
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.app.ActivityOptionsCompat
import androidx.core.widget.addTextChangedListener
import com.base.scanqrclear.R
import com.base.scanqrclear.ads.AdsMgr
import com.base.scanqrclear.ads.AdsShowCallBack
import com.base.scanqrclear.bean.WhatsappUIBean
import com.base.scanqrclear.bean.WhatsappUIBean.Companion.createWhatsappQrString
import com.base.scanqrclear.databinding.DialogWhastsppBinding
import com.base.scanqrclear.qr.QrStyleActivity
import com.base.scanqrclear.utils.ActivityLauncher
import com.base.scanqrclear.utils.AppPreferences
import com.base.scanqrclear.utils.LogEx


object WhatsappDialog {

    private val TAG = "WhatsappDialog"

    @SuppressLint("SetTextI18n")
    fun Activity.showWhatsappDialog(launcher: ActivityLauncher) {


        val dialog = AlertDialog.Builder(this).create()
        val binding = DialogWhastsppBinding.inflate(LayoutInflater.from(this))
        dialog.setView(binding.root)
        dialog.setCanceledOnTouchOutside(false)

        dialog.show()

        val params = dialog.window?.attributes
        params?.width = ConstraintLayout.LayoutParams.MATCH_PARENT
//        params?.height = resources.getDimensionPixelOffset(R.dimen.dp_400)
        params?.gravity = Gravity.BOTTOM
//        params?.y = 50

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

        AdsMgr.showNative(binding.flAd, R.layout.layout_admob_native_custom)

        binding.flClose.setOnClickListener {
            dialog.dismiss()
        }

        binding.editNumber.addTextChangedListener { s: Editable? ->
            binding.tvBtn.isEnabled = !s.isNullOrEmpty()
        }

        var code = AppPreferences.getInstance().getInt("areaCode", 1)
        binding.tvAreaCode.setOnClickListener {
            val intent = Intent(this@showWhatsappDialog, AreaCodeActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_NO_HISTORY

            val options = ActivityOptionsCompat.makeCustomAnimation(
                this,
                R.anim.slide_in_from_right,
                R.anim.slide_out_to_left
            )

            launcher.launch(intent, options) { result ->
                LogEx.logDebug(TAG, "code=${result.resultCode}")
                code = result.resultCode
                binding.tvAreaCode.text = "+${code}"
                AppPreferences.getInstance().putInt("areaCode", code)
            }
        }


        val jump = {
            dialog.dismiss()

            val phone = "$code" + binding.editNumber.text.toString()
            val bean = WhatsappUIBean(phone)

            startActivity(Intent(this, QrStyleActivity::class.java).apply {
                putExtra("qrString", bean.createWhatsappQrString())
            })
        }

        binding.tvBtn.setOnClickListener {

            AdsMgr.showInsert(this, false, object : AdsShowCallBack() {
                override fun close(where: Int) {
                    jump.invoke()
                }

                override fun failed(where: Int) {
                    jump.invoke()
                }

                override fun googleFailed(where: Int) {
                    jump.invoke()
                }
            })


        }


    }
}