package com.module.backstage.fragment.product

import android.content.Context
import android.graphics.Color
import android.util.Log
import android.view.View
import androidx.core.view.get
import androidx.fragment.app.viewModels
import androidx.navigation.Navigation
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.module.backstage.R
import com.module.backstage.databinding.BackstageFragmentCottonCandyBinding
import com.module.backstage.enums.SettingItemEnum
import com.module.backstage.vm.ProductFragmentVM
import com.quyunshuo.androidbaseframemvvm.base.utils.SpUtils
import com.quyunshuo.androidbaseframemvvm.common.bean.SettingTypeBean
import com.quyunshuo.androidbaseframemvvm.common.constant.MMKVName
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 ProductFragment : BaseFragment<BackstageFragmentCottonCandyBinding, ProductFragmentVM>() {
    private val CHANNELS = arrayOf(
        SettingItemEnum.PRO_1,
        SettingItemEnum.PRO_2,
        SettingItemEnum.PRO_4
    )
    private val fragmentid = arrayOf(
        R.id.backstage_pricefragment,
        R.id.backstage_showproductfragment,
        R.id.backstage_debugproductfragment,
        )

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

    private var arrayListState: HashMap<Int, SettingTypeBean>? = null
    override val mViewModel: ProductFragmentVM by viewModels()
    override fun createVB() = BackstageFragmentCottonCandyBinding.inflate(layoutInflater)


    override fun BackstageFragmentCottonCandyBinding.initView() {
        showItem()
        initMagicIndicator8()
    }
    /**
     * 哪些item要显示
     */
    fun showItem(){
        val loginName = SpUtils.getString(MMKVName.LOGIN_USER, "")
        Log.d(TAG, "loginName: " + loginName)
        if (loginName != null && loginName.equals("Staff")) {
            val productStr = SpUtils.getString(MMKVName.SETTING_TYPE, "")
            if (productStr!="") {
                 arrayListState =  Gson().fromJson(
                    productStr,
                    object : TypeToken<HashMap<Int, SettingTypeBean>?>() {}.type
                )
            }

        }
    }

    override fun initObserve() {
    }


    override fun initRequestData() {
        mViewModel.getBanners()
        mViewModel.getArticleData()
    }

    private val TAG = "ProductFragment"
    private fun initMagicIndicator8() {
        val commonNavigator = CommonNavigator(this@ProductFragment.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 = UiUtil.getStringRes(mDataList[index].nameId)
                simplePagerTitleView.normalColor = Color.parseColor("#ffffff")
                simplePagerTitleView.selectedColor = Color.parseColor("#1969FF")
                simplePagerTitleView.setOnClickListener {
                    mBinding.magicIndicator8.onPageSelected(index)
                    mBinding.magicIndicator8.onPageScrolled(index, 0.0F, 0);


                    val navController = Navigation.findNavController(
                        requireActivity(),
                        mBinding.cottoncandyFragmenttcontainerview.id
                    )
                    navController.navigate(fragmentid[index])
                    Log.d(TAG, "getTitleView: ")
                }
                if (arrayListState!=null) {
                    val selected = arrayListState!![mDataList[index].id]?.isSelected
                    if (selected!=null &&!selected)simplePagerTitleView.visibility = View.GONE
                }
                return simplePagerTitleView
            }

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

}