package com.hongyegroup.cpt_main.ui

import android.os.Bundle
import android.view.View
import com.guadou.cs_cptservices.adapter.BannerRectangleCircleIndicator
import com.guadou.cs_cptservices.base.fragment.YYBaseVDBLoadingFragment
import com.guadou.cs_cptservices.interfaces.IFragmentRefresh
import com.guadou.lib_baselib.bean.DataBindingConfig
import com.guadou.lib_baselib.engine.toast
import com.guadou.lib_baselib.ext.horizontal
import com.guadou.lib_baselib.ext.vertical
import com.guadou.lib_baselib.utils.CommUtils
import com.guadou.lib_baselib.view.gloading.GLoadingTitleStatus
import com.guadou.lib_baselib.view.gloading.Gloading
import com.guadou.lib_baselib.view.gloading.GloadingPlaceHolderlAdapter
import com.hongyegroup.cpt_main.BR
import com.hongyegroup.cpt_main.R
import com.hongyegroup.cpt_main.databinding.FragmentHomeBinding
import com.hongyegroup.cpt_main.mvvm.HomeViewModel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * 首页列表
 */
class HomeFragment : YYBaseVDBLoadingFragment<HomeViewModel, FragmentHomeBinding>(), IFragmentRefresh,
    OnRefreshListener {

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

    //重新生成GLoading对象-跳动动画
    override fun generateGLoading(view: View): Gloading.Holder {
        return Gloading.from(GloadingPlaceHolderlAdapter(R.layout.layout_placeholder_home))
            .wrap(view, GLoadingTitleStatus(true, true, true))
            .withRetry { onGoadingRetry() }
    }

    override fun startObserve() {
    }

    override fun init(savedInstanceState: Bundle?) {

        initRV()
        initData()
        initListener()
    }

    private fun initData() {
        showStateLoading()

        //获取首页的数据
        //requestHomeData()

        CommUtils.getHandler().postDelayed({
            mBinding.refreshLayout.finishRefresh(250)
            showStateSuccess()
            popopData()
        }, 1500)
    }

    override fun onGoadingRetry() {
        initData()
    }

    private fun initRV() {
        mBinding.rvHotJob.horizontal().adapter = mViewModel.mHotJobAdapter

        mBinding.rvCityJob.horizontal().adapter = mViewModel.mCityJobAdapter

        mBinding.rvAreaJob.horizontal().adapter = mViewModel.mAreaJobAdapter

        mBinding.rvJobTips.vertical(2).adapter = mViewModel.mJobTipsAdapter

        mBinding.banner.addBannerLifecycleObserver(this)
        mBinding.banner.indicator = BannerRectangleCircleIndicator(mActivity)
        mBinding.banner.adapter = mViewModel.mBannerAdapter
    }


    private fun initListener() {
        mBinding.refreshLayout.setEnableNestedScroll(true)
        mBinding.refreshLayout.setEnableLoadMore(false)
        mBinding.refreshLayout.setEnableRefresh(true)
        mBinding.refreshLayout.setOnRefreshListener(this)


        mViewModel.mHotJobAdapter.setOnItemClickListener { adapter, view, position ->
            toast("去兼职工作详情页面")
        }

        mViewModel.mCityJobAdapter.setOnItemClickListener { adapter, view, position ->
            toast("去兼职工作详情页面")
        }

        mViewModel.mAreaJobAdapter.setOnItemClickListener { adapter, view, position ->
            toast("去兼职工作详情页面")
        }

        mViewModel.mJobTipsAdapter.setOnItemClickListener { adapter, view, position ->
            toast("去公告板详情页面")
        }

        mBinding.banner.setOnBannerListener { _, position ->
            toast("点击Banner根据类型跳转:$position")
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        CommUtils.getHandler().postDelayed({
            mBinding.refreshLayout.finishRefresh()
            popopData()
        }, 1500)
    }

    /**
     * 模拟数据
     */
    private fun popopData() {
        mViewModel.setupDataes()
    }

    override fun scrollTopRefresh() {
        mBinding.scrollView.smoothScrollTo(0, 0)
        mBinding.refreshLayout.autoRefresh()
    }

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

        fun gotoWalletPage() {
            toast("去钱包页面")
        }

        fun gotoRewardsPage() {
            toast("去奖励页面")
        }

        fun gotoParttimePage() {
            toast("去兼职页面")
        }

        fun gotoBulletinBoardPage() {
            toast("去公告板页面")
        }
    }


}