package com.abbidot.tracker.dialog

import android.content.Context
import android.content.DialogInterface
import android.text.InputFilter
import android.text.InputFilter.LengthFilter
import android.text.InputType
import android.view.View
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.constant.GetResultCallback
import com.qmuiteam.qmui.skin.QMUISkinManager
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog


/**
 *Created  by .yzq on 2021/11/16/016.
 * @link
 * @description:通用提示Dialog
 */
class DialogUtils {

    companion object {

        private const val CURRENT_DIALOG_STYLE = R.style.QMUI_Dialog

        /**
         * 信息提示Dialog
         * QMUITipDialog.Builder.ICON_TYPE_SUCCESS
         * QMUITipDialog.Builder.ICON_TYPE_FAIL
         * view:通过view线程自动销毁
         */
        fun showTipDialog(context: Context,
                          view: View,
                          tipWord: CharSequence,
                          @QMUITipDialog.Builder.IconType iconType: Int,
                          isFinish: Boolean = false) {

            val tipDialog = if (QMUITipDialog.Builder.ICON_TYPE_NOTHING == iconType) {
                QMUITipDialog.Builder(context).setTipWord(tipWord).create()
            } else {
                QMUITipDialog.Builder(context).setIconType(iconType).setTipWord(tipWord).create()
            }
            val activity = (context as BaseActivity<*>)
            //activity前台显示运行才弹出
            if (activity.isFrontRunning) {
                tipDialog.show()
                view.postDelayed({
                    tipDialog.dismiss()
                    if (isFinish && !activity.isFinishing) activity.finish()
                }, 1500)
            }
        }

        /**
         * 信息提示Dialog
         * 显示文字提示信息
         * view:通过view线程自动销毁
         */
        fun showTipDialog(context: Context,
                          view: View,
                          tipWord: CharSequence,
                          isFinish: Boolean = false) {
            showTipDialog(context, view, tipWord, QMUITipDialog.Builder.ICON_TYPE_NOTHING,
                    isFinish = isFinish)
        }


        /**
         * 弹窗Dialog
         */
        fun showMessageNegativeDialog(context: Context,
                                      title: String,
                                      message: String,
                                      okStr: CharSequence,
                                      noStr: CharSequence,
                                      okListener: QMUIDialogAction.ActionListener,
                                      noListener: QMUIDialogAction.ActionListener?) {

            var cancelListener: QMUIDialogAction.ActionListener? = noListener
            if (null == noListener) {
                cancelListener = QMUIDialogAction.ActionListener { dialog, _ -> dialog.dismiss() }
            }

            QMUIDialog.MessageDialogBuilder(context).setTitle(title).setMessage(message)
                    .setSkinManager(QMUISkinManager.defaultInstance(context))
                    .addAction(noStr, cancelListener).setCanceledOnTouchOutside(false)
                    .setCancelable(false)
                    //QMUIDialogAction.ACTION_PROP_NEGATIVE 字体红色
                    .addAction(0, okStr, QMUIDialogAction.ACTION_PROP_NEGATIVE, okListener)
                    .create(CURRENT_DIALOG_STYLE).show()
        }

        /**
         * 没有标题的
         */
        fun showMessageNegativeDialog(context: Context,
                                      message: String,
                                      okStr: CharSequence,
                                      noStr: CharSequence,
                                      okListener: QMUIDialogAction.ActionListener,
                                      noListener: QMUIDialogAction.ActionListener?) {
            showMessageNegativeDialog(context, "", message, okStr, noStr, okListener, noListener)

        }

        /**
         * 没有标题的
         * 确定按钮文字
         */
        fun showMessageNegativeDialog(context: Context,
                                      message: String,
                                      okListener: QMUIDialogAction.ActionListener,
                                      noListener: QMUIDialogAction.ActionListener?) {
            showMessageNegativeDialog(context, "", message, context.getString(R.string.txt_sure),
                    context.getString(R.string.txt_cancel), okListener, noListener)
        }

        /**
         * 确定按钮文字
         */
        fun showMessageNegativeDialog(context: Context,
                                      title: String,
                                      message: String,
                                      okListener: QMUIDialogAction.ActionListener,
                                      noListener: QMUIDialogAction.ActionListener?) {
            showMessageNegativeDialog(context, title, message, context.getString(R.string.txt_sure),
                    context.getString(R.string.txt_cancel), okListener, noListener)
        }

        /**
         * 显示菜单按钮弹窗
         */
        fun showMenuDialog(context: Context,
                           items: Array<String>,
                           listener: DialogInterface.OnClickListener) {
            QMUIDialog.MenuDialogBuilder(context)
                    .setSkinManager(QMUISkinManager.defaultInstance(context))
                    .addItems(items, listener).create(CURRENT_DIALOG_STYLE).show()
        }

        /**
         * 有标题的输入弹窗
         */
        fun showEditTextDialog(context: Context,
                               title: String,
                               hint: String,
                               content: String,
                               getResultCallback: GetResultCallback,
                               inputType: Int = InputType.TYPE_CLASS_TEXT,
                               maxLength: Int = 0): QMUIDialog.EditTextDialogBuilder {
            val editTextDialogBuilder = QMUIDialog.EditTextDialogBuilder(context)

            editTextDialogBuilder.setTitle(title).setDefaultText(content)
                    .setSkinManager(QMUISkinManager.defaultInstance(context)).setPlaceholder(hint)
                    .setInputType(inputType).addAction(context.getString(
                            R.string.txt_cancel)) { dialog, _ -> dialog.dismiss() }
                    .addAction(context.getString(R.string.txt_sure)) { dialog, index ->
                        dialog.dismiss()
                        getResultCallback.onResult(editTextDialogBuilder.editText.text.toString())
                    }.create(CURRENT_DIALOG_STYLE).show()

            editTextDialogBuilder.editText.apply {
                setSelection(content.length)
                if (maxLength > 0) {
                    //手动设置maxLength为maxLength
                    val filters = arrayOf<InputFilter>(LengthFilter(maxLength))
                    setFilters(filters)
                }
            }
            return editTextDialogBuilder
        }

        /**
         * 没有标题的输入弹窗
         */
        fun showEditTextDialog(context: Context,
                               hint: String,
                               content: String,
                               getResultCallback: GetResultCallback,
                               inputType: Int = InputType.TYPE_CLASS_TEXT,
                               maxLength: Int = 0): QMUIDialog.EditTextDialogBuilder {
            return showEditTextDialog(context, "", hint, content, getResultCallback,
                    inputType = inputType, maxLength = maxLength)
        }


    }
}