package com.base.appzxhy.ui.category

import android.widget.TextView
import androidx.activity.addCallback
import androidx.core.content.ContextCompat
import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.angcyo.tablayout.delegate2.ViewPager2Delegate
import com.base.appzxhy.R
import com.base.appzxhy.base.BaseActivity
import com.base.appzxhy.databinding.ActivityCategoryBinding
import com.base.appzxhy.ui.vm.ShortPlayViewModel
import com.base.appzxhy.utils.BarUtils
import com.base.appzxhy.utils.LogEx
import com.base.appzxhy.utils.XmlEx.inflate
import com.bytedance.sdk.shortplay.api.ShortPlay
import com.gyf.immersionbar.ktx.immersionBar

class CategoryActivity : BaseActivity<ActivityCategoryBinding>(ActivityCategoryBinding::inflate) {


    val viewModel by lazy(LazyThreadSafetyMode.NONE) {
        ViewModelProvider(this)[ShortPlayViewModel::class.java]
    }

    private var pageAdapter: FragmentStateAdapter? = null


    override fun initView() {
        super.initView()
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())

        binding.dslTabLayout.tabIndicator.indicatorDrawable =
            ContextCompat.getDrawable(this, R.drawable.indicator_bottom_line)
        binding.dslTabLayout.tabIndicator.indicatorEnableFlow = true
        binding.dslTabLayout.tabIndicator.ignoreChildPadding = true


        val textLength = resources.getDimensionPixelOffset(R.dimen.dp_5)
        viewModel.getCategoryList("en") { list ->
            runOnUiThread {
                list.forEach {
                    val view = R.layout.tab_text.inflate(this)
                    view.findViewById<TextView>(R.id.tv).text = it.name
                    binding.dslTabLayout.addView(view)
                }
                binding.dslTabLayout.configTabLayoutConfig {
                    onSelectIndexChange = { fromIndex, selectIndexList, reselect, fromUser ->
                        val toIndex = selectIndexList.first()
                        val name = list[toIndex].name
                        LogEx.logDebug(TAG, "length=${name.length}")

                        binding.dslTabLayout.tabIndicator.indicatorWidth = name.length * textLength
                    }
                }
                ViewPager2Delegate.install(binding.viewPager2, binding.dslTabLayout)

                initViewPager2(list)
            }
        }

    }

    private fun initViewPager2(list: List<ShortPlay.ShortPlayCategory>) {
        pageAdapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int {
                return list.size
            }

            override fun createFragment(position: Int): Fragment {
                val category = list[position]
                return CategoryPageFragment(category)
            }
        }
        binding.viewPager2.run {
            isUserInputEnabled = true
            //https://www.jianshu.com/p/f69bd30cf5b0
            //FragmentStateAdapter 这里必须传人fragment
            adapter = pageAdapter
        }
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            finish()
        }
        binding.flBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
    }

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

}