package kr.co.vividnext.sodalive.audio_content.order

import android.annotation.SuppressLint
import android.app.Activity
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AlertDialog
import coil.load
import coil.transform.CircleCropTransformation
import coil.transform.RoundedCornersTransformation
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
import kr.co.vividnext.sodalive.databinding.DialogAudioContentOrderConfirmBinding
import kr.co.vividnext.sodalive.extensions.dpToPx
import kr.co.vividnext.sodalive.extensions.moneyFormat

@SuppressLint("SetTextI18n")
class AudioContentOrderConfirmDialog(
    activity: Activity,
    layoutInflater: LayoutInflater,
    title: String,
    theme: String,
    coverImageUrl: String,
    profileImageUrl: String,
    nickname: String,
    duration: String,
    orderType: OrderType,
    price: Int,
    confirmButtonClick: () -> Unit,
) {

    private val alertDialog: AlertDialog

    val dialogView = DialogAudioContentOrderConfirmBinding.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.tvTheme.text = theme
        dialogView.tvProfileNickname.text = nickname

        dialogView.ivCover.load(coverImageUrl) {
            crossfade(true)
            placeholder(R.drawable.ic_place_holder)
            transformations(RoundedCornersTransformation(4f))
        }

        dialogView.ivProfile.load(profileImageUrl) {
            crossfade(true)
            placeholder(R.drawable.ic_place_holder)
            transformations(CircleCropTransformation())
        }

        dialogView.tvDuration.text = duration

        if (SharedPreferenceManager.userId == 17958L) {
            dialogView.ivCan.visibility = View.GONE
            dialogView.tvPrice.text = "${(price * 110).moneyFormat()}원"
        } else {
            dialogView.ivCan.visibility = View.VISIBLE
            dialogView.tvPrice.text = price.moneyFormat()
        }

        if (SharedPreferenceManager.userId == 17958L) {
            dialogView.tvNotice.text = if (orderType == OrderType.RENTAL) {
                "콘텐츠를 대여하시겠습니까?"
            } else {
                "콘텐츠를 소장하시겠습니까?"
            }
        } else {
            dialogView.tvNotice.text = if (orderType == OrderType.RENTAL) {
                "콘텐츠를 대여하시겠습니까?\n아래 캔이 차감됩니다."
            } else {
                "콘텐츠를 소장하시겠습니까?\n아래 캔이 차감됩니다."
            }
        }

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

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

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