package com.module.backstage.fragment.date

import android.content.Context
import android.graphics.Color
import android.util.Log
import androidx.fragment.app.viewModels
import androidx.navigation.Navigation
import com.module.backstage.R
import com.module.backstage.databinding.BackstageFragmentDateBinding
import com.module.backstage.vm.DateFragmentVM
import com.quyunshuo.androidbaseframemvvm.common.ui.BaseFragment
import com.quyunshuo.androidbaseframemvvm.common.util.UiUtil
import dagger.hilt.android.AndroidEntryPoint
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.WrapPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView
import java.util.Arrays

/**
 * 数据统计Fragment
 *
 * @author Qu Yunshuo
 * @since 2021/8/1 11:46 下午
 */
@AndroidEntryPoint
class DateFragment : BaseFragment<BackstageFragmentDateBinding, DateFragmentVM>() {
    private val CHANNELS = arrayOf(
        UiUtil.getStringRes(R.string.base_backstage_bar_chart_statistics),
        UiUtil.getStringRes(R.string.base_backstage_order_details)
    )
    private val fragmentid = arrayOf(
        R.id.backstage_histogramdatafragment,
        R.id.backstage_orderdatefragment

        )
    private val mDataList: List<String> = Arrays.asList(*CHANNELS)

    override val mViewModel: DateFragmentVM by viewModels()
    override fun createVB() = BackstageFragmentDateBinding.inflate(layoutInflater)


    override fun BackstageFragmentDateBinding.initView() {
//        bnTopnavigation.setOnNavigationItemSelectedListener {
//            //homeFragmentcontainerview1名字不能相同，不然有问题。会找到activity里面的。
//            val navController = Navigation.findNavController(requireActivity(),paramsetFragmenttcontainerview.id)
//            navController.navigate(it.itemId)
//            true
//        }
        initmagicIndicator2()
    }

    override fun initObserve() {
    }

    override fun initRequestData() {
    }

    private val TAG = "ProductFragment"
    private fun initmagicIndicator2() {
        val commonNavigator = CommonNavigator(this@DateFragment.context)
        commonNavigator.scrollPivotX = 0.35f
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount(): Int {
                return if (mDataList == null) 0 else mDataList.size
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val simplePagerTitleView = SimplePagerTitleView(context)
                simplePagerTitleView.textSize =  32f
                simplePagerTitleView.setPadding(20,0,20,0)
                simplePagerTitleView.text = mDataList[index]
                simplePagerTitleView.normalColor = Color.parseColor("#ffffff")
                simplePagerTitleView.selectedColor = Color.parseColor("#1969FF")
                simplePagerTitleView.setOnClickListener {
                    mBinding.magicIndicator2.onPageSelected(index)
                    mBinding.magicIndicator2.onPageScrolled(index, 0.0F, 0);


                    val navController = Navigation.findNavController(requireActivity(),mBinding.cottoncandyFragmenttcontainerview.id)
                    navController.navigate(fragmentid[index])
                    Log.d(TAG, "getTitleView: ")
                }
                return simplePagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator {
                val indicator = WrapPagerIndicator(context)
                indicator.fillColor = Color.parseColor("#ebe4e3")
                return indicator
            }
        }
        mBinding.magicIndicator2.navigator = commonNavigator
//        ViewPagerHelper.bind(mBinding.magicIndicator2, mBinding.viewPager)
    }

}