package com.adealink.weparty.media

import android.content.DialogInterface
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.Window
import android.view.WindowManager
import androidx.fragment.app.FragmentManager
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.media.MediaInfo
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.onClick
import com.adealink.weparty.R
import com.adealink.weparty.commonui.dialogfragment.BaseDialogFragment
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.databinding.DialogMediaConflictBinding
import com.adealink.weparty.media.datasource.MediaLocalService

class MediaConflictDialog : BaseDialogFragment(R.layout.dialog_media_conflict) {

    companion object {
        @JvmStatic
        fun show(
            exitMediaInfo: MediaInfo,
            enterMediaInfo: MediaInfo,
            fm: FragmentManager,
            onPositive: (() -> Unit)? = null,
            onNegative: (() -> Unit)? = null,
            onDismiss: (() -> Unit)? = null,
        ) {
            MediaConflictDialog().apply {
                this.exitMediaInfo = exitMediaInfo
                this.enterMediaInfo = enterMediaInfo
                this.onPositive = onPositive
                this.onNegative = onNegative
                this.onDismiss = onDismiss
            }.show(fm, "CallMediaConflictDialog")
        }
    }

    private val binding by viewBinding(DialogMediaConflictBinding::bind)

    private var exitMediaInfo: MediaInfo? = null
    private var enterMediaInfo: MediaInfo? = null
    private var onPositive: (() -> Unit)? = null
    private var onNegative: (() -> Unit)? = null
    private var onDismiss: (() -> Unit)? = null

    private var remind = false

    override fun initViews() {
        super.initViews()
        binding.tvMessage.text = getCompatString(
            R.string.room_media_conflict_tips,
            exitMediaInfo?.mediaName ?: "",
            enterMediaInfo?.mediaName ?: ""
        )
        binding.clNeverRemind.setOnClickListener {
            remind = !remind
            MediaLocalService.updateConflictRemind(exitMediaInfo, enterMediaInfo, remind)
            updateDontRemind()
        }
        updateDontRemind()
        binding.topCloseBtn.onClick {
            onPositive?.invoke()
            dismiss()
        }
        binding.btnCancel.setOnClickListener {
            onNegative?.invoke()
            dismiss()
        }
        binding.btnConfirm.setOnClickListener {
            onPositive?.invoke()
            dismiss()
        }
    }

    private fun updateDontRemind() {
        if (remind) {
            binding.ivNeverRemind.setImageResource(R.drawable.common_check_selected_ic)
        } else {
            binding.ivNeverRemind.setImageResource(R.drawable.common_check_unselected_ic)
        }
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        onDismiss?.invoke()
    }

    override fun resetWindowAttributes(window: Window) {
        super.resetWindowAttributes(window)
        window.setLayout(
            300.dp(),
            WindowManager.LayoutParams.WRAP_CONTENT
        )
        window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        val attr = window.attributes
        attr.gravity = Gravity.CENTER
        attr.dimAmount = 0.7f
        window.attributes = attr
    }

}