package com.base.scanqr.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.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.WebsiteUIBean
import com.base.scanqr.databinding.ActivityWebsiteBinding
import com.base.scanqr.utils.BarUtils
import com.base.scanqr.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 {
            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()
                }
            })
        }
        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://")) {
                startActivity(Intent(this, WebsiteCodeActivity::class.java).apply {
                    putExtra("data", Gson().toJson(websiteUIBean))
                })
                finish()
            } else {
                toast("This is not a valid website address", true)
            }

        }
    }

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