package com.base.scanqr.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.scanqr.R
import com.base.scanqr.ads.AdsMgr
import com.base.scanqr.ads.AdsShowCallBack
import com.base.scanqr.base.BaseActivity
import com.base.scanqr.bean.TelephoneUIBean
import com.base.scanqr.databinding.ActivityTelephoneBinding
import com.base.scanqr.utils.BarUtils
import com.base.scanqr.utils.ContactUtils.queryTel
import com.base.scanqr.utils.IntentUtils.intentPickContacts
import com.base.scanqr.utils.LogEx
import com.base.scanqr.utils.PermissionUtils.checkContactsReadPermission
import com.base.scanqr.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 {
            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()
                }
            })
        }
        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
            }
            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)
        }
    }


}