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.FragmentFollowBinding
import com.adealink.weparty.profile.relation.adapter.FollowItemViewBinder
import com.adealink.weparty.profile.relation.data.FollowItemData
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 FollowFragment : BaseFragment(R.layout.fragment_follow),
    FollowItemViewBinder.OnFollowItemClick {

    private val binding by viewBinding(FragmentFollowBinding::bind)
    private val viewModel by activityViewModels<FollowViewModel> { ProfileViewModelFactory() }
    private val listAdapter by fastLazy { MultiTypeListAdapter<FollowItemData>() }

    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(FollowItemViewBinder(this))
        binding.rvFollow.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
        binding.rvFollow.adapter = listAdapter
        binding.rvFollow.addItemDecoration(
            VerticalSpaceItemDecoration(
                20.dp(),
                firstSpaceHeight = 16.dp()
            )
        )
    }

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

    override fun observeViewModel() {
        super.observeViewModel()
        viewModel.followListLD.observe(viewLifecycleOwner) {
            if (it.isNullOrEmpty()) {
                binding.rvFollow.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.rvFollow.show()
                binding.vErrorView.gone()
            }
        }
        viewModel.followListRltLD.observe(viewLifecycleOwner) { rlt ->
            binding.vRefresh.finishRefresh()
            binding.vRefresh.finishLoadMore()
            binding.vRefresh.setEnableLoadMore(viewModel.hasMoreFollow())
            showFailedToast(rlt)
        }
    }

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

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

    override fun unFollow(item: FollowItemData, position: Int) {
        if (item.data.uid.isEmpty()) {
            return
        }
        viewModel.unFollow(item.data.uid, true).observe(viewLifecycleOwner) { rlt ->
            when (rlt) {
                is Rlt.Success -> {
                    item.data.followStatus = rlt.data.status
                    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()
    }

}