package com.base.scanqrclear.ui.facebookqr

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.FacebookUIBean
import com.base.scanqrclear.bean.FacebookUIBean.Companion.createFacebookQrString
import com.base.scanqrclear.databinding.DialogFacebookBinding
import com.base.scanqrclear.qr.QrStyleActivity
import com.google.gson.Gson

object FacebookDialog {


    fun Activity.showFacebookDialog() {

        val dialog = AlertDialog.Builder(this).create()
        val binding = DialogFacebookBinding.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 isUrlOrId: Boolean = true
        binding.dslTabLayout.configTabLayoutConfig {
            onSelectIndexChange = { fromIndex, selectIndexList, reselect, fromUser ->
                val selectIndex = selectIndexList[0]
                if (selectIndex == 0) {
                    isUrlOrId = true
                    binding.editContent.hint = getString(R.string.please_enter_the_url)
                } else {
                    isUrlOrId = false
                    binding.editContent.hint = getString(R.string.please_enter_your_id)
                }
            }
        }

        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 = FacebookUIBean(content)

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

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

        }

    }
}