package com.quiescent.wallpaper.collection.test.ui

import android.util.Log
import android.widget.ImageView
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.quiescent.wallpaper.collection.test.WallpaperCollection
import com.quiescent.wallpaper.collection.test.R
import com.quiescent.wallpaper.collection.test.databinding.ActivityMainBinding
import com.quiescent.wallpaper.collection.test.databinding.ItemTabBinding

class MainActivity : BaseActivity<ActivityMainBinding>(), OnTabSelectedListener {

    override fun getViewBinding(): ActivityMainBinding {
        return ActivityMainBinding.inflate(layoutInflater)
    }

    override fun onViewUi() {
        val list = mutableListOf<Fragment>()
        val wallpaperDataMutableList = WallpaperCollection.instance.list
        wallpaperDataMutableList.shuffle()
        wallpaperDataMutableList.forEachIndexed { index, wallpaprData ->
            val newTab = binding.tab.newTab()
            binding.tab.addTab(newTab)
            newTab.customView = ItemTabBinding.inflate(layoutInflater).apply {
                tvCategoryName.text = wallpaprData.name
            }.root
            list.add(HomeListFragment.newInstance(wallpaprData.name))
        }
        binding.vp.run {
            offscreenPageLimit = 1
            adapter = object : FragmentStatePagerAdapter(supportFragmentManager) {
                override fun getCount(): Int = list.size

                override fun getItem(position: Int): Fragment = list[position]
                override fun getPageTitle(position: Int): CharSequence? {
                    return wallpaperDataMutableList[position].name
                }

            }
            addOnPageChangeListener(object : OnPageChangeListener {
                override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int
                ) {
                }

                override fun onPageSelected(position: Int) {
                    binding.tab.getTabAt(position)?.select()
                }

                override fun onPageScrollStateChanged(state: Int) {

                }

            })
        }

        binding.tab.addOnTabSelectedListener(this)
        binding.tab.getTabAt(0)?.select()
    }

    override fun onTabSelected(tab: TabLayout.Tab?) {
        tab?.let {
            binding.vp.setCurrentItem(it.position, false)
        }
        tab?.customView?.run {
            findViewById<ImageView>(R.id.view_indicator)?.let {
                it.isVisible = true
            }
        }
    }

    override fun onTabUnselected(tab: TabLayout.Tab?) {
        tab?.customView?.run {
            findViewById<ImageView>(R.id.view_indicator)?.let {
                it.isVisible = false
            }
        }
    }

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

    }

}