package kr.co.vividnext.sodalive.audio_content.detail

import android.annotation.SuppressLint
import android.app.Activity
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.WindowManager
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import kr.co.vividnext.sodalive.databinding.DialogAudioContentDeleteBinding
import kr.co.vividnext.sodalive.extensions.dpToPx

@SuppressLint("SetTextI18n")
class AudioContentDeleteDialog(
    activity: Activity,
    layoutInflater: LayoutInflater,
    title: String,
    confirmButtonClick: () -> Unit
) {

    private val alertDialog: AlertDialog

    val dialogView = DialogAudioContentDeleteBinding.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.tvTitle.text = "[$title]을 삭제하시겠습니까?"
        dialogView.tvCancel.setOnClickListener {
            alertDialog.dismiss()
        }

        dialogView.tvConfirm.setOnClickListener {
            if (dialogView.tvNotice.isSelected) {
                alertDialog.dismiss()
                confirmButtonClick()
            } else {
                Toast.makeText(
                    activity,
                    "동의하셔야 삭제할 수 있습니다.",
                    Toast.LENGTH_LONG
                ).show()
            }
        }

        dialogView.tvNotice.setOnClickListener {
            it.isSelected = !it.isSelected
        }
    }

    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
    }
}
