package com.base.scanqrclear.ui.paypalqr

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.widget.addTextChangedListener
import com.base.scanqrclear.R
import com.base.scanqrclear.ads.AdsMgr
import com.base.scanqrclear.ads.AdsShowCallBack
import com.base.scanqrclear.bean.PaypalUIBean
import com.base.scanqrclear.bean.PaypalUIBean.Companion.createPaypalQrString
import com.base.scanqrclear.databinding.DialogPaypalBinding
import com.base.scanqrclear.qr.QrStyleActivity

object PaypalDialog {

    fun Activity.showPaypalDialog() {

        val dialog = AlertDialog.Builder(this).create()
        val binding = DialogPaypalBinding.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)

        var isMeLinkOrMeUsername: Boolean = true
        binding.dslTabLayout.configTabLayoutConfig {
            onSelectIndexChange = { fromIndex, selectIndexList, reselect, fromUser ->
                val selectIndex = selectIndexList[0]
                if (selectIndex == 0) {
                    isMeLinkOrMeUsername = true
                    binding.editContent.hint = "Please enter Me link"
                } else {
                    isMeLinkOrMeUsername = false
                    binding.editContent.hint = "Please enter Me username"
                }
            }
        }

        binding.flClose.setOnClickListener {
            dialog.dismiss()
        }
        binding.editContent.addTextChangedListener { s: Editable? ->
            binding.tvBtn.isEnabled = !s.isNullOrEmpty()
        }

        val jump = {
            dialog.dismiss()

            val content = binding.editContent.text.toString()
            val bean = PaypalUIBean(content)

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

        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()
                }
            })

        }

    }

}