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.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.MutableLiveData
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.live.roulette.config.RouletteSettingsViewModel.SelectedRoulette
import kr.co.vividnext.sodalive.mypage.can.charge.CanChargeActivity

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

    private val selectedRouletteLiveData = MutableLiveData(
        SelectedRoulette.ROULETTE_1
    )

    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() {
        if (previewList.isEmpty()) {
            alertDialog.dismiss()
        } else {
            initSelectRouletteButton()
            setRouletteData(previewList[0])
        }

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

    private fun initSelectRouletteButton() {
        if (previewList.size < 2) {
            dialogView.llSelectRoulette.visibility = View.GONE
        } else {
            dialogView.llSelectRoulette.visibility = View.VISIBLE

            if (previewList.size > 2) {
                dialogView.llSelectRoulette3.visibility = View.VISIBLE
            } else {
                dialogView.llSelectRoulette3.visibility = View.GONE
            }
        }

        dialogView.llSelectRoulette1.setOnClickListener {
            if (selectedRouletteLiveData.value != SelectedRoulette.ROULETTE_1) {
                selectedRouletteLiveData.value = SelectedRoulette.ROULETTE_1
            }
        }

        dialogView.llSelectRoulette2.setOnClickListener {
            if (selectedRouletteLiveData.value != SelectedRoulette.ROULETTE_2) {
                selectedRouletteLiveData.value = SelectedRoulette.ROULETTE_2
            }
        }

        dialogView.llSelectRoulette3.setOnClickListener {
            if (selectedRouletteLiveData.value != SelectedRoulette.ROULETTE_3) {
                selectedRouletteLiveData.value = SelectedRoulette.ROULETTE_3
            }
        }

        selectedRouletteLiveData.observe(activity) {
            deselectAllRoulette()
            when (it) {
                SelectedRoulette.ROULETTE_2 -> {
                    selectRouletteButton(
                        dialogView.ivSelectRoulette2,
                        dialogView.llSelectRoulette2,
                        dialogView.tvSelectRoulette2
                    )
                    setRouletteData(previewList[1])
                    dialogView.tvCancel.setTextColor(
                        ContextCompat.getColor(activity, R.color.color_ffcb14)
                    )
                    dialogView.tvCancel.setBackgroundResource(
                        R.drawable.bg_round_corner_10_transparent_ffcb14
                    )
                }

                SelectedRoulette.ROULETTE_3 -> {
                    selectRouletteButton(
                        dialogView.ivSelectRoulette3,
                        dialogView.llSelectRoulette3,
                        dialogView.tvSelectRoulette3
                    )
                    setRouletteData(previewList[2])
                    dialogView.tvCancel.setTextColor(
                        ContextCompat.getColor(activity, R.color.color_ff14d9)
                    )
                    dialogView.tvCancel.setBackgroundResource(
                        R.drawable.bg_round_corner_10_transparent_ff14d9
                    )
                }

                else -> {
                    selectRouletteButton(
                        dialogView.ivSelectRoulette1,
                        dialogView.llSelectRoulette1,
                        dialogView.tvSelectRoulette1
                    )
                    setRouletteData(previewList[0])
                    dialogView.tvCancel.setTextColor(
                        ContextCompat.getColor(activity, R.color.color_3bb9f1)
                    )
                    dialogView.tvCancel.setBackgroundResource(
                        R.drawable.bg_round_corner_10_transparent_3bb9f1
                    )
                }
            }
        }
    }

    private fun deselectAllRoulette() {
        dialogView.ivSelectRoulette1.visibility = View.GONE
        dialogView.ivSelectRoulette2.visibility = View.GONE
        dialogView.ivSelectRoulette3.visibility = View.GONE

        dialogView.llSelectRoulette1.setBackgroundResource(R.drawable.bg_round_corner_6_7_13181b)
        dialogView.tvSelectRoulette1.setTextColor(
            ContextCompat.getColor(
                activity,
                R.color.color_3bb9f1
            )
        )

        dialogView.llSelectRoulette2.setBackgroundResource(R.drawable.bg_round_corner_6_7_13181b)
        dialogView.tvSelectRoulette2.setTextColor(
            ContextCompat.getColor(
                activity,
                R.color.color_ffcb14
            )
        )

        dialogView.llSelectRoulette3.setBackgroundResource(R.drawable.bg_round_corner_6_7_13181b)
        dialogView.tvSelectRoulette3.setTextColor(
            ContextCompat.getColor(
                activity,
                R.color.color_ff14d9
            )
        )
    }

    private fun selectRouletteButton(
        ivSelectRoulette: ImageView,
        llSelectRoulette: LinearLayout,
        tvSelectRoulette: TextView
    ) {
        ivSelectRoulette.visibility = View.VISIBLE
        llSelectRoulette.setBackgroundResource(
            when (selectedRouletteLiveData.value) {
                SelectedRoulette.ROULETTE_2 -> R.drawable.bg_round_corner_6_7_ffcb14
                SelectedRoulette.ROULETTE_3 -> R.drawable.bg_round_corner_6_7_ff14d9
                else -> R.drawable.bg_round_corner_6_7_3bb9f1
            }
        )
        tvSelectRoulette.setTextColor(
            ContextCompat.getColor(
                activity,
                when (selectedRouletteLiveData.value) {
                    SelectedRoulette.ROULETTE_2 -> R.color.black
                    else -> R.color.color_eeeeee
                }

            )
        )
    }

    @SuppressLint("SetTextI18n")
    private fun setRouletteData(roulettePreview: RoulettePreview) {
        dialogView.tvSpinRoulette.text = "${roulettePreview.can}캔으로 룰렛 돌리기"
        dialogView.tvSpinRoulette.setOnClickListener {
            if (onClickSpin != null) {
                onClickSpin!!(roulettePreview.id)
            }

            alertDialog.dismiss()
        }

        dialogView.tvSpinRoulette.setTextColor(
            ContextCompat.getColor(
                activity,
                when (selectedRouletteLiveData.value) {
                    SelectedRoulette.ROULETTE_2 -> R.color.black
                    else -> R.color.white
                }
            )
        )
        dialogView.tvSpinRoulette.setBackgroundResource(
            when (selectedRouletteLiveData.value) {
                SelectedRoulette.ROULETTE_2 -> R.drawable.bg_round_corner_10_ffcb14
                SelectedRoulette.ROULETTE_3 -> R.drawable.bg_round_corner_10_ff14d9
                else -> R.drawable.bg_round_corner_10_3bb9f1
            }
        )

        dialogView.tvTitle.text = title.ifBlank {
            when (selectedRouletteLiveData.value) {
                SelectedRoulette.ROULETTE_2 -> "룰렛 2"
                SelectedRoulette.ROULETTE_3 -> "룰렛 3"
                else -> "룰렛 1"
            }
        }

        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
        }

        dialogView.llRouletteOptionContainer.removeAllViews()
        roulettePreview.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
    }
}
