package kr.co.vividnext.sodalive.explorer.profile.creator_community.all

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

class CreatorCommunityReportDialog(
    activity: Activity,
    layoutInflater: LayoutInflater,
    confirmButtonClick: (String) -> Unit
) {
    private val alertDialog: AlertDialog
    val dialogView = DialogCommunityPostReportBinding.inflate(layoutInflater)
    var reason = ""

    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.tvReport.setOnClickListener {
            if (reason.isNotBlank()) {
                alertDialog.dismiss()
                confirmButtonClick(reason)
            } else {
                Toast.makeText(activity, "신고 이유를 선택하세요.", Toast.LENGTH_LONG).show()
            }
        }

        dialogView.radioGroup.setOnCheckedChangeListener { radioGroup, checkedId ->
            val radioButton = radioGroup.findViewById<RadioButton>(checkedId)
            reason = radioButton.text.toString()
        }
    }

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