package com.base.appzxhy.ui.main

import android.graphics.Color
import android.view.LayoutInflater
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.base.appzxhy.R
import com.base.appzxhy.base.BaseActivity
import com.base.appzxhy.bean.HomeTabUIBean
import com.base.appzxhy.databinding.ActivityMainBinding
import com.base.appzxhy.databinding.ItemHomeTabBinding
import com.base.appzxhy.ui.vm.ShortPlayViewModel
import com.base.appzxhy.utils.LogEx
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.gyf.immersionbar.ktx.immersionBar

class MainActivity : BaseActivity<ActivityMainBinding>(ActivityMainBinding::inflate) {

    private val homeTabs by lazy {
        arrayOf(
            HomeTabUIBean(R.drawable.bg_selector_home, getString(R.string.home)),
            HomeTabUIBean(R.drawable.bg_selector_foryou, getString(R.string.for_you)),
            HomeTabUIBean(R.drawable.bg_selector_mylist, getString(R.string.my_list)),
            HomeTabUIBean(R.drawable.bg_selector_rewards, getString(R.string.rewards)),
            HomeTabUIBean(R.drawable.bg_selector_profile, "Profile"),
        )
    }
    val viewModel by lazy(LazyThreadSafetyMode.NONE) {
        ViewModelProvider(this)[ShortPlayViewModel::class.java]
    }

    private val homeFragment by lazy(LazyThreadSafetyMode.NONE) {
        HomeFragment()
    }
    private val slideFragment by lazy(LazyThreadSafetyMode.NONE) {
        PlaySlideFragment()
    }
    private val myListFragment by lazy(LazyThreadSafetyMode.NONE) {
        MyListFragment()
    }
    private val rewardsFragment by lazy(LazyThreadSafetyMode.NONE) {
        RewardsFragment()
    }
    private val profileFragment by lazy(LazyThreadSafetyMode.NONE) {
        ProfileFragment()
    }
    private val fragments = arrayOf(homeFragment, slideFragment, myListFragment, rewardsFragment, profileFragment)
    private var pageAdapter: FragmentStateAdapter? = null

    override fun initView() {
        super.initView()
        initViewPager2()
        initTab()
    }

    private fun initViewPager2() {
        pageAdapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int {
                return fragments.size
            }

            override fun createFragment(position: Int): Fragment {
                return fragments[position]
            }

        }
        binding.viewPager2.run {
            isUserInputEnabled = false
            //https://www.jianshu.com/p/f69bd30cf5b0
            //FragmentStateAdapter 这里必须传人fragment
            adapter = pageAdapter
        }
        binding.viewPager2.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels)
            }

            override fun onPageSelected(position: Int) {
            }
        })
    }

    private fun initTab() {
        homeTabs.forEachIndexed { index, homeTab ->
            val tab = binding.tabLayout.newTab()
            val view = LayoutInflater.from(this).inflate(R.layout.item_home_tab, null)
            val tabBinding = ItemHomeTabBinding.bind(view)
            tabBinding.ivIcon.setImageResource(homeTab.icon)
            tabBinding.tvTab.text = homeTab.tab
//            tabBinding.tvTab.setTextColor(ContextCompat.getColor(this, R.color.color_tab_home))
            tab.customView = tabBinding.root
            tab.id = index
            binding.tabLayout.addTab(tab)
        }
        binding.tabLayout.addOnTabSelectedListener(object : OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                tab?.setSelected()
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                tab?.setUnselected()
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }
        })

        val defaultTab = binding.tabLayout.getTabAt(0)
        defaultTab?.select()
        defaultTab?.setSelected()
    }


    fun TabLayout.Tab.setUnselected() {
        val textView = customView?.findViewById<TextView>(R.id.tv_tab)
        LogEx.logDebug(TAG, "setUnselected ${textView?.text}")
        textView?.setTextColor(Color.parseColor("#A7A7A7"))
    }

    fun TabLayout.Tab.setSelected() {
        val textView = customView?.findViewById<TextView>(R.id.tv_tab)
        LogEx.logDebug(TAG, "setSelected ${textView?.text}")
        textView?.setTextColor(Color.parseColor("#FFFFFF"))
        binding.viewPager2.setCurrentItem(this.id, false)
    }

    override fun configSystemBar() {
        immersionBar {
            statusBarColor("#00FFFFFF")
            statusBarDarkFont(false)
        }
    }

}