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.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())

    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            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))
                })
            } else {
                toast("This is not a valid website address", true)
            }

        }
    }

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