package com.zj365.health.act

import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Route
import com.androidkun.xtablayout.XTabLayout
import com.xty.base.act.IBaseAct
import com.xty.base.vp2.VpAdapter
import com.xty.common.LogUtils
import com.xty.common.arouter.ARouterUrl
import com.zj365.health.R
import com.zj365.health.databinding.ActRankListBinding
import com.zj365.health.fragment.*
import com.zj365.health.fragment.CommunityRankFrag
import com.zj365.health.fragment.CommunityStepRankFrag
import com.zj365.health.fragment.WholeRankFrag
import com.zj365.health.fragment.WholeStepRankFrag

/**
 * 排行榜
 */
@Route(path = ARouterUrl.RANK_LIST)
class RankListAct : IBaseAct() {
    val binding by lazy { ActRankListBinding.inflate(layoutInflater) }

    var status = 0
    var type = 0//0:健康排行 1：步数排行
    var userId :String?= null
    val listFragment by lazy {
        listOf<Fragment>(WholeRankFrag(), CommunityRankFrag())
    }
    val tabName by lazy { arrayOf(R.string.rank_2, R.string.rank_1) }

    val stepListFragment = mutableListOf<Fragment>()


    override fun setLayout(): View = binding.root

    override fun initData() {
        super.initData()
        status = intent.extras!!.getInt("status")
        type = intent.extras!!.getInt("type")
        userId = intent.extras!!.getString("userId")
        var wholeStepRankFrag = WholeStepRankFrag()
        var communityStepRankFrag = CommunityStepRankFrag()
        bundle.clear()
        bundle.putString("userId",userId)
        wholeStepRankFrag.arguments = bundle
        communityStepRankFrag.arguments = bundle
        stepListFragment.add(wholeStepRankFrag)
        stepListFragment.add(communityStepRankFrag)
    }

    override fun initView() {
        super.initView()
        super.initView()
        statusBar(binding.title.mView)
        initTitle()
        initVp2()
        initTab()
    }


    private fun initTitle() {
        binding.title.mTvTitle.text =
            if (type == 0) getString(R.string.intera_2) else getString(R.string.intera_4)
        binding.title.mIvBack.apply {
            this.setOnClickListener {
                finish()
            }
        }

    }

    //设置tab
    private fun initTab() {
        tabName.forEach {
            var tab = binding.xTablayout.newTab()
            tab.text = resources.getString(it)
            binding.xTablayout.addTab(tab)
        }
        binding.xTablayout.setOnTabSelectedListener(object : XTabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: XTabLayout.Tab?) {
                LogUtils.d("nowPosition:${binding.mVp2.currentItem};;;position:${tab!!.position}")
                binding.mVp2.currentItem = tab!!.position
            }

            override fun onTabUnselected(tab: XTabLayout.Tab?) {
            }

            override fun onTabReselected(tab: XTabLayout.Tab?) {
            }
        })
    }

    //设置vp2
    private fun initVp2() {
        binding.mVp2.adapter = VpAdapter(if (type == 0) listFragment else stepListFragment, this)
        binding.mVp2.offscreenPageLimit = 2
        binding.mVp2.currentItem = 0
        binding.mVp2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                binding.xTablayout.getTabAt(position)!!.select()
            }
        })
    }

    fun changeSubTitle(str: String) {
        binding.xTablayout.getTabAt(1)!!.text = str

    }
}