package com.keyboard.bubble.skin.keyboard.fagment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import com.keyboard.bubble.skin.keyboard.App
import com.keyboard.bubble.skin.keyboard.bean.ParentBean
import com.keyboard.bubble.skin.keyboard.databinding.LayoutThemeFragmentBinding
import com.keyboard.bubble.skin.keyboard.myada.AdapterTab

class ViewPage : Fragment() {


    private lateinit var vb: LayoutThemeFragmentBinding


    private lateinit var viewContent: ViewPager
    lateinit var viewAllList: MutableList<ParentBean>
    private lateinit var adapterTab: AdapterTab



    companion object {

        @JvmStatic
        fun newInstance() =
            ViewPage()
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        vb = LayoutThemeFragmentBinding.inflate(layoutInflater)
        viewContent = vb.viewPager
        setTabRecycler()
        setContent()
        return vb.root
    }


    private fun setTabRecycler() {
        viewAllList = App.list
        adapterTab = AdapterTab(
            requireContext()
        ).apply {
            tabTitleName(viewAllList)
            setChangeListener(object : AdapterTab.OnChangListener {
                override fun onChange(posit: Int) {
                    viewContent.setCurrentItem(posit, false)
                }

            })
        }
        vb.tabRecycler.run {
            adapter = adapterTab
            layoutManager = LinearLayoutManager(requireContext(), RecyclerView.HORIZONTAL, false)
        }
    }

    private fun setContent() {
        val fragList = mutableListOf<Fragment>()
        for (index in 0 until viewAllList.size) {
            fragList.add(MainPage.newInstance(index))
        }

        viewContent.adapter = object : FragmentStatePagerAdapter(
            childFragmentManager,
            BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
        ) {
            override fun getCount(): Int = fragList.size

            override fun getItem(position: Int): Fragment = fragList[position]

        }
        var curViewPager = 0
        viewContent.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                if (curViewPager != position) {
                    adapterTab.setSelectedPos(position)
//                    tabRecycler.scrollToPosition(position)
                    curViewPager = position
                }

            }

            override fun onPageSelected(position: Int) {

            }

            override fun onPageScrollStateChanged(state: Int) {

            }

        })
    }


}