package com.zxhl.main.page.view

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.flyco.tablayout.CommonTabLayout
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.zxhl.cms.common.base.BaseFragment
import com.zxhl.main.R

abstract class BaseViewPagerFragment : BaseFragment() {

    protected var viewPager: ViewPager2? = null
    protected var pageChangeCallback: PageChangeCallback? = null

    protected var tabLayout: CommonTabLayout? = null
    protected val adapter: VpAdapter by lazy { VpAdapter(requireActivity()!!).apply { addFragments(createFragments) } }


    protected var offscreenPageLimit =1

    abstract  val createTitles: ArrayList<CustomTabEntity>

    abstract val createFragments: Array<Fragment>

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        setupViews()
    }

    override fun onDestroy() {
        super.onDestroy()
        pageChangeCallback?.run { viewPager?.unregisterOnPageChangeCallback(this) }
        pageChangeCallback = null
    }

    open fun setupViews() {
        initViewPager()
    }

    protected fun initViewPager() {
        viewPager = fragmentView?.findViewById(R.id.viewPager)
        tabLayout = fragmentView?.findViewById(R.id.id_fragment_home_tab)
        viewPager?.offscreenPageLimit = offscreenPageLimit
        //val adapter = FragmentAdapter(childFragmentManager)
        viewPager?.adapter = adapter

//        for (i in createFragments.indices) {
//            adapter?.addFrag(createFragments[i], createTitles[i].tabTitle)
//        }
        tabLayout?.setTabData(createTitles)
        tabLayout?.setOnTabSelectListener(object : OnTabSelectListener {

            override fun onTabSelect(position: Int) {
                viewPager?.currentItem = position
            }

            override fun onTabReselect(position: Int) {

            }
        })
        pageChangeCallback = PageChangeCallback()
        viewPager?.registerOnPageChangeCallback(pageChangeCallback!!)
//        adapter?.notifyDataSetChanged()
//        tabLayout?.setOverScrollMode(View.OVER_SCROLL_NEVER)
    }
    inner class PageChangeCallback : ViewPager2.OnPageChangeCallback() {

        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
            tabLayout?.currentTab = position
        }
    }
    inner class VpAdapter(fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) {

        private val fragments = mutableListOf<Fragment>()

        fun addFragments(fragment: Array<Fragment>) {
            fragments.addAll(fragment)
        }

        override fun getItemCount() = fragments.size

        override fun createFragment(position: Int) = fragments[position]
    }

}