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.Rlt
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.FragmentFansBinding
import com.adealink.weparty.profile.relation.adapter.FansItemViewBinder
import com.adealink.weparty.profile.relation.data.FansItemData
import com.adealink.weparty.profile.relation.viewmodel.FollowViewModel
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 FansFragment : BaseFragment(R.layout.fragment_fans), FansItemViewBinder.OnFansItemClick {
    private val binding by viewBinding(FragmentFansBinding::bind)
    private val viewModel by activityViewModels<FollowViewModel> { ProfileViewModelFactory() }
    private val listAdapter by fastLazy { MultiTypeListAdapter<FansItemData>() }
    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(FansItemViewBinder(this))
        binding.rvFans.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
        binding.rvFans.adapter = listAdapter
        binding.rvFans.addItemDecoration(
            VerticalSpaceItemDecoration(
                20.dp(),
                firstSpaceHeight = 16.dp()
            )
        )
    }

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

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

    override fun follow(item: FansItemData, position: Int) {
        if (item.data.uid.isEmpty()) {
            return
        }
        viewModel.follow(item.data.uid).observe(viewLifecycleOwner) { rlt ->
            when (rlt) {
                is Rlt.Success -> {
                    listAdapter.notifyItemChanged(position)
                }

                is Rlt.Failed -> {
                    showFailedToast(rlt)
                }
            }
        }
    }

    override fun unFollow(item: FansItemData, position: Int) {
        if (item.data.uid.isEmpty()) {
            return
        }
        viewModel.unFollow(item.data.uid).observe(viewLifecycleOwner) { rlt ->
            when (rlt) {
                is Rlt.Success -> {
                    listAdapter.notifyItemChanged(position)
                }

                is Rlt.Failed -> {
                    showFailedToast(rlt)
                }
            }
        }
    }

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

}