package com.zj365.mime.act

import android.os.Bundle
import android.util.Log
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Route
import com.androidkun.xtablayout.XTabLayout
import com.xty.base.act.BaseVmAct
import com.xty.base.vp2.VpAdapter
import com.xty.common.arouter.ARouterUrl
import com.zj365.mime.R
import com.zj365.mime.databinding.ActMyProgrammeBinding
import com.zj365.mime.fragment.ProgrammeHistoryFrag
import com.zj365.mime.fragment.ProgrammeSportFrag
import com.zj365.mime.vm.MyProgVm


/**
 * 我的方案
 */
@Route(path = ARouterUrl.MY_PROG)
class MyProgAct : BaseVmAct<MyProgVm>() {

    val listFrag = listOf(ProgrammeHistoryFrag(), ProgrammeSportFrag())
    val binding by lazy { ActMyProgrammeBinding.inflate(layoutInflater) }

    var userId: String? = null

    override fun setLayout() = binding.root

    override fun initData() {
        super.initData()
        isDarkBar = true
        userId = intent.getStringExtra("userId")
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = getString(R.string.progress)
        binding.title.mIvBack.setOnClickListener { finish() }
        val titleArray = resources.getStringArray(R.array.programme_title)
        titleArray.forEach {
            val tab = binding.xTablayout.newTab()
            tab.text = it
            binding.xTablayout.addTab(tab)
        }
        listFrag.forEach {
            val bundle = Bundle()
            bundle.putString("userId", userId)
            it.arguments = bundle
        }
        binding.xTablayout.setOnTabSelectedListener(object : XTabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: XTabLayout.Tab?) {
                binding.mVp2.setCurrentItem(tab!!.position, false)
                Log.e("TAG", "tab: " + tab.position)
            }

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

            override fun onTabReselected(tab: XTabLayout.Tab?) {
            }
        })
        binding.mVp2.adapter = VpAdapter(listFrag, 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()
            }
        })
    }

    override fun liveObserver() {

    }

}