package com.wisewoods.base.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.ProgressBar;

import androidx.fragment.app.DialogFragment;

import com.afollestad.materialdialogs.LayoutMode;
import com.afollestad.materialdialogs.MaterialDialog;
import com.afollestad.materialdialogs.bottomsheets.BottomSheet;
import com.afollestad.materialdialogs.list.DialogListExtKt;
import com.blankj.utilcode.util.ActivityUtils;

import java.util.List;

import kotlin.Unit;
import kotlin.jvm.functions.Function3;

public class DialogUtils {

    public static final int CANCEL = 0;
    public static final int OK = 1;

    public static interface IDialogOnOKListener extends DialogInterface.OnClickListener {
        void clickOk(DialogInterface dialog);

        default void onClick(DialogInterface dialog, int which) {
            if (which == DialogUtils.OK) {
                clickOk(dialog);
            }
        }
    }

    public static Dialog showMD(Context context, String message) {
        if (context == null){
            context = ActivityUtils.getTopActivity();
        }
        if (message == null) {
            message = "正在加载中";
        }
        MaterialDialog dialog = new MaterialDialog(context, MaterialDialog.getDEFAULT_BEHAVIOR());
        dialog.cancelable(false);
        dialog.setContentView(new ProgressBar(ActivityUtils.getTopActivity()));
        dialog.message(0, message, null);
        dialog.show();
        return dialog;

    }

    public static Dialog showMD() {
        return showMD(null, null);
    }

    public static Dialog showCommon(String message, final DialogInterface.OnClickListener listener) {
        return showCommon(null, message, null, null, listener);
    }

    public static Dialog showTip(String title, String message, final DialogInterface.OnClickListener listener) {
        return showCommon(title, message, "", "", listener);
    }

    public static Dialog showOkTip(String message, final DialogInterface.OnClickListener listener) {
        return showCommon(null, message, null, "", listener);
    }

    public static Dialog showOkTip(String title, String message, final DialogInterface.OnClickListener listener) {
        return showCommon(title, message, null, "", listener);
    }

    public static Dialog showCommon(String title, String message, String ok, String cancel,
                                    final DialogInterface.OnClickListener listener) {
        if (title == null) {
            title = "提示";
        }
        if (ok == null) {
            ok = "确认";
        }
        if (cancel == null) {
            cancel = "取消";
        }
        MaterialDialog dialog = new MaterialDialog(ActivityUtils.getTopActivity(), MaterialDialog.getDEFAULT_BEHAVIOR());
        dialog.title(0, title);
        dialog.message(0, message, null);
        if (!"".equals(cancel)) {
            dialog.negativeButton(0, cancel, materialDialog -> {
                if (listener != null) {
                    listener.onClick(materialDialog, CANCEL);
                }
                return null;
            });
        }
        if (!"".equals(ok)) {
            dialog.positiveButton(0, ok, materialDialog -> {
                if (listener != null) {
                    listener.onClick(materialDialog, OK);
                }
                return null;
            });
        }
        dialog.show();
        return dialog;
    }

    public static Dialog showList(String title, List<CharSequence> items, Function3<? super MaterialDialog, ? super Integer,
            ? super CharSequence, Unit> listener) {
        BottomSheet bottomSheet = new BottomSheet(LayoutMode.WRAP_CONTENT);
        MaterialDialog dialog = new MaterialDialog(ActivityUtils.getTopActivity(), bottomSheet);
        dialog.title(0, title);
        DialogListExtKt.listItems(dialog, null, items, null, true, listener);
        dialog.show();
        return dialog;
    }

    public static void dissmiss(Dialog dialog) {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public static void dissmiss(DialogFragment dialog) {
        if (dialog != null && dialog.getDialog() != null
                && dialog.getDialog().isShowing()) {
            dialog.dismiss();
        }
    }


}
