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

import android.app.Activity
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.WindowManager
import androidx.appcompat.app.AlertDialog
import coil.load
import coil.transform.CircleCropTransformation
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.databinding.DialogLiveNoChattingBinding
import kr.co.vividnext.sodalive.extensions.dpToPx

class LiveRoomNoChattingDialog(
    activity: Activity,
    layoutInflater: LayoutInflater,
    nickname: String,
    profileUrl: String,
    confirmButtonClick: () -> Unit,
) {

    private val alertDialog: AlertDialog

    val dialogView = DialogLiveNoChattingBinding.inflate(layoutInflater)

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

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

        dialogView.tvCancel.setOnClickListener {
            alertDialog.dismiss()
        }

        dialogView.tvConfirm.setOnClickListener {
            alertDialog.dismiss()
            confirmButtonClick()
        }

        dialogView.tvNickname.text = nickname
        dialogView.ivProfile.load(profileUrl) {
            crossfade(true)
            placeholder(R.drawable.bg_placeholder)
            transformations(CircleCropTransformation())
        }
    }

    fun show(width: Int) {
        alertDialog.show()

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

        alertDialog.window?.attributes = lp
    }
}
