package com.hongyegroup.cpt_ewallet.ui

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.asynclayoutinflater.view.AsyncLayoutInflater
import com.guadou.cs_cptservices.base.activity.YYBaseVDBLoadingActivity
import com.guadou.lib_baselib.bean.DataBindingConfig
import com.guadou.lib_baselib.ext.commContext
import com.guadou.lib_baselib.ext.gotoActivity
import com.guadou.lib_baselib.ext.vertical
import com.guadou.lib_baselib.view.gloading.GLoadingTitleStatus
import com.guadou.lib_baselib.view.gloading.Gloading
import com.guadou.lib_baselib.view.gloading.GloadingGlobalAdapter
import com.hongyegroup.cpt_ewallet.R
import com.hongyegroup.cpt_ewallet.databinding.ActivityWithdrawAppointmentBinding
import com.hongyegroup.cpt_ewallet.mvvm.WithdrawAppointmentViewModel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * 提现的预约列表
 */
class WithdrawAppointmentActivity : YYBaseVDBLoadingActivity<WithdrawAppointmentViewModel, ActivityWithdrawAppointmentBinding>(), OnRefreshListener {

    companion object {
        fun startInstance() {
            commContext().gotoActivity<WithdrawAppointmentActivity>()
        }
    }

    override fun generateGLoading(): Gloading.Holder {
        return Gloading.from(GloadingGlobalAdapter())
            .wrap(this, GLoadingTitleStatus(true, true, false))
            .withRetry { onGoadingRetry() }
    }

    override fun onGoadingRetry() {
        mViewModel.isNeedPlaceHolder = true
        initData()
    }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.activity_withdraw_appointment)
    }

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

        initRV()
        initData()
        initListener()
    }

    private fun initData() {
        mViewModel.getAppointmentList().observe(this) {
            mBinding.refreshLayout.finishRefresh()
        }
    }

    @SuppressLint("InflateParams")
    private fun initRV() {
        mBinding.recyclerView.vertical().adapter = mViewModel.mAdapter

        AsyncLayoutInflater(mActivity).inflate(R.layout.item_withdraw_appointment_footer, null) { view, _, _ ->
            mViewModel.mAdapter.addFooterView(view)
        }
    }


    private fun initListener() {
        mViewModel.mAdapter.loadMoreModule.isEnableLoadMore = false

        //刷新控件初始化
        mBinding.refreshLayout.setEnableNestedScroll(true)
        mBinding.refreshLayout.setEnableLoadMore(false)
        mBinding.refreshLayout.setEnableRefresh(true)
        mBinding.refreshLayout.setOnRefreshListener(this)

        //点击事件
        mViewModel.mAdapter.addChildClickViewIds(R.id.btn_query_status)
        mViewModel.mAdapter.setOnItemChildClickListener { _, view, position ->
            if (view.id == R.id.btn_query_status) {
                //去反馈页面
                AppointmentEnquiryActivity.startInstance()
            }
        }

    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        initData()
    }

    override fun startObserve() {

    }

}