package com.base.scanqrclear.ui.text

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.TextUIBean
import com.base.scanqrclear.bean.config.AdConfigBean
import com.base.scanqrclear.databinding.ActivityTextBinding
import com.base.scanqrclear.utils.BarUtils
import com.base.scanqrclear.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 {
            if (AdConfigBean.adsConfigBean.functionBackShowAd) {
                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()
                    }
                })
            } else {
                finishToMainTop()
            }
        }
        binding.flBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.flQueren.setOnClickListener {
            if (binding.edit.text.isEmpty()) {
                toast("Content is empty")
                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()
            }
        }
    }

    private fun jumpCodeActivity() {
        startActivity(Intent(this, TextCodeActivity::class.java).apply {
            val textUIBean = TextUIBean()
            textUIBean.content = binding.edit.text.toString()
            putExtra("data", Gson().toJson(textUIBean))
        })
        finish()
    }
}