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

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.Window
import android.view.WindowManager
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.LiveData
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kr.co.vividnext.sodalive.databinding.DialogLiveRoomDonationMessageBinding
import kr.co.vividnext.sodalive.extensions.dpToPx
import kr.co.vividnext.sodalive.extensions.moneyFormat

class LiveRoomDonationMessageDialog(
    layoutInflater: LayoutInflater,
    creatorId: Long,
    private val activity: FragmentActivity,
    private val donationMessageListLiveData: LiveData<List<LiveRoomDonationMessage>>,
    private val donationMessageCountLiveData: LiveData<Int>,
    private val getDonationMessageList: () -> Unit,
    private val deleteDonationMessage: (String) -> Unit,
    copyMessage: (String) -> Unit
) {
    private val alertDialog: AlertDialog
    private val dialogView = DialogLiveRoomDonationMessageBinding.inflate(layoutInflater)
    private val adapter = LiveRoomDonationMessageAdapter(
        creatorId = creatorId,
        onClickDeleteMessage = { deleteDonationMessage(it) },
        copyMessage = copyMessage
    )

    init {
        val dialogBuilder = AlertDialog.Builder(activity)
        dialogBuilder.setView(dialogView.root)

        alertDialog = dialogBuilder.create()
        alertDialog.setCancelable(false)
        alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        alertDialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

        alertDialog.setOnShowListener {
            val lp = WindowManager.LayoutParams()
            lp.copyFrom(alertDialog.window?.attributes)
            lp.width = activity.resources.displayMetrics.widthPixels - (26.7f.dpToPx()).toInt()
            lp.height = activity.resources.displayMetrics.heightPixels - (200f.dpToPx()).toInt()

            alertDialog.window?.attributes = lp
        }

        dialogView.tvClose.setOnClickListener { alertDialog.dismiss() }
        setupRecyclerView()
        bindData()
    }

    private fun setupRecyclerView() {
        dialogView.rvDonationMessage.isNestedScrollingEnabled = true
        dialogView.rvDonationMessage.layoutManager = LinearLayoutManager(activity)
        dialogView.rvDonationMessage.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(
                outRect: Rect,
                view: View,
                parent: RecyclerView,
                state: RecyclerView.State
            ) {
                super.getItemOffsets(outRect, view, parent, state)

                when (parent.getChildAdapterPosition(view)) {
                    0 -> {
                        outRect.top = 0.dpToPx().toInt()
                        outRect.bottom = 4.dpToPx().toInt()
                    }

                    adapter.itemCount - 1 -> {
                        outRect.top = 4.dpToPx().toInt()
                        outRect.bottom = 0.dpToPx().toInt()
                    }

                    else -> {
                        outRect.top = 4.dpToPx().toInt()
                        outRect.bottom = 4.dpToPx().toInt()
                    }
                }
            }
        })
        dialogView.rvDonationMessage.adapter = adapter
    }

    @SuppressLint("SetTextI18n")
    private fun bindData() {
        donationMessageListLiveData.observe(activity) {
            if (it.isEmpty()) {
                dialogView.rvDonationMessage.visibility = View.GONE
                dialogView.tvNone.visibility = View.VISIBLE
            } else {
                dialogView.rvDonationMessage.visibility = View.VISIBLE
                dialogView.tvNone.visibility = View.GONE
            }

            adapter.update(it)
        }

        donationMessageCountLiveData.observe(activity) {
            dialogView.tvDonationMessageCount.text = "(${it.moneyFormat()})"
        }
    }

    fun show() {
        alertDialog.show()
        getDonationMessageList()
    }
}
