package com.adealink.weparty.playmate.comment

import android.os.Bundle
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.frame.router.annotation.RouterUri
import com.adealink.frame.util.DisplayUtil
import com.adealink.frame.util.naviBarHeight
import com.adealink.frame.util.onClick
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.diffutil.BaseListDiffUtil
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.commonui.widget.BottomDialogFragment
import com.adealink.weparty.module.playmate.Playmate
import com.adealink.weparty.playmate.R
import com.adealink.weparty.playmate.comment.viewmodel.CommentViewModel
import com.adealink.weparty.playmate.databinding.DialogCommentListBinding
import com.adealink.weparty.playmate.detail.adapter.PlaymateDetailCommentLabelViewBinder
import com.adealink.weparty.playmate.detail.adapter.PlaymateDetailCommentViewBinder
import com.adealink.weparty.playmate.detail.data.BasePlaymateDetailItem
import com.adealink.weparty.playmate.detail.data.PlaymateDetailCommentItemData
import com.adealink.weparty.playmate.viewmodel.PlaymateViewModelFactory
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.adealink.weparty.R as APP_R

@RouterUri(path = [Playmate.Comment.LIST_DIALOG], desc = "评价列表弹窗")
class CommentDialog : BottomDialogFragment(R.layout.dialog_comment_list) {

    var playmateId: String? = null

    private val binding by viewBinding(DialogCommentListBinding::bind)
    private val adapter by fastLazy { MultiTypeListAdapter<BasePlaymateDetailItem>(BaseListDiffUtil()) }

    private val commentViewModel by activityViewModels<CommentViewModel> { PlaymateViewModelFactory() }

    override val height: Int = (DisplayUtil.getScreenHeight() * 0.85f).toInt()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Router.bind(this)
    }

    override fun initViews() {
        super.initViews()
        binding.ivClose.onClick {
            dismiss()
        }

        binding.refreshLayout.setEnableRefresh(true)
        binding.refreshLayout.setEnableLoadMore(true)
        binding.refreshLayout.setEnableAutoLoadMore(true)
        binding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                refreshComment()
            }


            override fun onLoadMore(refreshLayout: RefreshLayout) {
                loadMoreComment()
            }
        })
        adapter.register(PlaymateDetailCommentLabelViewBinder())
        adapter.register(PlaymateDetailCommentViewBinder())
        binding.rvComment.adapter = adapter
        binding.rvComment.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
        binding.rvComment.setPadding(0, 0, 0, activity.naviBarHeight() + 24.dp())
    }

    override fun loadData() {
        super.loadData()
        commentViewModel.setPlaymate(playmateId)
        refreshComment()
    }

    private fun refreshComment() {
        commentViewModel.refreshList()
    }

    private fun loadMoreComment() {
        commentViewModel.loadMoreList()
    }

    override fun observeViewModel() {
        super.observeViewModel()
        commentViewModel.commentListRltLD.observe(viewLifecycleOwner) { rlt ->
            binding.refreshLayout.finishRefresh()
            binding.refreshLayout.finishLoadMore()
            binding.refreshLayout.setEnableLoadMore(commentViewModel.hasMoreData())
            showFailedToast(rlt)
        }
        commentViewModel.commentListLD.observe(viewLifecycleOwner) { list ->
            if (list.isNullOrEmpty()) {
                binding.rvComment.gone()
                binding.vErrorView.show(
                    errorEmptyResId = APP_R.drawable.common_list_empty_ic,
                    title = APP_R.string.commonui_list_empty
                )
            } else {
                adapter.submitList(
                    list.mapIndexed { index, data ->
                        PlaymateDetailCommentItemData(data, index < list.size - 1)
                    }
                )
                binding.rvComment.show()
                binding.vErrorView.gone()
            }
        }
    }

}