package com.hongyegroup.cpt_rewards.ui

import android.content.Intent
import android.os.Bundle
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.guadou.cs_cptservices.adapter.ViewPagerFragmentAdapter
import com.guadou.lib_baselib.base.activity.BaseVDBActivity
import com.guadou.lib_baselib.base.vm.BaseViewModel
import com.guadou.lib_baselib.bean.DataBindingConfig
import com.guadou.lib_baselib.utils.CommUtils
import com.hongyegroup.cpt_rewards.BR
import com.hongyegroup.cpt_rewards.R
import com.hongyegroup.cpt_rewards.databinding.ActivityMyRewardsBinding
import com.hongyegroup.cpt_rewards.ui.fragment.MyRewardsActiveFragment
import com.hongyegroup.cpt_rewards.ui.fragment.MyRewardsExpiredFragment
import com.hongyegroup.cpt_rewards.ui.fragment.MyRewardsUsedFragment

class MyRewardsActivity : BaseVDBActivity<BaseViewModel, ActivityMyRewardsBinding>() {

    val mClickProxy by lazy { ClickProxy() }

    var mTableList = ArrayList<TextView>()
    var mFragments = ArrayList<Fragment>()
    val mViewPagerAdapter by lazy { ViewPagerFragmentAdapter(supportFragmentManager, mFragments) }

    companion object {

        fun startInstance() {
            val context = CommUtils.getContext()
            val intent = Intent(context, MyRewardsActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(intent)
        }

    }

    override fun init(savedInstanceState: Bundle?) {

        initView()
        initListener()

    }

    override fun getDataBindingConfig(): DataBindingConfig {

        return DataBindingConfig(R.layout.activity_my_rewards)
            .addBindingParams(BR.click, mClickProxy)

    }

    private fun initListener() {

        //ViewPager滑动监听
        mBinding.viewPagerMyRewards.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {

                setIndicator(position)

            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        })

    }

    private fun initView() {

        mTableList.add(mBinding.tvTabActive)
        mTableList.add(mBinding.tvTabExpired)
        mTableList.add(mBinding.tvTabUsed)

        mFragments.add(MyRewardsActiveFragment())
        mFragments.add(MyRewardsExpiredFragment())
        mFragments.add(MyRewardsUsedFragment())

        mBinding.viewPagerMyRewards.adapter = mViewPagerAdapter
        mBinding.viewPagerMyRewards.offscreenPageLimit = 2

    }

    //设置View Pager Table
    private fun setIndicator(position: Int) {

        for (index in mTableList.indices) {

            val tableTv = mTableList[index]

            if (index == position) {

                tableTv.setTextColor(CommUtils.getColor(R.color.white))
                tableTv.setBackgroundResource(R.drawable.shape_round_blue)

            } else {

                tableTv.setTextColor(CommUtils.getColor(R.color.black))
                tableTv.setBackgroundResource(R.drawable.shape_round_white)

            }
        }
    }

    override fun startObserve() {

    }

    /**
     * DataBinding事件处理
     */
    inner class ClickProxy {

        //切换ViewPager
        fun changeViewPager(position: Int){

            mBinding.viewPagerMyRewards.setCurrentItem(position)

        }

    }

}