package com.keyboard.skinning.cool.fragment

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager.widget.ViewPager
import com.keyboard.skinning.cool.App
import com.keyboard.skinning.cool.adpter.AdapterParent
import com.keyboard.skinning.cool.data.ClassData
import com.keyboard.skinning.cool.databinding.FragmentMainBinding
import com.keyboard.skinning.cool.listener.ClickSeeAllListener

class FragmentMain : Fragment() {


    private lateinit var vb: FragmentMainBinding


    private lateinit var viewContent: ViewPager
    lateinit var viewAllList: MutableList<ClassData>
    private lateinit var adapterParent: AdapterParent



    companion object {

        @JvmStatic
        fun newInstance() =
            FragmentMain()
    }

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


    private fun setTabRecycler() {
        viewAllList = App.list
        adapterParent = AdapterParent(
            requireContext(), viewAllList
        ).apply {
            setClickAction(object : ClickSeeAllListener {
                override fun OnClickSeeAll(name: String) {
                    startActivity(Intent(requireContext(),
                        com.keyboard.skinning.cool.activity.AllActivity::class.java).apply {
                        putExtra(com.keyboard.skinning.cool.activity.AllActivity.KEY_NAME,name)
                    })

                }

            })
        }
        vb.tabRecycler.run {
            adapter = adapterParent
            layoutManager = LinearLayoutManager(requireContext())
        }
    }

    private fun setContent() {
//        val fragList = mutableListOf<Fragment>()
//        for (index in 0 until viewAllList.size) {
//            fragList.add(FragThemes.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) {
//                    adapterTabName.setSelectedPos(position)
////                    tabRecycler.scrollToPosition(position)
//                    curViewPager = position
//                }
//
//            }
//
//            override fun onPageSelected(position: Int) {
//
//            }
//
//            override fun onPageScrollStateChanged(state: Int) {
//
//            }
//
//        })
    }


}