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

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 androidx.appcompat.app.AlertDialog
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.LiveData
import coil.load
import coil.transform.RoundedCornersTransformation
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.databinding.DialogLiveRoomUserProfileBinding
import kr.co.vividnext.sodalive.extensions.dpToPx

class LiveRoomUserProfileDialog(
    private val activity: FragmentActivity,
    private val userProfileLiveData: LiveData<GetLiveRoomUserProfileResponse>,
    layoutInflater: LayoutInflater,
    private val isStaff: (Long) -> Boolean,
    private val onClickSetManager: (Long) -> Unit,
    private val onClickReleaseManager: (Long) -> Unit,
    private val onClickFollow: (Long) -> Unit,
    private val onClickUnFollow: (Long) -> Unit,
    private val onClickInviteSpeaker: (Long) -> Unit,
    private val onClickChangeListener: (Long) -> Unit,
    private val onClickKickOut: (Long) -> Unit,
    private val onClickNoChatting: (Long, String, String) -> Unit,
    private val onClickPopupMenu: (Long, String, Boolean, View) -> Unit,
) {
    private val alertDialog: AlertDialog
    private val dialogView = DialogLiveRoomUserProfileBinding.inflate(layoutInflater)

    private var isIntroduceFold = true

    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()
        bindUserProfile()
    }

    fun dismiss() {
        alertDialog.dismiss()
    }

    fun show() {
        alertDialog.show()

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

        alertDialog.window?.attributes = lp
    }

    fun isShowing(): Boolean {
        return alertDialog.isShowing
    }

    private fun setupView() {
        val profileLP = dialogView.ivProfile.layoutParams
        profileLP.width = activity.resources.displayMetrics.widthPixels - (66.7f.dpToPx()).toInt()
        profileLP.height = activity.resources.displayMetrics.widthPixels - (66.7f.dpToPx()).toInt()
        dialogView.ivProfile.layoutParams = profileLP
    }

    private fun bindUserProfile() {
        userProfileLiveData.observe(activity) { userProfile ->
            dialogView.ivProfile.load(userProfile.profileUrl) {
                crossfade(true)
                placeholder(R.drawable.ic_place_holder)
                transformations(RoundedCornersTransformation(8.dpToPx()))
            }

            dialogView.tvNickname.text = userProfile.nickname
            dialogView.tvGender.text = userProfile.gender
            dialogView.ivClose.setOnClickListener { alertDialog.dismiss() }
            dialogView.tvIntroduce.setOnClickListener {
                isIntroduceFold = !isIntroduceFold

                if (isIntroduceFold) {
                    dialogView.tvIntroduce.maxLines = 2
                } else {
                    dialogView.tvIntroduce.maxLines = Int.MAX_VALUE
                }
            }

            if (userProfile.isSpeaker != null) {
                dialogView.tvInviteSpeaker.visibility = View.VISIBLE

                if (userProfile.isSpeaker) {
                    dialogView.tvInviteSpeaker.text = "리스너 변경"
                    dialogView.tvInviteSpeaker.setOnClickListener {
                        onClickChangeListener(userProfile.userId)
                        alertDialog.dismiss()
                    }
                } else {
                    dialogView.tvInviteSpeaker.text = "스피커 초대"
                    dialogView.tvInviteSpeaker.setOnClickListener {
                        onClickInviteSpeaker(userProfile.userId)
                        alertDialog.dismiss()
                    }
                }
            } else {
                dialogView.tvInviteSpeaker.visibility = View.GONE
                dialogView.tvKickOut.visibility = View.GONE
            }

            if (userProfile.isManager != null) {
                dialogView.tvSetManager.visibility = View.VISIBLE
                dialogView.tvNoChatting.visibility = View.VISIBLE

                if (userProfile.isManager) {
                    dialogView.tvSetManager.text = "스탭 해제"
                    dialogView.tvSetManager.setOnClickListener {
                        onClickReleaseManager(userProfile.userId)
                        alertDialog.dismiss()
                    }
                } else {
                    dialogView.tvSetManager.text = "스탭 지정"
                    dialogView.tvSetManager.setOnClickListener {
                        onClickSetManager(userProfile.userId)
                        alertDialog.dismiss()
                    }
                }

                dialogView.tvNoChatting.setOnClickListener {
                    onClickNoChatting(
                        userProfile.userId,
                        userProfile.nickname,
                        userProfile.profileUrl
                    )
                }
            } else {
                dialogView.tvSetManager.visibility = View.GONE
                dialogView.tvNoChatting.visibility = View.GONE
            }

            if (
                (userProfile.isSpeaker != null && !isStaff(userProfile.userId)) ||
                (userProfile.isSpeaker != null && userProfile.isManager != null)
            ) {
                dialogView.tvKickOut.visibility = View.VISIBLE
                dialogView.tvKickOut.setOnClickListener {
                    onClickKickOut(userProfile.userId)
                    alertDialog.dismiss()
                }
            }

            if (userProfile.isFollowing != null) {
                dialogView.ivFollow.visibility = View.VISIBLE

                if (userProfile.isFollowing) {
                    dialogView.ivFollow.setImageResource(R.drawable.btn_following)
                    dialogView.ivFollow.setOnClickListener { onClickUnFollow(userProfile.userId) }
                } else {
                    dialogView.ivFollow.setImageResource(R.drawable.btn_follow)
                    dialogView.ivFollow.setOnClickListener { onClickFollow(userProfile.userId) }
                }

                if (userProfile.tags.isNotBlank()) {
                    dialogView.tvTags.text = userProfile.tags
                    dialogView.tvTags.visibility = View.VISIBLE
                } else {
                    dialogView.tvTags.visibility = View.GONE
                }

                if (userProfile.introduce.isNotBlank()) {
                    dialogView.tvIntroduce.text = userProfile.introduce
                    dialogView.tvIntroduce.visibility = View.VISIBLE
                } else {
                    dialogView.tvIntroduce.visibility = View.GONE
                }
            } else {
                dialogView.tvTags.visibility = View.GONE
                dialogView.ivFollow.visibility = View.GONE
                dialogView.tvIntroduce.visibility = View.GONE
            }

            dialogView.ivMenu.setOnClickListener {
                onClickPopupMenu(
                    userProfile.userId,
                    userProfile.nickname,
                    userProfile.isBlock,
                    dialogView.ivMenu
                )
            }
        }
    }
}
