package com.remax.visualnovel.extension

import android.annotation.SuppressLint
import android.app.Activity
import android.text.method.ScrollingMovementMethod
import android.view.Gravity
import android.view.ViewGroup
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.core.view.isVisible
import com.remax.visualnovel.R
import com.remax.visualnovel.databinding.DialogConfirmBinding
import com.remax.visualnovel.databinding.DialogDoubleBtnBinding
import com.remax.visualnovel.databinding.DialogSingleBtnLayout2Binding
import com.remax.visualnovel.databinding.DialogSingleBtnLayoutBinding
import com.remax.visualnovel.databinding.DialogSingleBtnWithIconBinding
import com.remax.visualnovel.extension.glide.loadNoCenterCrop
import com.remax.visualnovel.utils.KeyboardUtils
import com.remax.visualnovel.utils.spannablex.utils.dp
import com.remax.visualnovel.widget.dialoglib.LBindingDialog
import com.remax.visualnovel.widget.dialoglib.ScreenUtils
import com.remax.visualnovel.widget.ui.buttons.ButtonView
import com.remax.visualnovel.widget.uitoken.changeTextStyle
import com.remax.visualnovel.widget.uitoken.view.UITokenTextView
import kotlin.math.min


/**
 * 底部水平布局 两个按钮的dialog
 */
fun Activity.showConfirmDialog(
    @DrawableRes dialogBg: Int? = null,
    @StringRes title: Int? = null,
    @StringRes description: Int? = null,
    @DrawableRes topBtnIconRes: Int? = null,
    topBtnClick: (() -> Unit)? = null,
    @StringRes bottomLeftStrRes: Int? = null,
    @StringRes bottomRightStrRes: Int? = null,
    bottomRightClick: (() -> Unit)? = null,
): LBindingDialog<DialogConfirmBinding> {

    val dialog = LBindingDialog(this, DialogConfirmBinding::inflate)
        .with()
        .setCenter()

    dialog.setCanceledOnTouchOutside(false)
    dialog.setCancelable(false)

    dialog.binding.run {
        setOnClick(ivTop, tvRight, tvLeft) {
            when (this) {
                tvLeft ->  {
                    dialog.dismiss()
                }
                tvRight ->  {
                    bottomRightClick?.invoke()
                    dialog.dismiss()
                }
                ivTop -> topBtnClick?.invoke()
            }
        }

        if (dialogBg != null) root.background = getDrawable(dialogBg)
        if (topBtnIconRes != null) ivTop.setImageResource(topBtnIconRes) else tvTitle.isVisible = false
        if (title != null) tvTitle.text = getString(title) else tvTitle.isVisible = false
        if (description != null) tvDescription.text = getString(description) else tvDescription.isVisible = false
        if (bottomLeftStrRes != null) tvLeft.text = getString(bottomLeftStrRes) else tvLeft.isVisible = false
        if (bottomRightStrRes != null) tvRight.text = getString(bottomRightStrRes) else tvRight.isVisible = false
    }

    dialog.show()
    return dialog
}

/**
 * 双按钮的全局统一的dialog样式
 */
fun Activity.showDoubleBtnDialog(
    title: String = getString(R.string.tips),
    text: String,
    topBtnText: String? = null,
    topBtnClick: (() -> Unit)? = null,
    bottomBtnText: String? = null,
    bottomBtnClick: (() -> Unit)? = null,
    isShow: Boolean = true,
    isDel: Boolean = false,
    contentGravity: Int = Gravity.CENTER,
    needMaxHeight: Boolean = true,
    @DrawableRes titleImage: Int? = null
): LBindingDialog<DialogDoubleBtnBinding> {
    val dialog = LBindingDialog(this, DialogDoubleBtnBinding::inflate)
        .with()
        .setCenter()

    dialog.setCanceledOnTouchOutside(false)
    dialog.setCancelable(false)

    dialog.binding.run {
        if (needMaxHeight) {
            textTv.maxHeight = (ScreenUtils.getHeightRealPixels() * 0.7).toInt()
            textTv.movementMethod = ScrollingMovementMethod.getInstance()
        }

        setOnClick(leftBtn, rightBtn) {
            when (this) {
                leftBtn -> bottomBtnClick?.invoke()
                rightBtn -> topBtnClick?.invoke()
            }
            dialog.dismiss()
        }

        titleTv.text = title
        titleIv.isVisible = titleImage != null
        if (titleIv.isVisible) {
            titleTv.isVisible = false
            titleIv.setImageResource(titleImage!!)
            textTv.setMargin(topMargin = 10.dp)
        }

        textTv.gravity = contentGravity
        textTv.text = "$text\n"
        if (bottomBtnText != null) {
            leftBtn.text = bottomBtnText
        }
        if (isDel){
            rightBtn.setButtonStyle(ButtonView.DefaultButton_Destructive)
        }
        if (topBtnText != null) {
            rightBtn.text = topBtnText
        }
    }

    if (isShow) {
        dialog.show()
    }

    return dialog
}

/**
 * 单按钮的全局统一的dialog样式
 */
fun Activity.showSingleBtnDialog(
    title: String = getString(R.string.tips),
    text: String,
    btnText: String? = null,
    contentGravity: Int = Gravity.CENTER,
    isShow: Boolean = true,
    autoDismiss: Boolean = true,
    needMaxHeight: Boolean = true,
    btnClick: (() -> Unit)? = null
): LBindingDialog<DialogSingleBtnLayoutBinding> {
    val dialog = showSingleCancelBtnDialog(title, text, btnText, btnClick, isShow, autoDismiss, contentGravity, needMaxHeight)
    dialog.binding.cancelBtn.isVisible = false
    return dialog
}


/**
 * 单按钮带图标的全局统一的dialog样式
 */
fun Activity.showIconAndSingleBtnDialog(
    title: String,
    text: String,
    iconRes: Int? = null,
    btnText: String? = null,
    btnClick: (() -> Unit)? = null,
    isShow: Boolean = true,
    closeIsShow: Boolean = false,
    iconUrl: String? = null,
    contentGravity: Int = Gravity.CENTER,
    needMaxHeight: Boolean = true
): LBindingDialog<DialogSingleBtnWithIconBinding> {
    val dialog = LBindingDialog(this, DialogSingleBtnWithIconBinding::inflate).with()
        .setCenter()
        .setCancelBtn(R.id.ivClose)

    dialog.setCanceledOnTouchOutside(false)
    dialog.setCancelable(false)

    dialog.binding.run {
        if (needMaxHeight) {
            tvText.maxHeight = (ScreenUtils.getHeightRealPixels() * 0.7).toInt()
            tvText.movementMethod = ScrollingMovementMethod.getInstance()
        }
        tvText.gravity = contentGravity
        setOnClick(tvBtn) {
            dialog.dismiss()
            btnClick?.invoke()
        }

        ivClose.isVisible = closeIsShow
        if (iconRes != null) {
            ivIcon.setImageResource(iconRes)
        } else {
            ivIcon.loadNoCenterCrop(iconUrl ?: "")
        }
        tvTitle.text = title
        tvText.text = text
        tvBtn.text = btnText ?: this@showIconAndSingleBtnDialog.getString(R.string.i_understand)
    }
    if (isShow) {
        dialog.show()
    }
    return dialog
}

fun Activity.showMoreTxtDialog(
    titleText: String = getString(R.string.tips),
    texts: List<String>,
    btnText: String? = null,
    btnClick: (() -> Unit)? = null,
    isShow: Boolean = true,
    autoDismiss: Boolean = true,
    showCloseBotton: Boolean = false,
    contentGravity: Int = Gravity.START,
    showCancel: Boolean = false
): LBindingDialog<DialogSingleBtnLayout2Binding> {
    val dialog = LBindingDialog(this, DialogSingleBtnLayout2Binding::inflate).with()
        .setCenter()
        .setCancelBtn(R.id.cancelBtn)
        .setCancelBtn(R.id.closeBtn)

    dialog.setCanceledOnTouchOutside(false)
    dialog.setCancelable(false)

    dialog.binding.run {
        cancelBtn.isVisible = showCancel
        closeBtn.isVisible = showCloseBotton

        dialog.setOnShowListener {
            val groupHeight = group.measuredHeight
            val maxHeight = ScreenUtils.getHeightRealPixels() * 0.7f - 24.dp - 96.dp
            scrollView.setSize(height = min(groupHeight, maxHeight.toInt()))
        }

        setOnClick(okBtn) {
            btnClick?.invoke()
            if (autoDismiss) {
                dialog.dismiss()
            }
        }
        okBtn.setText(R.string.i_understand)
        btnText?.let(okBtn::setText)

        title.text = titleText

        group.removeAllViews()
        texts.forEachIndexed { index, s ->
            val textView = UITokenTextView(this@showMoreTxtDialog)
            group.addView(textView)
            with(textView) {
                setSize(ViewGroup.LayoutParams.MATCH_PARENT)
                text = s
                gravity = contentGravity
                changeTextStyle {
                    textUIColorToken = getString(R.string.color_txt_primary_normal)
                    textUITextToken = getString(R.string.txt_body_m)
                }
                if (index > 0) {
                    setMargin(topMargin = 16.dp)
                }
            }
        }
        if (isShow) {
            dialog.show()
        }
    }

    return dialog
}

/**
 * 单按钮的全局统一的dialog样式-待顶部关闭按钮
 */
@SuppressLint("SetTextI18n")
fun Activity.showSingleCancelBtnDialog(
    title: String,
    text: String,
    btnText: String? = null,
    btnClick: (() -> Unit)? = null,
    isShow: Boolean = true,
    autoDismiss: Boolean = true,
    contentGravity: Int = Gravity.CENTER,
    needMaxHeight: Boolean = true
): LBindingDialog<DialogSingleBtnLayoutBinding> {
    val dialog = LBindingDialog(this, DialogSingleBtnLayoutBinding::inflate)
        .with()
        .setCenter()
        .setCancelBtn(R.id.cancelBtn)

    dialog.setCanceledOnTouchOutside(false)
    dialog.setCancelable(false)

    dialog.binding.run {
        if (needMaxHeight) {
            singleText.maxHeight = (ScreenUtils.getHeightRealPixels() * 0.7).toInt()
            singleText.movementMethod = ScrollingMovementMethod.getInstance()
        }

        setOnClick(singleBtn) {
            btnClick?.invoke()
            if (autoDismiss) {
                dialog.dismiss()
            }
        }

        singleTitle.text = title
        singleText.text = "$text\n"
        singleText.gravity = contentGravity
        singleBtn.text = btnText ?: this@showSingleCancelBtnDialog.getString(R.string.i_understand)
    }
    if (isShow) {
        dialog.show()
    }
    return dialog
}


/**
 * 退出编辑二次确认弹窗
 */
fun Activity.showExitHintDialog(isFinish: Boolean = true, continueCallback: (() -> Unit)? = null) {
    showDoubleBtnDialog(
        getString(R.string.tips),
        getString(R.string.exit_dialog_hint),
        topBtnText = getString(R.string.exit),
        topBtnClick = {
            if (KeyboardUtils.isSoftInputVisible(this)) {
                KeyboardUtils.hideSoftInput(this)
            }
            continueCallback?.invoke()
            if (isFinish) {
                finish()
            }
        })
}