package com.dahe.gldriver.ui.waybill

import android.content.Context
import android.graphics.Color
import androidx.fragment.app.Fragment
import com.dahe.gldriver.R
import com.dahe.gldriver.databinding.FragmentWaybillBinding
import com.dahe.gldriver.ui.waybill.fragment.AllWaybillFragment
import com.dahe.mylibrary.base.BaseFragment
import com.dahe.mylibrary.callback.RefreshCallBack
import net.lucode.hackware.magicindicator.FragmentContainerHelper
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView


/**
 * @ClassName HomeFragment
 * @Author 用户
 * @Date 2023/9/21 10:13
 * @Description TODO
 */
class WaybillFragment : BaseFragment<FragmentWaybillBinding>(), RefreshCallBack {

    private val mFragmentContainerHelper = FragmentContainerHelper()
    var mFragments = listOf<Fragment>(AllWaybillFragment(),AllWaybillFragment(),AllWaybillFragment(),AllWaybillFragment(),AllWaybillFragment())
    val titles = mutableListOf(
        "全部",
        "待装货",
        "待卸货",
        "待评价",
        "取消"
    )

    override fun onFragmentVisibleChange(isVisible: Boolean) {
        if (isVisible) {
        }

    }

    override fun onFragmentFirstVisible() {
        initRecy()



        mFragmentContainerHelper.handlePageSelected(0,true)
        switchPages(0)

        var commonNavigator = CommonNavigator(mContext)
        commonNavigator.run {
            isAdjustMode = true
            adapter = object : CommonNavigatorAdapter() {
                override fun getCount(): Int {
                    return titles.size
                }

                override fun getTitleView(context: Context?, index: Int): IPagerTitleView {
                    val colorTransitionPagerTitleView = ColorTransitionPagerTitleView(context)
                    colorTransitionPagerTitleView.normalColor = Color.BLACK
                    colorTransitionPagerTitleView.selectedColor = Color.RED
                    colorTransitionPagerTitleView.text = titles[index]
                    colorTransitionPagerTitleView.setOnClickListener {
                        mFragmentContainerHelper.handlePageSelected(index)
                        switchPages(index)
                    }
                    return colorTransitionPagerTitleView
                }

                override fun getIndicator(context: Context?): IPagerIndicator {
//                    val indicator = LinePagerIndicator(context)
//                    indicator.mode = LinePagerIndicator.MODE_WRAP_CONTENT
//                    return indicator

                    val indicator = LinePagerIndicator(context)
                    indicator.mode = LinePagerIndicator.MODE_WRAP_CONTENT
                    indicator.yOffset =
                        UIUtil.dip2px(context, 3.0).toFloat()
                    indicator.setColors(Color.RED)
                    return indicator
                }

            }
        }

        binding.magicIndicator.navigator = commonNavigator
        mFragmentContainerHelper.attachMagicIndicator(binding.magicIndicator)
//        ViewPagerHelper.bind(binding.magicIndicator, binding.viewPager);
    }


    override fun getRefreshDate(stat: Int, page: Int, count: Int) {

    }

    private fun initRecy() {

    }
    private fun switchPages(index: Int) {
        val fragmentManager = requireActivity().supportFragmentManager
        val fragmentTransaction = fragmentManager.beginTransaction()
        var fragment: Fragment
        var i = 0
        val j: Int = mFragments.size
        while (i < j) {
            if (i == index) {
                i++
                continue
            }
            fragment = mFragments[i]
            if (fragment.isAdded) {
                fragmentTransaction.hide(fragment)
            }
            i++
        }
        fragment = mFragments[index]
        if (fragment.isAdded) {
            fragmentTransaction.show(fragment)
        } else {
            fragmentTransaction.add(R.id.fragmentContainer, fragment)
        }
        fragmentTransaction.commitAllowingStateLoss()
    }
}