package com.adealink.weparty.wallet.detail.diamond

import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.weparty.commonui.BaseFragment
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.recycleview.adapter.MultiTypeListAdapter
import com.adealink.weparty.commonui.recycleview.itemdecoration.VerticalSpaceItemDecoration
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.wallet.R
import com.adealink.weparty.wallet.databinding.FragmentDetailDiamondBinding
import com.adealink.weparty.wallet.detail.data.DiamondDetailItemData
import com.adealink.weparty.wallet.viewmodel.WalletViewModelFactory
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.adealink.weparty.R as APP_R

class DiamondFragment: BaseFragment(R.layout.fragment_detail_diamond) {

    private val binding by viewBinding(FragmentDetailDiamondBinding::bind)
    private val viewModel by viewModels<DiamondDetailViewModel> { WalletViewModelFactory() }
    private val listAdapter by fastLazy { MultiTypeListAdapter<DiamondDetailItemData>() }
    override fun initViews() {
        super.initViews()
        binding.vRefresh.setEnableRefresh(true)
        binding.vRefresh.setEnableLoadMore(true)
        binding.vRefresh.setEnableAutoLoadMore(true)
        binding.vRefresh.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                viewModel.pullFollowList()
            }


            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.loadMoreFollowList()
            }
        })

        listAdapter.register(DiamondDetailItemViewBinder())
        binding.rvList.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
        binding.rvList.adapter = listAdapter
        binding.rvList.addItemDecoration(
            VerticalSpaceItemDecoration(
                1.dp(),
                firstSpaceHeight = 8.dp()
            )
        )
    }

    override fun loadData() {
        super.loadData()
        viewModel.pullFollowList()
    }

    override fun observeViewModel() {
        super.observeViewModel()
        viewModel.detailListLD.observe(viewLifecycleOwner) {
            listAdapter.submitList(it)
            if (it.isNullOrEmpty()) {
                binding.rvList.gone()
                binding.vErrorView.show(
                    errorEmptyResId = APP_R.drawable.common_list_empty_ic,
                    title = APP_R.string.commonui_list_empty
                )
            } else {
                binding.rvList.show()
                binding.vErrorView.gone()
            }
        }
        viewModel.detailListRltLD.observe(viewLifecycleOwner) { rlt ->
            binding.vRefresh.finishRefresh()
            binding.vRefresh.finishLoadMore()
            showFailedToast(rlt)
        }
    }

}