package com.adealink.weparty.profile.relation

import androidx.fragment.app.activityViewModels
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.frame.router.Router
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.module.profile.Profile
import com.adealink.weparty.profile.R
import com.adealink.weparty.profile.databinding.FragmentVisitorBinding
import com.adealink.weparty.profile.relation.adapter.VisitorItemViewBinder
import com.adealink.weparty.profile.relation.data.VisitorItemData
import com.adealink.weparty.profile.relation.viewmodel.VisitorViewModel
import com.adealink.weparty.profile.viewmodel.ProfileViewModelFactory
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 VisitorFragment : BaseFragment(R.layout.fragment_visitor),
    VisitorItemViewBinder.OnVisitorItemClick {
    private val binding by viewBinding(FragmentVisitorBinding::bind)
    private val viewModel by activityViewModels<VisitorViewModel> { ProfileViewModelFactory() }
    private val listAdapter by fastLazy { MultiTypeListAdapter<VisitorItemData>() }
    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.pullVisitorList()
            }


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

        listAdapter.register(VisitorItemViewBinder(this))
        binding.rvVisitor.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
        binding.rvVisitor.adapter = listAdapter
        binding.rvVisitor.addItemDecoration(
            VerticalSpaceItemDecoration(
                0,
                firstSpaceHeight = 6.dp()
            )
        )
    }

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

    override fun observeViewModel() {
        super.observeViewModel()
        viewModel.visitorListLD.observe(viewLifecycleOwner) {
            if (it.isNullOrEmpty()) {
                binding.rvVisitor.gone()
                binding.vErrorView.show(
                    errorEmptyResId = APP_R.drawable.common_follow_list_empty_ic,
                    title = APP_R.string.commonui_list_empty
                )
            } else {
                listAdapter.submitList(it)
                binding.rvVisitor.show()
                binding.vErrorView.gone()
            }
        }
        viewModel.visitorListRltLD.observe(viewLifecycleOwner) { rlt ->
            binding.vRefresh.finishRefresh()
            binding.vRefresh.finishLoadMore()
            binding.vRefresh.setEnableLoadMore(viewModel.hasMoreData())
            showFailedToast(rlt)
        }
    }

    override fun goProfile(uid: String) {
        val act = activity ?: return
        Router.build(act, Profile.UserProfile.PATH)
            .putExtra(Profile.Common.EXTRA_UID, uid)
            .start()
    }

}