package com.base.browserwhite.ui.activity.webstore

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.Drawable
import androidx.core.view.updatePadding
import androidx.lifecycle.lifecycleScope
import com.base.browserwhite.R
import com.base.browserwhite.bean.WebCategoryBean
import com.base.browserwhite.bean.WebSiteGroupBean
import com.base.browserwhite.bean.webCategoryBeanList
import com.base.browserwhite.bean.webSiteGroupBeanList
import com.base.browserwhite.databinding.ActivityWevStoreBinding
import com.base.browserwhite.databinding.ItemTabWebstoreBinding
import com.base.browserwhite.ui.activity.BaseActivity
import com.base.browserwhite.ui.activity.webbrowser.WebBrowserActivity
import com.base.browserwhite.ui.adapter.WebSiteGroupAdapter
import com.base.browserwhite.utils.BarUtils
import com.base.browserwhite.utils.LogEx
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


class WevStoreActivity : BaseActivity<ActivityWevStoreBinding>() {

    private val TAG = "WevStoreActivity"

    override val binding: ActivityWevStoreBinding by lazy {
        ActivityWevStoreBinding.inflate(layoutInflater)
    }
    private val webCategoryBean: ArrayList<WebCategoryBean> = arrayListOf()
    private val webSiteGroupBean: ArrayList<WebSiteGroupBean> = arrayListOf()
    private lateinit var adapter: WebSiteGroupAdapter

    @SuppressLint("SetTextI18n")
    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())

        adapter = WebSiteGroupAdapter(clickAction = { bean ->
            startActivity(Intent(this, WebBrowserActivity::class.java).apply {
                putExtra("uri", bean.url)
            })
            finish()
        })
        binding.rv.adapter = adapter
        binding.tabLayout.observeIndexChange { fromIndex, toIndex, reselect, fromUser ->
            if (!reselect) {
                binding.rv.smoothScrollToPosition(toIndex)
            }
        }

        initData()
    }

    override fun initListener() {
        super.initListener()

        binding.ivGuanbi.setOnClickListener {
            finishToMain()
        }
    }

    private fun initData() = lifecycleScope.launch(Dispatchers.IO) {
        webCategoryBean.addAll(webCategoryBeanList)
        webSiteGroupBean.addAll(webSiteGroupBeanList)

        val imageSize = this@WevStoreActivity.resources.getDimensionPixelSize(R.dimen.dp_30)
        launch(Dispatchers.Main) {
            webCategoryBean.forEach { bean ->
//                LogEx.logDebug(TAG, bean.iconUrl)
                val tabBinding = ItemTabWebstoreBinding.inflate(layoutInflater)
                tabBinding.tv.text = bean.name

                Glide.with(this@WevStoreActivity).asDrawable().load(bean.iconUrl).into(
                    object : CustomTarget<Drawable>() {
                        override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
                            LogEx.logDebug(TAG, "resource=${resource.intrinsicWidth} ${resource.intrinsicHeight}")
                            /// 这一步必须要做,否则不会显示.
                            resource.setBounds(0, 0, imageSize, imageSize)

                            // 设置TextView的左侧图标
                            tabBinding.tv.setCompoundDrawables(resource, null, null, null)

                            // 设置图片与文本之间的间距
                            val padding = 10 // 例如10dp的间距
                            tabBinding.tv.setCompoundDrawablePadding(padding)

//                            tabBinding.tv.invalidate()
                        }

                        override fun onLoadCleared(placeholder: Drawable?) {
                        }

                    }
                )
                binding.tabLayout.addView(tabBinding.root)
            }

            adapter.setData(webSiteGroupBeanList)
        }

    }

}