package com.mints.goodmoney.ui.fragment

import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.androidkun.xtablayout.XTabLayout
import com.mints.goodmoney.R
import com.mints.goodmoney.manager.UserManager
import com.mints.goodmoney.mvp.presenters.MyPresenter
import com.mints.goodmoney.ui.activitys.LoginActivity
import com.mints.goodmoney.ui.adapter.MessageFragAdapter
import com.mints.goodmoney.ui.fragment.base.BaseFragment
import com.mints.goodmoney.ui.fragment.base.LazyLoadBaseFragment
import com.mints.goodmoney.ui.widgets.tablayout.TabLayout
import com.mints.goodmoney.utils.LogUtil
import kotlinx.android.synthetic.main.fragment_main_money.*
import java.util.*

/**
 * 描述：赚钱大厅
 * 作者：孟崔广
 * 时间：2020/10/29 13:48
 */
class MoneyFragment : BaseFragment(), XTabLayout.OnTabSelectedListener {

    private var mainFragment: MainFragment? = null

    private var curPosition = 0;//当前标签选中的下标

    override fun getContentViewLayoutID() = R.layout.fragment_main_money

    override fun initViewsAndEvents() {
        initView()
    }

    override fun onHiddenChanged(hidden: Boolean) {
        if (curPosition == 0) {
            mainFragment?.onHiddenChanged(hidden)
        }
        super.onHiddenChanged(hidden)
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (curPosition == 0) {
            mainFragment?.userVisibleHint = isVisibleToUser
        }
    }

    private fun initView() {
        val tabs: MutableList<String> = ArrayList()
        tabs.add("视频")
        tabs.add("游戏")
        tabs.add("书城")

        val fragments: MutableList<Fragment> = ArrayList()
        mainFragment = MainFragment()
        fragments.add(mainFragment!!)
        fragments.add(DhGameFragment())
        fragments.add(ZhangyueFragment())

        //去除两侧应阴影
        vpFragmentMoney.setOverScrollMode(ViewPager.OVER_SCROLL_NEVER)
        vpFragmentMoney.offscreenPageLimit = tabs.size + 3


        val adapter = MessageFragAdapter(childFragmentManager, fragments, tabs)
        vpFragmentMoney.setAdapter(adapter)
        xtFragmentMoney.setxTabDisplayNum(tabs.size)
        xtFragmentMoney.setupWithViewPager(vpFragmentMoney)
        xtFragmentMoney.addOnTabSelectedListener(this)
    }

    fun selectVedio() {
        vpFragmentMoney.currentItem = 0
    }

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

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

    override fun onTabSelected(tab: XTabLayout.Tab) {
        if (tab.position == 1 && !UserManager.getInstance().userIsLogin()) {
            readyGo(LoginActivity::class.java)
            vpFragmentMoney.currentItem = if (curPosition == 1) 0 else curPosition
            return
        }

//        if (tab.position == 1) {
//            vpFragmentMoney.setCanScroll(false)
//        } else {
//            vpFragmentMoney.setCanScroll(true)
//        }

        curPosition = tab.position
    }
}