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

import android.annotation.SuppressLint
import android.content.Intent
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.AppCompatActivity
import coil.load
import coil.transform.CircleCropTransformation
import com.google.android.material.bottomsheet.BottomSheetDialog
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.common.Constants
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
import kr.co.vividnext.sodalive.databinding.DialogLiveRoomDonationBinding
import kr.co.vividnext.sodalive.extensions.moneyFormat
import kr.co.vividnext.sodalive.mypage.can.charge.CanChargeActivity

class LiveRoomDonationDialog(
    private val activity: AppCompatActivity,
    layoutInflater: LayoutInflater,
    onClickDonation: (Int, String) -> Unit
) {

    private val bottomSheetDialog: BottomSheetDialog = BottomSheetDialog(activity)
    private val dialogView = DialogLiveRoomDonationBinding.inflate(layoutInflater)

    init {
        bottomSheetDialog.setContentView(dialogView.root)
        bottomSheetDialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        bottomSheetDialog.setCancelable(false)

        dialogView.tvCancel.setOnClickListener { bottomSheetDialog.dismiss() }
        dialogView.tvDonation.setOnClickListener {
            try {
                val can = dialogView.etDonationCan.text.toString().toInt()
                val message = dialogView.etDonationMessage.text.toString()

                if (can > 0) {
                    bottomSheetDialog.dismiss()
                    onClickDonation(can, message)
                } else {
                    Toast.makeText(
                        activity,
                        "1캔 이상 후원하실 수 있습니다.",
                        Toast.LENGTH_LONG
                    ).show()
                }
            } catch (e: NumberFormatException) {
                Toast.makeText(
                    activity,
                    "1캔 이상 후원하실 수 있습니다.",
                    Toast.LENGTH_LONG
                ).show()
            }
        }

        setupView()
    }

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

            val lp = WindowManager.LayoutParams()
            lp.copyFrom(bottomSheetDialog.window?.attributes)
            lp.width = width
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT

            bottomSheetDialog.window?.attributes = lp
        }
    }

    @SuppressLint("SetTextI18n")
    private fun setupView() {
        dialogView.tvCan.text = SharedPreferenceManager.can.moneyFormat()
        dialogView.tvPlus10.setOnClickListener { addCan(10) }
        dialogView.tvPlus100.setOnClickListener { addCan(100) }
        dialogView.tvPlus1000.setOnClickListener { addCan(1000) }
        dialogView.tvPlus10000.setOnClickListener { addCan(10000) }

        dialogView.ivProfile.load(SharedPreferenceManager.profileImage) {
            crossfade(true)
            placeholder(R.drawable.ic_logo)
            transformations(CircleCropTransformation())
        }

        dialogView.tvCan.setOnClickListener {
            bottomSheetDialog.dismiss()

            val intent = Intent(activity, CanChargeActivity::class.java)
            intent.putExtra(Constants.EXTRA_PREV_LIVE_ROOM, true)
            activity.startActivity(intent)
        }
    }

    @SuppressLint("SetTextI18n")
    private fun addCan(can: Int) {
        try {
            val currentCan = dialogView.etDonationCan.text.toString().toInt()
            dialogView.etDonationCan.setText((currentCan + can).toString())
        } catch (e: NumberFormatException) {
            dialogView.etDonationCan.setText(can.toString())
        }
    }
}
