package com.base.scanqrclear.ui.website

import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
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.WebsiteUIBean
import com.base.scanqrclear.bean.config.AdConfigBean
import com.base.scanqrclear.databinding.ActivityWebsiteBinding
import com.base.scanqrclear.utils.BarUtils
import com.base.scanqrclear.utils.ToastUtils.toast
import com.google.gson.Gson
import com.gyf.immersionbar.ktx.immersionBar

class WebsiteActivity : BaseActivity<ActivityWebsiteBinding>(ActivityWebsiteBinding::inflate) {

    private var websiteUIBean = WebsiteUIBean()

    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@WebsiteActivity, 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.edit.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                websiteUIBean.http = s.toString()
            }
        })
        binding.flQueren.setOnClickListener {
            if (websiteUIBean.http.startsWith("http://") || websiteUIBean.http.startsWith("https://")) {
                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()
                }
            } else {
                toast("This is not a valid website address", true)
            }

        }
    }

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

    override fun configSystemBar() {
        immersionBar {
            statusBarColor("#FFFFFFFF")
            statusBarDarkFont(true)
        }
    }
}