package com.hongyegroup.cpt_ewallet.ui

import android.os.Bundle
import com.guadou.cs_cptservices.base.fragment.YYBaseVDBFragment
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.vertical
import com.guadou.lib_baselib.utils.CommUtils
import com.hongyegroup.cpt_ewallet.BR
import com.hongyegroup.cpt_ewallet.R
import com.hongyegroup.cpt_ewallet.databinding.FragmentEwalletMainBinding
import com.hongyegroup.cpt_ewallet.mvvm.EWalletMainViewModel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * 用于MainActivity的子Fragment
 * 钱包的主页
 */
class EWalletMainFragment : YYBaseVDBFragment<EWalletMainViewModel, FragmentEwalletMainBinding>(), IFragmentRefresh,
    OnRefreshListener {

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

    override fun startObserve() {
    }

    override fun init(savedInstanceState: Bundle?) {

        initRV()
        initData()
        initListener()
    }

    private fun initData() {
        showStateLoading()

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

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

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

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

    private fun initRV() {
        mBinding.rvHistory.vertical().adapter = mViewModel.mAdapter
    }

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

        mViewModel.mAdapter.setOnItemClickListener { adapter, view, position ->
            toast("点击进入发票详情")
        }
    }

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

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

    }

}