package com.base.scanqrclear.ui.telephone

import android.content.Intent
import android.net.Uri
import androidx.activity.addCallback
import androidx.core.view.updatePadding
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.lifecycleScope
import com.base.scanqrclear.R
import com.base.scanqrclear.ads.AdsMgr
import com.base.scanqrclear.ads.AdsShowCallBack
import com.base.scanqrclear.base.BaseActivity
import com.base.scanqrclear.bean.TelephoneUIBean
import com.base.scanqrclear.bean.config.AdConfigBean
import com.base.scanqrclear.databinding.ActivityTelephoneBinding
import com.base.scanqrclear.utils.BarUtils
import com.base.scanqrclear.utils.ContactUtils.queryTel
import com.base.scanqrclear.utils.IntentUtils.intentPickContacts
import com.base.scanqrclear.utils.LogEx
import com.base.scanqrclear.utils.PermissionUtils.checkContactsReadPermission
import com.base.scanqrclear.utils.ToastUtils.toast
import com.google.gson.Gson
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


class TelephoneActivity : BaseActivity<ActivityTelephoneBinding>(ActivityTelephoneBinding::inflate) {

    private var telephoneBean = TelephoneUIBean()

    override fun onResumeOneShoot() {
        super.onResumeOneShoot()
        val flag = changeLanguage()
        if (flag) return
    }

    override fun initView() {
        super.initView()
        binding.clTop.updatePadding(top = BarUtils.getStatusBarHeight())
        AdsMgr.showNative(binding.flAd, R.layout.layout_admob_app_exit)
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            if (AdConfigBean.adsConfigBean.functionBackShowAd) {
                AdsMgr.showInsert(this@TelephoneActivity, false, object : AdsShowCallBack() {
                    override fun close(where: Int) {
                        finishToMainTop()
                    }

                    override fun failed(where: Int) {
                        finishToMainTop()
                    }

                    override fun googleFailed(where: Int) {
                        finishToMainTop()
                    }
                })
            } else {
                finishToMainTop()
            }
        }
        binding.flBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.edit.addTextChangedListener { s ->
            telephoneBean.tel = s.toString()
        }
        binding.tvSelect.setOnClickListener {
            if (!checkContactsReadPermission()) {
                launcher.launch(arrayOf(android.Manifest.permission.READ_CONTACTS)) {
                    if (checkContactsReadPermission()) {
                        pickTel()
                    }
                }
            } else {
                pickTel()
            }

        }
        binding.flQueren.setOnClickListener {
            if (telephoneBean.tel.isEmpty()) {
                toast("Enter phone number", true)
                return@setOnClickListener
            }
            if (AdConfigBean.adsConfigBean.functionUseShowAd) {
                AdsMgr.showInsert(this, true, object : AdsShowCallBack() {
                    override fun close(where: Int) {
                        jumpCodeActivity()
                    }

                    override fun failed(where: Int) {
                        jumpCodeActivity()
                    }

                    override fun googleFailed(where: Int) {
                        jumpCodeActivity()
                    }
                })
            } else {
                jumpCodeActivity()
            }
        }
    }

    fun jumpCodeActivity() {
        startActivity(Intent(this@TelephoneActivity, TelephoneCodeActivity::class.java).apply {
            putExtra("data", Gson().toJson(telephoneBean))
        })
        finish()
    }

    private fun pickTel() {
        val intent = intentPickContacts()
        try {
            launcher.launch(intent) { result ->
                val contactUri: Uri? = result.data?.data
                LogEx.logDebug(TAG, "")
                if (contactUri != null) {
                    lifecycleScope.launch(Dispatchers.IO) {
                        val tel = queryTel(contactUri) ?: ""
                        telephoneBean.tel = tel
                        launch(Dispatchers.Main) {
                            binding.edit.setText(tel)
                        }
                    }
                }

            }
        } catch (e: Exception) {
            toast("No application was found to handle the contacts pick", true)
        }
    }


}