package com.themes.fancy.keyboard.keyboard.viewui

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.themes.fancy.keyboard.keyboard.MyApplication
import com.themes.fancy.keyboard.keyboard.data.Class
import com.themes.fancy.keyboard.keyboard.databinding.LayoutThemeFragmentBinding
import com.themes.fancy.keyboard.keyboard.myada.TabAdapter

class FragmentTheme : Fragment() {


    private lateinit var vb: LayoutThemeFragmentBinding


    private lateinit var viewContent: ViewPager
    lateinit var viewAllList: MutableList<Class>
    private lateinit var tabAdapter: TabAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        arguments?.let {
//            paramIndex = it.getInt(ARG_PARAM1)
//        }
    }

    companion object {

        @JvmStatic
        fun newInstance(index: Int) =
            FragmentTheme().apply {
//                arguments = Bundle().apply {
//                    putInt(ARG_PARAM1, index)
//                }
            }
    }

    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 = MyApplication.list
        tabAdapter = TabAdapter(
            requireContext()
        ).apply {
            tabTitleName(viewAllList)
            setChangeListener(object : TabAdapter.OnChangListener {
                override fun onChange(posit: Int) {
                    viewContent.setCurrentItem(posit, false)
                }

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

    private fun setContent() {
        val fragList = mutableListOf<Fragment>()
        for (index in 0 until viewAllList.size) {
            fragList.add(FragmentMain.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) {
                    tabAdapter.setSelectedPos(position)
//                    tabRecycler.scrollToPosition(position)
                    curViewPager = position
                }

            }

            override fun onPageSelected(position: Int) {

            }

            override fun onPageScrollStateChanged(state: Int) {

            }

        })
    }


}