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

import android.app.Activity
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.LinearLayout
import androidx.appcompat.app.AlertDialog
import kr.co.vividnext.sodalive.databinding.DialogLiveRoomBinding
import kr.co.vividnext.sodalive.extensions.dpToPx

class LiveRoomDialog(
    activity: Activity,
    layoutInflater: LayoutInflater,
) {

    private val alertDialog: AlertDialog
    private val dialogView = DialogLiveRoomBinding.inflate(layoutInflater)
    private val handler = Handler(Looper.getMainLooper())

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

        alertDialog = dialogBuilder.create()
        alertDialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    }

    fun show(width: Int) {
        if (!alertDialog.isShowing) {
            alertDialog.show()

            val lp = WindowManager.LayoutParams()
            lp.copyFrom(alertDialog.window?.attributes)
            lp.width = width - (53.4f.dpToPx()).toInt()
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT

            alertDialog.window?.attributes = lp

            if (dialogView.llActionButtons.visibility == View.GONE) {
                alertDialog.setCancelable(true)
                val llContentLp = dialogView.llContent.layoutParams as LinearLayout.LayoutParams
                llContentLp.bottomMargin = 0f.dpToPx().toInt()
                dialogView.llContent.layoutParams = llContentLp

                handler.postDelayed({
                    dismiss()
                }, 1000)
            } else {
                alertDialog.setCancelable(false)
                val llContentLp = dialogView.llContent.layoutParams as LinearLayout.LayoutParams
                llContentLp.bottomMargin = 10f.dpToPx().toInt()
                dialogView.llContent.layoutParams = llContentLp
            }
        }
    }

    private fun dismiss() {
        alertDialog.dismiss()
        dialogView.tvContent.text = ""
        dialogView.llActionButtons.visibility = View.GONE
        dialogView.tvCancel.text = ""
        dialogView.tvConfirm.text = ""
        dialogView.tvCancel.setOnClickListener { }
        dialogView.tvConfirm.setOnClickListener { }

        val llContentLp = dialogView.llContent.layoutParams as LinearLayout.LayoutParams
        llContentLp.bottomMargin = 10f.dpToPx().toInt()
        dialogView.llContent.layoutParams = llContentLp
    }

    fun setCancel(title: String, action: () -> Unit) {
        dialogView.tvCancel.text = title
        dialogView.tvCancel.setOnClickListener {
            dismiss()
            action()
        }

        dialogView.llActionButtons.visibility = View.VISIBLE
    }

    fun setConfirm(title: String, action: () -> Unit) {
        dialogView.tvConfirm.text = title
        dialogView.tvConfirm.setOnClickListener {
            dismiss()
            action()
        }

        dialogView.llActionButtons.visibility = View.VISIBLE
    }

    fun setContent(content: String) {
        dialogView.tvContent.text = content
    }
}
