package com.yidian.shenghuoquan.bgoods.ui.coupon.fragment

import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import com.yidian.bcommon.mvvm.BaseMvvmFragment
import com.yidian.shenghuoquan.bgoods.R
import com.yidian.shenghuoquan.bgoods.databinding.CommodityFragmentCommodityListBinding
import com.yidian.shenghuoquan.bgoods.dto.CommodityStatus
import com.yidian.shenghuoquan.bgoods.ui.coupon.adapter.CommodityListAdapter
import com.yidian.shenghuoquan.bgoods.ui.coupon.viewmodel.CommodityListViewModel

/**
 * author: yinjiacheng
 * date: 7/2/21 10:44 AM
 * description: 商品列表
 */
class CommodityListFragment :
  BaseMvvmFragment<CommodityFragmentCommodityListBinding, CommodityListViewModel>(R.layout.commodity_fragment_commodity_list),
  OnRefreshListener, OnLoadMoreListener {

  companion object {

    // 商品状态
    // e.g. 上架中、审核中、被驳回、已下架
    private const val ARGUMENT_COMMODITY_STATUS = "commodity_status"

    fun newInstance(status: CommodityStatus): CommodityListFragment {
      return CommodityListFragment().apply {
        arguments = bundleOf(Pair(ARGUMENT_COMMODITY_STATUS, status))
      }
    }
  }

  private val status by lazy { arguments?.get(ARGUMENT_COMMODITY_STATUS) as CommodityStatus }
  private val adapter by lazy { activity?.let { CommodityListAdapter(it, status) } }

  override val vm: CommodityListViewModel by viewModels()

  override fun initView() {
    initRecyclerView()
    initRefresh()
    subscribeLiveData()
    // 首次加载
    refreshCommodityList()
  }

  private fun initRecyclerView() {
    binding.rvCommodityList.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
    binding.rvCommodityList.adapter = adapter
    binding.rvCommodityList.addItemDecoration(CommodityListAdapter.CommodityItemDecoration())
  }

  private fun initRefresh() {
    binding.rlCommodityList.setOnRefreshListener(this)
    binding.rlCommodityList.setOnLoadMoreListener(this)
  }

  /**
   * 订阅数据
   */
  private fun subscribeLiveData() {
    // 下拉刷新完成
    vm.refreshFinish.observe(this, {
      binding.rlCommodityList.finishRefresh()
      binding.pvEmptyPage.isVisible = it.isEmpty()
      adapter?.updateData(it)
    })
    // 上拉加载更多完成
    vm.loadMoreFinish.observe(this, {
      binding.rlCommodityList.finishLoadMore()
      adapter?.insertData(it)
    })
    // 商品数据变化
    adapter?.commodityDataChange?.observe(this, {
      if (it.isNullOrEmpty()) {
        binding.pvEmptyPage.isVisible = true
      }
    })
  }

  /**
   * 刷新商品列表
   */
  fun refreshCommodityList() {
    binding.rlCommodityList.autoRefresh()
  }

  override fun onRefresh(refreshLayout: RefreshLayout) {
    // 触发下拉刷新
    vm.requestCommodityManagementList(status, null, CommodityListViewModel.LoadType.REFRESH)
  }

  override fun onLoadMore(refreshLayout: RefreshLayout) {
    // 触发上拉加载更多
    vm.requestCommodityManagementList(status, null, CommodityListViewModel.LoadType.LOAD_MORE)
  }

}
