package com.module.backstage.fragment.other


import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.fragment.app.viewModels
import androidx.navigation.Navigation
import androidx.recyclerview.widget.GridLayoutManager
import com.module.backstage.R
import com.module.backstage.adapter.TestAdapter
import com.module.backstage.databinding.BackstageFragmentOtherBinding
import com.quyunshuo.androidbaseframemvvm.common.listener.AdapterClickListener
import com.module.backstage.vm.OtherFragmentVM
import com.module.pay.common.OtherEnum
import com.quyunshuo.androidbaseframemvvm.base.ktx.observeLiveData
import com.quyunshuo.androidbaseframemvvm.common.ui.BaseFragment
import com.quyunshuo.androidbaseframemvvm.common.util.ToastUtil
import com.quyunshuo.androidbaseframemvvm.common.util.UiUtil
import com.quyunshuo.androidbaseframemvvm.common.util.XLogUtil
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 OtherFragment : BaseFragment<BackstageFragmentOtherBinding, OtherFragmentVM>() {
    private val TAG = "OtherFragment"
    private val CHANNELS = arrayOf(
        UiUtil.getStringRes(R.string.base_other1),
        UiUtil.getStringRes(R.string.base_other2),
        UiUtil.getStringRes(R.string.base_other3),
        UiUtil.getStringRes(R.string.base_other4),
        UiUtil.getStringRes(R.string.base_other5),
        UiUtil.getStringRes(R.string.base_other6)
    )
    private val fragmentid = arrayOf(
        R.id.backstage_systemsettingsfragment,
        R.id.backstage_systemsettingsfragment,
        R.id.backstage_systemsettingsfragment,
        R.id.backstage_systemsettingsfragment,
        R.id.backstage_systemsettingsfragment,
        R.id.backstage_systemsettingsfragment
    )
    private val mDataList: List<String> = Arrays.asList(*CHANNELS)

    override val mViewModel: OtherFragmentVM by viewModels()
    override fun createVB() = BackstageFragmentOtherBinding.inflate(layoutInflater)
    private var otherAdapter: TestAdapter? = null


    override fun BackstageFragmentOtherBinding.initView() {
        keyboardView?.removeAllEditText()
        initCommonNavigator()
        ivSearch.setOnClickListener {
            val searchText = etSearch.text.trim()
            XLogUtil.d("searchText: $searchText" + ("" == searchText))
//
            //进行数据的搜索
            mViewModel.search(searchText)
        }

        with(rvOtherSearch) {
            //设置布局排列方式，默认垂直排列
            val gridLayoutManager: GridLayoutManager =
                GridLayoutManager(
                    this@OtherFragment.context,
                    2,
                    androidx.recyclerview.widget.GridLayoutManager.VERTICAL,
                    false
                )
            layoutManager = gridLayoutManager

            mViewModel.list.add(OtherEnum.CHANGE)
            otherAdapter = TestAdapter(mViewModel.list)
            otherAdapter!!.setItemListener(object : AdapterClickListener {
                override fun onClickListener(view: View?, position: Int, data: String?) {
                    if (data == null) {
                        //Todo 需要做处理，部分的输入项，不需要添加到这个自定义键盘里面来。
                        addAllKeyboardView(view)
                        return
                    }
                    if ("" == data) {
                        ToastUtil.switchToastStyleToWarn(UiUtil.getStringRes(R.string.base_backstage_data_null_tips))
                        return
                    }
                }

            })

            adapter = otherAdapter
        }
    }

    override fun initObserve() {
        observeLiveData(mViewModel.readAllDataSuccess, ::searchResult)
    }

    /**
     * 搜索结果
     */
    fun searchResult(i: Int) {
        if (i == 0) {
            ToastUtil.switchToastStyleToWarn("搜索结果为空")
            mBinding.rvOtherSearch.visibility = View.GONE
            mBinding.fcvOther.visibility = View.VISIBLE
            return
        }
        mBinding.rvOtherSearch.visibility = View.VISIBLE

        //mBinding.fcvOther.visibility = View.GONE
        toggleFragmentVisibility(false)
        Log.d(TAG, "searchResult: " + mViewModel.list)
        otherAdapter?.setData(mViewModel.list)
       // otherAdapter?.notifyDataSetChanged()

    }
    fun toggleFragmentVisibility(show: Boolean) {
        val fragment = childFragmentManager.findFragmentById(R.id.fcv_other)
        fragment?.let {
            childFragmentManager.beginTransaction().apply {
                if (show) {
                    show(it)
                } else {
                    hide(it)
                }
            }.commit()
        }
    }

    override fun initRequestData() {
    }

    /**
     * 初始化 commonNavigator
     */
    private fun initCommonNavigator() {
        val commonNavigator = CommonNavigator(this@OtherFragment.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.magicOther.onPageSelected(index)
                    mBinding.magicOther.onPageScrolled(index, 0.0F, 0);
                    val navController =
                        Navigation.findNavController(requireActivity(), mBinding.fcvOther.id)
                    var bundle = Bundle()
                    bundle.putInt("FUNCTION", index)
                    navController.navigate(fragmentid[index], bundle)
                    mBinding.rvOtherSearch.visibility = View.GONE
                   // mBinding.fcvOther.visibility = View.VISIBLE
                    toggleFragmentVisibility(true)
                }
                return simplePagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator {
                val indicator = WrapPagerIndicator(context)
                indicator.fillColor = Color.parseColor("#ebe4e3")
                return indicator
            }
        }
        mBinding.magicOther.navigator = commonNavigator
    }

    override fun onDestroyView() {
        mBinding.rvOtherSearch.adapter = null
        super.onDestroyView()
    }
}