package com.ccwangluo.accelerator.ui

import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.viewpager.widget.ViewPager
import com.ccwangluo.cc_quickly.databinding.FragmentBtnNavBinding


import com.ccwangluo.cc_quickly.ui.quick.MineFragment
import com.ccwangluo.cc_quickly.ui.quick.NewsFragment
import com.ccwangluo.cc_quickly.ui.quick.QuickFragment
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.xuexiang.xpage.annotation.Page
import com.xuexiang.xpage.base.XPageFragment
import com.xuexiang.xpage.utils.TitleBar
import com.xuexiang.xui.adapter.FragmentAdapter
import com.xuexiang.xutil.common.CollectionUtils


@Page(name = "主页")
class BottomNavigationFragment : XPageFragment(), ViewPager.OnPageChangeListener,
    BottomNavigationView.OnNavigationItemSelectedListener {

    lateinit var binding: FragmentBtnNavBinding

    override fun inflateView(inflater: LayoutInflater?, container: ViewGroup?): View {
        binding = FragmentBtnNavBinding.inflate(inflater!!, container, false)
        return binding.root
    }
    override fun initTitleBar(): TitleBar? {
        //不使用@Page标注的一定要注意覆盖这个方法
        return null
    }

    var titles = arrayOf("加速", "资讯", "我的")

    override fun initViews() {

        val adapter = FragmentAdapter<XPageFragment>(
            childFragmentManager
        )
        adapter.addFragment(QuickFragment.newInstance(), titles[0])
        adapter.addFragment(NewsFragment.newInstance(), titles[1])
        adapter.addFragment(MineFragment.newInstance(), titles[2])
        binding.viewPager.setOffscreenPageLimit(titles.size - 1);
        binding.viewPager.adapter = adapter

    }

    override fun initListeners() {
        binding.viewPager.addOnPageChangeListener(this)
        binding.bottomNavigation.setOnNavigationItemSelectedListener(this)
        binding.bottomNavigation.setItemIconTintList(null);
        binding.bottomNavigation.itemTextColor

    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)

    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
        binding.bottomNavigation.menu.getItem(position).isChecked = true
    }

    override fun onPageSelected(position: Int) {

    }

    override fun onPageScrollStateChanged(index: Int) {

    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        val index: Int = CollectionUtils.arrayIndexOf(titles, item.getTitle())
        if (index != -1) {
            binding.viewPager.setCurrentItem(index, true)
            return true
        }
        return false
    }

}