package com.ccwangluo.accelerator.ui

import android.view.*
import androidx.lifecycle.ViewModelProvider
import androidx.viewpager.widget.ViewPager
import com.ccwangluo.accelerator.databinding.FragmentBtnNavBinding

import com.ccwangluo.accelerator.ui.home.AccelertorFragment
import com.ccwangluo.accelerator.ui.home.AccelertorViewModel
import com.ccwangluo.accelerator.ui.home.MineFragment
import com.ccwangluo.accelerator.ui.home.NewsFragment
import com.ccwangluo.accelerator.utils.AcceleratorUtils
import com.ccwangluo.accelerator.utils.LoginUtils
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 = "main")
class BottomNavigationFragment : XPageFragment(), ViewPager.OnPageChangeListener,
    BottomNavigationView.OnNavigationItemSelectedListener {
    private lateinit var accelertorViewModel: AccelertorViewModel
    lateinit var binding: FragmentBtnNavBinding

    override fun inflateView(inflater: LayoutInflater?, container: ViewGroup?): View {
        binding = FragmentBtnNavBinding.inflate(inflater!!, container, false)
        accelertorViewModel =
            ViewModelProvider(requireActivity()).get(AccelertorViewModel::class.java)
        return binding.root
    }

    override fun initTitleBar(): TitleBar? {
        //不使用@Page标注的一定要注意覆盖这个方法
        return null
    }

    var titles = arrayOf("加速", "资讯", "我的")
    var fragments = arrayOf(
        AccelertorFragment.newInstance(),
        NewsFragment.newInstance(),
        MineFragment.newInstance()
    )

    override fun initViews() {

        val adapter = FragmentAdapter<XPageFragment>(
            childFragmentManager
        )
        adapter.addFragment(fragments[0], titles[0])
        adapter.addFragment(fragments[1], titles[1])
        adapter.addFragment(fragments[2], titles[2])
        binding.viewPager.setOffscreenPageLimit(titles.size - 1);
        binding.viewPager.adapter = adapter
        binding.viewPager.setNoScroll(true)
    }

    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)
        fragments[binding.viewPager.currentItem].onHiddenChanged(hidden)
        if (hidden) {
            getUserProfile()
        }
    }


    fun getUserProfile() {
        LoginUtils.token?.let {
            LoginUtils.getUserProfile(this) {
                it?.let {
                    accelertorViewModel.userProfile.postValue(it)
                }
            }
        }
    }

    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
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return super.onKeyDown(keyCode, event)
    }

}