package kr.co.vividnext.sodalive.live.room.donation

import android.annotation.SuppressLint
import android.graphics.Rect
import android.view.LayoutInflater
import android.view.View
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomsheet.BottomSheetDialog
import kr.co.vividnext.sodalive.databinding.DialogLiveRoomDonationRankingBinding
import kr.co.vividnext.sodalive.extensions.dpToPx
import kr.co.vividnext.sodalive.extensions.moneyFormat
import kr.co.vividnext.sodalive.live.room.LiveRoomViewModel

@SuppressLint("NotifyDataSetChanged")
class LiveRoomDonationRankingDialog(
    private val activity: FragmentActivity,
    layoutInflater: LayoutInflater,
    viewModel: LiveRoomViewModel,
    roomId: Long
) {
    private val bottomSheetDialog: BottomSheetDialog = BottomSheetDialog(activity)
    private val dialogView = DialogLiveRoomDonationRankingBinding.inflate(layoutInflater)
    private val adapter = LiveRoomDonationRankingAdapter()

    init {
        bottomSheetDialog.setContentView(dialogView.root)
        bottomSheetDialog.setCancelable(false)

        dialogView.ivClose.setOnClickListener { bottomSheetDialog.dismiss() }
        setupRecyclerView()

        viewModel.donationStatus(roomId) {
            adapter.items.clear()
            adapter.items.addAll(it.donationList)
            adapter.notifyDataSetChanged()
            dialogView.tvTotalCan.text = it.totalCan.moneyFormat()
            dialogView.tvTotalCount.text = it.totalCount.moneyFormat()
            dialogView.tvTotalSecretCan.text = it.totalSecretCan.moneyFormat()

            if (it.totalSecretCan > 0) {
                dialogView.llTotalCanSubTitle.visibility = View.VISIBLE
                dialogView.tvTotalSecretCan.visibility = View.VISIBLE
                dialogView.tvTotalCanSlash.visibility = View.VISIBLE
            } else {
                dialogView.llTotalCanSubTitle.visibility = View.GONE
                dialogView.tvTotalSecretCan.visibility = View.GONE
                dialogView.tvTotalCanSlash.visibility = View.GONE
            }
        }
    }

    private fun setupRecyclerView() {
        dialogView.rvDonationRanking.layoutManager = LinearLayoutManager(
            activity,
            LinearLayoutManager.VERTICAL,
            false
        )

        dialogView.rvDonationRanking.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(
                outRect: Rect,
                view: View,
                parent: RecyclerView,
                state: RecyclerView.State
            ) {
                super.getItemOffsets(outRect, view, parent, state)

                outRect.left = 13.3f.dpToPx().toInt()
                outRect.right = 13.3f.dpToPx().toInt()

                when (parent.getChildAdapterPosition(view)) {
                    0, 1, 2 -> {
                        outRect.top = 0
                        outRect.bottom = 0
                    }

                    3 -> {
                        outRect.top = 20.dpToPx().toInt()
                        outRect.bottom = 6.7f.dpToPx().toInt()
                    }

                    else -> {
                        outRect.top = 6.7f.dpToPx().toInt()
                        outRect.bottom = 6.7f.dpToPx().toInt()
                    }
                }
            }
        })

        dialogView.rvDonationRanking.adapter = adapter
    }

    fun show() {
        bottomSheetDialog.show()
    }
}
