package com.adealink.weparty.profile.dynamic.fragment.dialog

import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.DisplayUtil
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.recycleview.itemdecoration.VerticalSpaceItemDecoration
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.commonui.widget.BottomDialogFragment
import com.adealink.weparty.profile.R
import com.adealink.weparty.profile.databinding.DialogDynamicCommentBinding
import com.adealink.weparty.profile.dynamic.adapter.DynamicCommentViewBinder
import com.adealink.weparty.profile.dynamic.data.publishNewCommentLD
import com.adealink.weparty.profile.dynamic.fragment.data.BaseDynamicDetailItem
import com.adealink.weparty.profile.dynamic.viewmodel.DynamicCommentViewModel
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 DynamicCommentDialog : BottomDialogFragment(R.layout.dialog_dynamic_comment) {

    private val binding by viewBinding(DialogDynamicCommentBinding::bind)
    private val commentViewModel by activityViewModels<DynamicCommentViewModel>()

    private val commentAdapter by fastLazy {
        MultiTypeListAdapter<BaseDynamicDetailItem>(
            BaseListDiffUtil()
        )
    }


    override val height: Int = (DisplayUtil.getScreenHeight() * 0.75).toInt()
    private var dynamicId: String? = null

    fun setDynamicId(id: String) {
        dynamicId = id
    }

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

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


            override fun onLoadMore(refreshLayout: RefreshLayout) {
                commentViewModel.loadMore()
            }
        })

        commentAdapter.register(DynamicCommentViewBinder())
        binding.rvComment.adapter = commentAdapter
        binding.rvComment.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
        binding.rvComment.addItemDecoration(
            VerticalSpaceItemDecoration(22.dp(), 0, 0)
        )

        binding.clInput.onClick {
            clickTalk()
        }
    }

    override fun loadData() {
        super.loadData()
        val dynamicId = dynamicId
        if (dynamicId.isNullOrEmpty()) {
            return
        }
        commentViewModel.loadDynamicDetail(dynamicId)
    }

    override fun observeViewModel() {
        super.observeViewModel()
        commentViewModel.totalCommentLD.observe(viewLifecycleOwner) {
            binding.tvTotalComment.text =
                getCompatString(R.string.profile_dynamic_total_comment, it.toString())
        }
        commentViewModel.commentListLD.observe(viewLifecycleOwner) {
            if (it.isNullOrEmpty()) {
                binding.rvComment.gone()
                binding.vErrorView.show(
                    errorEmptyResId = APP_R.drawable.common_order_list_empty_ic,
                    title = APP_R.string.commonui_list_empty
                )
            } else {
                commentAdapter.submitList(it)
                binding.rvComment.show()
                binding.vErrorView.gone()
            }
        }
        commentViewModel.commentListRltLD.observe(viewLifecycleOwner) { rlt ->
            binding.vRefresh.finishRefresh()
            binding.vRefresh.finishLoadMore()
            binding.vRefresh.setEnableLoadMore(commentViewModel.hasMoreData())
            showFailedToast(rlt)
        }
        publishNewCommentLD.observeWithoutCache(viewLifecycleOwner) {
            if (this@DynamicCommentDialog.dynamicId == it.dynamicId) {
                commentViewModel.onNewCommentAdd(it.comment)
            }
        }
    }

    private fun clickTalk() {
        val id = dynamicId ?: return
        DynamicTalkDialog().apply {
            setDynamicId(id)
        }.show(childFragmentManager)
    }
}