package com.test.basd.smartjunkcleaner.activity.browser

import android.graphics.Bitmap
import android.graphics.Color
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.webkit.WebView
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import com.blankj.utilcode.util.BarUtils
import com.test.basd.smartjunkcleaner.databinding.ActivityLayoutSecureBrowserBinding
import com.test.basd.smartjunkcleaner.helps.BaseActivity

class SecureBrowserActivity : BaseActivity<ActivityLayoutSecureBrowserBinding>() {
    private val tabs = mutableListOf<BrowserView?>(null)
    private var currentTab = 0
    override val binding: ActivityLayoutSecureBrowserBinding by lazy {
        ActivityLayoutSecureBrowserBinding.inflate(layoutInflater)
    }

    override fun initView() {
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())


    }

    override fun initListener() {
        binding.idGoogle.setOnClickListener {

        }
        binding.idYoutube.setOnClickListener {
            loadUrl("https://www.youtube.com/")
        }
        binding.idFacebook.setOnClickListener {

        }
        binding.idAmazon.setOnClickListener {

        }
        binding.idInstagram.setOnClickListener {

        }
        binding.idTwitter.setOnClickListener {

        }
        binding.idReddit.setOnClickListener {

        }
        binding.idTiktok.setOnClickListener {

        }
    }
    private fun loadUrl(url: String) {


        binding.idContainer.isVisible = true
        binding.idContent.isVisible=false
        if (tabs[currentTab] == null) {
            tabs[currentTab] = BrowserView(
                object : BrowserView.WebViewListener {
                    override val activity = this@SecureBrowserActivity

                    override fun onProgressChanged(progress: Int) {

                    }

                    override fun onReceivedTitle(title: String) {
                       // binding.idAddressEdit.setText(title)
                    }

                    override fun onReceivedIcon(icon: Bitmap?) {
//                        icon?.let {
//                            binding.idIcon.setImageBitmap(it)
//                        }
                    }

                    override fun onPageStarted(url: String, icon: Bitmap?) {

                    }

                    override fun onPageFinished(view: WebView?, url: String) {


                        binding.idBack.isSelected = view?.canGoBack() == true
                        binding.idForward.isSelected = view?.canGoForward() == true

                       // refreshTabs()
                    }
                },
                url
            )
        } else {
            tabs[currentTab]?.loadUrl(url)
        }
        tabs[currentTab]?.isHome = false

        binding.idContainer.removeAllViews()
        binding.idContainer.addView(tabs[currentTab]?.getWebView(), 0, MATCH_PARENT)
    }
}