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.AcceleratorFragment
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.ccwangluo.accelerator.utils.datareport.DataRePortUtils
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 acceleratorViewModel: AccelertorViewModel
    lateinit var binding: FragmentBtnNavBinding

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

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

    var titles = arrayOf("加速", "资讯", "我的")
    var fragments = arrayOf(
        AcceleratorFragment.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 {
                    acceleratorViewModel.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)
            when(index){
                 0 -> DataRePortUtils.report("st_speed_tab_clk")
                 1 -> DataRePortUtils.report("st_msg_tab_clk")
            }
            return true
        }
        return false
    }

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

}