package kr.co.vividnext.sodalive.live.roulette

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.FragmentActivity
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.DialogRoulettePreviewBinding
import kr.co.vividnext.sodalive.extensions.dpToPx
import kr.co.vividnext.sodalive.extensions.moneyFormat
import kr.co.vividnext.sodalive.mypage.can.charge.CanChargeActivity

class RoulettePreviewDialog(
    private val activity: FragmentActivity,
    private val preview: RoulettePreview,
    private val title: String = "",
    private val onClickSpin: (() -> Unit)? = null,
    layoutInflater: LayoutInflater
) {
    private val alertDialog: AlertDialog
    private val dialogView = DialogRoulettePreviewBinding.inflate(layoutInflater)

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

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

        setupView()
    }

    fun show() {
        alertDialog.show()

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

        alertDialog.window?.attributes = lp
    }

    @SuppressLint("SetTextI18n")
    private fun setupView() {
        dialogView.tvCancel.setOnClickListener { alertDialog.dismiss() }

        dialogView.tvSpinRoulette.text = "${preview.can}캔으로 룰렛 돌리기"
        dialogView.tvSpinRoulette.setOnClickListener {
            if (onClickSpin != null) {
                onClickSpin!!()
            }

            alertDialog.dismiss()
        }

        dialogView.tvTitle.text = title.ifBlank { "룰렛" }
        if (onClickSpin != null) {
            dialogView.tvCan.visibility = View.VISIBLE
            dialogView.tvCan.text = SharedPreferenceManager.can.moneyFormat()
            dialogView.tvCan.setOnClickListener {
                alertDialog.dismiss()

                val intent = Intent(activity, CanChargeActivity::class.java)
                intent.putExtra(Constants.EXTRA_GO_TO_PREV_PAGE, true)
                activity.startActivity(intent)
            }
        } else {
            dialogView.tvCan.visibility = View.GONE
        }

        preview.items.forEachIndexed { index, item ->
            dialogView.llRouletteOptionContainer.addView(createOptionView(index, item))
        }
    }

    @SuppressLint("SetTextI18n")
    private fun createOptionView(index: Int, item: RoulettePreviewItem): View {
        val itemView = LayoutInflater
            .from(activity)
            .inflate(
                R.layout.layout_roulette_preview_item,
                dialogView.llRouletteOptionContainer,
                false
            )

        val tvItemTitle = itemView.findViewById<TextView>(R.id.tv_roulette_item_title)
        val tvOrder = itemView.findViewById<TextView>(R.id.tv_order)

        tvItemTitle.text = "${item.title} (${item.percent})"
        tvOrder.text = "${index + 1}"

        return itemView
    }
}
