package com.base.scanqr.ui.text

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.TextUIBean
import com.base.scanqr.databinding.ActivityTextBinding
import com.base.scanqr.utils.BarUtils
import com.base.scanqr.utils.ToastUtils.toast
import com.google.gson.Gson

class TextActivity : BaseActivity<ActivityTextBinding>(ActivityTextBinding::inflate) {


    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@TextActivity, 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.edit.text.isEmpty()) {
                toast("Content is empty")
                return@setOnClickListener
            }
            startActivity(Intent(this, TextCodeActivity::class.java).apply {
                val textUIBean = TextUIBean()
                textUIBean.content = binding.edit.text.toString()
                putExtra("data", Gson().toJson(textUIBean))
            })
            finish()
        }
    }
}