package com.hongyegroup.cpt_newsfeed.ui

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.viewpager.widget.ViewPager
import com.guadou.cs_cptservices.base.fragment.YYBaseVDBFragment
import com.guadou.cs_cptservices.interfaces.IFragmentRefresh
import com.guadou.lib_baselib.base.Ghost
import com.guadou.lib_baselib.bean.DataBindingConfig
import com.guadou.lib_baselib.engine.toast
import com.guadou.lib_baselib.ext.bindFragment
import com.guadou.lib_baselib.utils.CommUtils
import com.hongyegroup.cpt_newsfeed.BR
import com.hongyegroup.cpt_newsfeed.R
import com.hongyegroup.cpt_newsfeed.databinding.FragmentNewsfeedMainBinding
import com.hongyegroup.cpt_newsfeed.mvvm.NewsFeedMainViewModel

/**
 * 首页动态广场的页面
 * Tab + ViewPager
 */
class NewsFeedMainFragment : YYBaseVDBFragment<NewsFeedMainViewModel, FragmentNewsfeedMainBinding>(), IFragmentRefresh {

    private var mCurPage = 1
    private lateinit var mNewsFeedMonentsFragment: NewsFeedListFragment
    private lateinit var mNewsFeedFollowingFragment: NewsFeedListFragment
    private lateinit var mNewsFeedProfileFragment: NewsFeedProfileFragment

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.fragment_newsfeed_main, BR.viewModel, mViewModel)
            .addBindingParams(BR.click, ClickProxy())
    }

    override fun startObserve() {
    }

    override fun init(savedInstanceState: Bundle?) {
        setStatusBarBlackText()

        initAdapter()
    }

    private fun initAdapter() {
        mNewsFeedMonentsFragment = NewsFeedListFragment(0)
        mNewsFeedFollowingFragment = NewsFeedListFragment(1)
        mNewsFeedProfileFragment = NewsFeedProfileFragment("8558", true)

        mViewModel.mFragments.add(mNewsFeedMonentsFragment)
        mViewModel.mFragments.add(mNewsFeedFollowingFragment)
        mViewModel.mFragments.add(mNewsFeedProfileFragment)

        mBinding.viewPager.bindFragment(childFragmentManager, mViewModel.mFragments)

        mBinding.viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
            override fun onPageScrollStateChanged(state: Int) {}
            override fun onPageSelected(position: Int) {
                mCurPage = position
                setIndicator(position)
            }
        })

        mBinding.viewPager.currentItem = 1
    }

    //点击顶部选择切换ViewPager的效果
    private fun setCurrentPage(index: Int) {
        mBinding.viewPager.currentItem = index
        setIndicator(index)
    }

    //滑动ViewPager切换顶部选择的效果
    private fun setIndicator(position: Int) {
        mCurPage = position
        when (position) {
            0 -> {
                mBinding.textMoments.setTextColor(CommUtils.getColor(R.color.app_blue))
                mBinding.viewFollowLine.visibility = View.VISIBLE
                mBinding.textFollowing.setTextColor(CommUtils.getColor(R.color.gray_88))
                mBinding.viewForYouLine.visibility = View.GONE
                mBinding.textPulished.setTextColor(CommUtils.getColor(R.color.gray_88))
                mBinding.viewPublished.visibility = View.GONE
            }
            1 -> {
                mBinding.textMoments.setTextColor(CommUtils.getColor(R.color.gray_88))
                mBinding.viewFollowLine.visibility = View.GONE
                mBinding.textFollowing.setTextColor(CommUtils.getColor(R.color.app_blue))
                mBinding.viewForYouLine.visibility = View.VISIBLE
                mBinding.textPulished.setTextColor(CommUtils.getColor(R.color.gray_88))
                mBinding.viewPublished.visibility = View.GONE
            }
            else -> {
                mBinding.textMoments.setTextColor(CommUtils.getColor(R.color.gray_88))
                mBinding.viewFollowLine.visibility = View.GONE
                mBinding.textFollowing.setTextColor(CommUtils.getColor(R.color.gray_88))
                mBinding.viewForYouLine.visibility = View.GONE
                mBinding.textPulished.setTextColor(CommUtils.getColor(R.color.app_blue))
                mBinding.viewPublished.visibility = View.VISIBLE
            }
        }
    }

    override fun scrollTopRefresh() {
        when (mCurPage) {
            0 -> mNewsFeedMonentsFragment.scrollTopRefresh()
            1 -> mNewsFeedFollowingFragment.scrollTopRefresh()
            else -> mNewsFeedProfileFragment.scrollTopRefresh()
        }
    }

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

        //切换到朋友圈
        fun switchMoments() {
            if (mCurPage == 0) {
                mNewsFeedMonentsFragment.scrollTopRefresh()
            } else {
                setCurrentPage(0)
            }
        }

        //切换已关注的
        fun switchFollowing() {
            if (mCurPage == 1) {
                mNewsFeedFollowingFragment.scrollTopRefresh()
            } else {
                setCurrentPage(1)
            }
        }

        //切换我的发布
        fun switchPulished() {
            if (mCurPage == 2) {
                mNewsFeedProfileFragment.scrollTopRefresh()
            } else {
                setCurrentPage(2)
            }
        }

        //去发布页面
        fun gotoPostPage() {
            Ghost.launchActivityForResult(mActivity, Intent(mActivity, NewsFeedPostActivity::class.java)) {
                val name = it?.getStringExtra("name")
                toast("获取回调 name:" + name)
            }
        }

    }

}