package com.base.scanqr.ui.email

import android.content.Intent
import androidx.activity.addCallback
import androidx.core.view.updatePadding
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.EmailUIBean
import com.base.scanqr.databinding.ActivityEmailBinding
import com.base.scanqr.utils.BarUtils
import com.base.scanqr.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 {
            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()
                }
            })
        }
        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()
            startActivity(Intent(this, EmailCodeActivity::class.java).apply {
                putExtra("data", Gson().toJson(email))
            })
        }
    }
}