package com.base.scanqrclear.ui.insqr

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.InstagramUIBean
import com.base.scanqrclear.bean.InstagramUIBean.Companion.createInstagramIdQrString
import com.base.scanqrclear.bean.InstagramUIBean.Companion.createInstagramUrlQrString
import com.base.scanqrclear.databinding.DialogFacebookBinding
import com.base.scanqrclear.qr.QrStyleActivity

object InstagramDialog {

    fun Activity.showInstagramDialog() {

        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 = if (isUrlOrId) InstagramUIBean(url = content) else InstagramUIBean(id = content)

            startActivity(Intent(this, QrStyleActivity::class.java).apply {
                putExtra("qrString", if (isUrlOrId) bean.createInstagramUrlQrString() else bean.createInstagramIdQrString())
            })
        }

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

        }
    }
}