package com.base.scanqrclear.ui.email

import android.content.Intent
import androidx.activity.addCallback
import androidx.core.view.updatePadding
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.EmailUIBean
import com.base.scanqrclear.bean.config.AdConfigBean
import com.base.scanqrclear.databinding.ActivityEmailBinding
import com.base.scanqrclear.utils.BarUtils
import com.base.scanqrclear.utils.ToastUtils.toast
import com.google.gson.Gson

class EmailActivity : BaseActivity<ActivityEmailBinding>(ActivityEmailBinding::inflate) {

    private val email = EmailUIBean()

    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@EmailActivity, 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.flQueren.setOnClickListener {
            if (!binding.editAddress.text.contains("@")) {
                toast("This is not a valid email address", true)
                return@setOnClickListener
            }
            if (binding.editSubject.text.isEmpty()) {
                toast("Enter subject email", true)
                return@setOnClickListener
            }
            if (binding.editMessage.text.isEmpty()) {
                toast("Enter message email", true)
                return@setOnClickListener
            }
            email.address = binding.editAddress.text.toString()
            email.subject = binding.editSubject.text.toString()
            email.message = binding.editMessage.text.toString()
            val activity = this
            if (AdConfigBean.adsConfigBean.functionUseShowAd) {
                AdsMgr.showInsert(activity, true, object : AdsShowCallBack() {
                    override fun close(where: Int) {
                        jumpCodeActivity()
                    }

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

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

        }
    }

    private fun jumpCodeActivity() {
        startActivity(Intent(this, EmailCodeActivity::class.java).apply {
            putExtra("data", Gson().toJson(email))
        })
        finish()
    }
}