package com.adealink.weparty.playmate.detail

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.weparty.commonui.BaseFragment
import com.adealink.weparty.commonui.recycleview.adapter.MultiTypeListAdapter
import com.adealink.weparty.commonui.recycleview.itemdecoration.VerticalSpaceItemDecoration
import com.adealink.weparty.module.playmate.Playmate
import com.adealink.weparty.playmate.R
import com.adealink.weparty.playmate.comment.CommentDialog
import com.adealink.weparty.playmate.databinding.FragmentPlaymateDetailBinding
import com.adealink.weparty.playmate.detail.adapter.PlaymateDetailCommentLabelViewBinder
import com.adealink.weparty.playmate.detail.adapter.PlaymateDetailCommentTitleViewBinder
import com.adealink.weparty.playmate.detail.adapter.PlaymateDetailCommentViewBinder
import com.adealink.weparty.playmate.detail.adapter.PlaymateDetailExtraFieldViewBinder
import com.adealink.weparty.playmate.detail.adapter.PlaymateDetailViewBinder
import com.adealink.weparty.playmate.detail.data.BasePlaymateDetailItem
import com.adealink.weparty.playmate.detail.viewmodel.PlaymateDetailViewModel
import com.adealink.weparty.playmate.viewmodel.PlaymateViewModelFactory

class PlaymateDetailFragment : BaseFragment(R.layout.fragment_playmate_detail) {

    private val binding by viewBinding(FragmentPlaymateDetailBinding::bind)
    private val detailViewModel by activityViewModels<PlaymateDetailViewModel> { PlaymateViewModelFactory() }
    private val adapter by fastLazy { MultiTypeListAdapter<BasePlaymateDetailItem>() }

    private var bottomMargin: Int? = null

    fun setBottomMargin(margin: Int) {
        bottomMargin = margin
    }

    override fun initViews() {
        super.initViews()
        adapter.register(PlaymateDetailViewBinder(viewLifecycleOwner))
        adapter.register(PlaymateDetailExtraFieldViewBinder())
        adapter.register(PlaymateDetailCommentTitleViewBinder({
            goMoreComment()
        }))
        adapter.register(PlaymateDetailCommentLabelViewBinder())
        adapter.register(PlaymateDetailCommentViewBinder())
        binding.rvDetail.adapter = adapter
        binding.rvDetail.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
        binding.rvDetail.addItemDecoration(
            VerticalSpaceItemDecoration(0, lastSpaceHeight = bottomMargin ?: 0)
        )
    }

    override fun observeViewModel() {
        super.observeViewModel()
        detailViewModel.detailItemListLD.observe(viewLifecycleOwner) {
            adapter.submitList(it)
        }
    }

    private fun goMoreComment() {
        val id = detailViewModel.detailLD.value?.playmateId
        if (id.isNullOrEmpty()) {
            return
        }
        CommentDialog().apply {
            playmateId = id
        }.show(childFragmentManager, Playmate.Comment.LIST_DIALOG)
    }

}