package com.kongzue.dialogxdemo.activity;

import static com.kongzue.dialogx.dialogs.PopTip.tip;

import android.animation.ValueAnimator;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.RenderEffect;
import android.graphics.Shader;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowInsets;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.button.MaterialButtonToggleGroup;
import com.kongzue.baseframework.BaseActivity;
import com.kongzue.baseframework.interfaces.DarkNavigationBarTheme;
import com.kongzue.baseframework.interfaces.DarkStatusBarTheme;
import com.kongzue.baseframework.interfaces.Layout;
import com.kongzue.baseframework.interfaces.NavigationBarBackgroundColorRes;
import com.kongzue.baseframework.util.CycleRunner;
import com.kongzue.baseframework.util.JumpParameter;
import com.kongzue.dialogx.DialogX;
import com.kongzue.dialogx.dialogs.BottomDialog;
import com.kongzue.dialogx.dialogs.BottomMenu;
import com.kongzue.dialogx.dialogs.CustomDialog;
import com.kongzue.dialogx.dialogs.FullScreenDialog;
import com.kongzue.dialogx.dialogs.GuideDialog;
import com.kongzue.dialogx.dialogs.InputDialog;
import com.kongzue.dialogx.dialogs.MessageDialog;
import com.kongzue.dialogx.dialogs.MessageMenu;
import com.kongzue.dialogx.dialogs.PopMenu;
import com.kongzue.dialogx.dialogs.PopNotification;
import com.kongzue.dialogx.dialogs.PopTip;
import com.kongzue.dialogx.dialogs.TipDialog;
import com.kongzue.dialogx.dialogs.WaitDialog;
import com.kongzue.dialogx.interfaces.BaseDialog;
import com.kongzue.dialogx.interfaces.BottomDialogSlideEventLifecycleCallback;
import com.kongzue.dialogx.interfaces.DialogLifecycleCallback;
import com.kongzue.dialogx.interfaces.DialogXAnimInterface;
import com.kongzue.dialogx.interfaces.DialogXRunnable;
import com.kongzue.dialogx.interfaces.MenuIconAdapter;
import com.kongzue.dialogx.interfaces.MenuItemTextInfoInterceptor;
import com.kongzue.dialogx.interfaces.OnBackPressedListener;
import com.kongzue.dialogx.interfaces.OnBackgroundMaskClickListener;
import com.kongzue.dialogx.interfaces.OnBindView;
import com.kongzue.dialogx.interfaces.OnBindingView;
import com.kongzue.dialogx.interfaces.OnDialogButtonClickListener;
import com.kongzue.dialogx.interfaces.OnIconChangeCallBack;
import com.kongzue.dialogx.interfaces.OnInputDialogButtonClickListener;
import com.kongzue.dialogx.interfaces.OnMenuButtonClickListener;
import com.kongzue.dialogx.interfaces.OnMenuItemClickListener;
import com.kongzue.dialogx.interfaces.OnMenuItemSelectListener;
import com.kongzue.dialogx.interfaces.OnSafeInsetsChangeListener;
import com.kongzue.dialogx.interfaces.PopMoveDisplacementInterceptor;
import com.kongzue.dialogx.style.IOSStyle;
import com.kongzue.dialogx.style.KongzueStyle;
import com.kongzue.dialogx.style.MIUIStyle;
import com.kongzue.dialogx.style.MaterialStyle;
import com.kongzue.dialogx.util.ItemDivider;
import com.kongzue.dialogx.util.TextInfo;
import com.kongzue.dialogxdemo.BuildConfig;
import com.kongzue.dialogxdemo.R;
import com.kongzue.dialogxdemo.custom.recycleview.CustomRecycleViewAdapter;
import com.kongzue.dialogxdemo.fragment.CustomFragment;
import com.kongzue.dialogxmaterialyou.style.MaterialYouStyle;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Layout(R.layout.activity_main)
@DarkStatusBarTheme(true)
@DarkNavigationBarTheme(true)
@NavigationBarBackgroundColorRes(R.color.emptyNavBar)
public class MainActivity extends BaseActivity {

    private ConstraintLayout boxTitle;
    private TextView txtTitle;
    private ImageView btnShare;
    private ImageView splitBody;
    private LinearLayout boxBody;
    private MaterialButtonToggleGroup grpStyle;
    private MaterialButton rdoMaterial;
    private MaterialButton rdoIos;
    private MaterialButton rdoKongzue;
    private MaterialButton rdoMiui;
    private MaterialButton rdoMaterialYou;
    private MaterialButtonToggleGroup grpTheme;
    private MaterialButton rdoAuto;
    private MaterialButton rdoLight;
    private MaterialButton rdoDark;
    private MaterialButtonToggleGroup grpMode;
    private MaterialButton rdoModeView;
    private MaterialButton rdoModeWindow;
    private MaterialButton rdoModeDialogFragment;
    private MaterialButton rdoModeFloatingActivity;
    private MaterialButton btnMessageDialog;
    private MaterialButton btnSelectDialog;
    private MaterialButton btnInputDialog;
    private MaterialButton btnSelectMessageMenu;
    private MaterialButton btnMutiSelectMessageMenu;
    private MaterialButton btnWaitDialog;
    private MaterialButton btnWaitAndTipDialog;
    private MaterialButton btnTipSuccess;
    private MaterialButton btnTipWarning;
    private MaterialButton btnTipError;
    private MaterialButton btnTipProgress;
    private MaterialButton btnPoptip;
    private MaterialButton btnPoptipBigMessage;
    private MaterialButton btnPoptipSuccess;
    private MaterialButton btnPoptipWarning;
    private MaterialButton btnPoptipError;
    private MaterialButton btnPopnotification;
    private MaterialButton btnPopnotificationBigMessage;
    private MaterialButton btnPopnotificationOverlay;
    private MaterialButton btnBottomDialog;
    private MaterialButton btnBottomMenu;
    private MaterialButton btnBottomReply;
    private MaterialButton btnBottomSelectMenu;
    private MaterialButton btnBottomMultiSelectMenu;
    private MaterialButton btnBottomCustomRecycleView;
    private MaterialButton btnCustomMessageDialog;
    private MaterialButton btnCustomInputDialog;
    private MaterialButton btnCustomBottomMenu;
    private MaterialButton btnCustomDialog;
    private MaterialButton btnCustomDialogAlign;
    private MaterialButton btnFullScreenDialogWebPage;
    private MaterialButton btnFullScreenDialogLogin;
    private MaterialButton btnFullScreenDialogFragment;
    private MaterialButton btnContextMenu;
    private TextView btnSelectMenu;
    private MaterialButton btnShowGuide;
    private MaterialButton btnShowGuideBaseView;
    private MaterialButton btnShowGuideBaseViewRectangle;
    private MaterialButton btnShowBreak;
    private MaterialButton btnListDialog;
    private TextView txtVer;

    @Override
    public void initViews() {
        boxTitle = findViewById(R.id.box_title);
        txtTitle = findViewById(R.id.txt_title);
        btnShare = findViewById(R.id.btn_share);
        splitBody = findViewById(R.id.split_body);
        boxBody = findViewById(R.id.box_body);
        grpStyle = findViewById(R.id.grp_style);
        rdoMaterial = findViewById(R.id.rdo_material);
        rdoIos = findViewById(R.id.rdo_ios);
        rdoKongzue = findViewById(R.id.rdo_kongzue);
        rdoMiui = findViewById(R.id.rdo_miui);
        rdoMaterialYou = findViewById(R.id.rdo_material_you);
        grpTheme = findViewById(R.id.grp_theme);
        rdoAuto = findViewById(R.id.rdo_auto);
        rdoLight = findViewById(R.id.rdo_light);
        rdoDark = findViewById(R.id.rdo_dark);
        grpMode = findViewById(R.id.grp_mode);
        rdoModeView = findViewById(R.id.rdo_mode_view);
        rdoModeWindow = findViewById(R.id.rdo_mode_window);
        rdoModeDialogFragment = findViewById(R.id.rdo_mode_dialogFragment);
        rdoModeFloatingActivity = findViewById(R.id.rdo_mode_floatingActivity);
        btnMessageDialog = findViewById(R.id.btn_messageDialog);
        btnSelectDialog = findViewById(R.id.btn_selectDialog);
        btnInputDialog = findViewById(R.id.btn_inputDialog);
        btnSelectMessageMenu = findViewById(R.id.btn_select_menu);
        btnMutiSelectMessageMenu = findViewById(R.id.btn_multiSelect_menu);
        btnWaitDialog = findViewById(R.id.btn_waitDialog);
        btnWaitAndTipDialog = findViewById(R.id.btn_waitAndTipDialog);
        btnTipSuccess = findViewById(R.id.btn_tipSuccess);
        btnTipWarning = findViewById(R.id.btn_tipWarning);
        btnTipError = findViewById(R.id.btn_tipError);
        btnTipProgress = findViewById(R.id.btn_tipProgress);
        btnPoptip = findViewById(R.id.btn_poptip);
        btnPoptipBigMessage = findViewById(R.id.btn_poptip_bigMessage);
        btnPoptipSuccess = findViewById(R.id.btn_poptip_success);
        btnPoptipWarning = findViewById(R.id.btn_poptip_warning);
        btnPoptipError = findViewById(R.id.btn_poptip_error);
        btnPopnotification = findViewById(R.id.btn_popnotification);
        btnPopnotificationBigMessage = findViewById(R.id.btn_popnotification_bigMessage);
        btnPopnotificationOverlay = findViewById(R.id.btn_popnotification_overlay);
        btnBottomDialog = findViewById(R.id.btn_bottom_dialog);
        btnBottomMenu = findViewById(R.id.btn_bottom_menu);
        btnBottomReply = findViewById(R.id.btn_bottom_reply);
        btnBottomSelectMenu = findViewById(R.id.btn_bottom_select_menu);
        btnBottomMultiSelectMenu = findViewById(R.id.btn_bottom_multiSelect_menu);
        btnBottomCustomRecycleView = findViewById(R.id.btn_bottom_custom_recycleView);
        btnCustomMessageDialog = findViewById(R.id.btn_customMessageDialog);
        btnCustomInputDialog = findViewById(R.id.btn_customInputDialog);
        btnCustomBottomMenu = findViewById(R.id.btn_customBottomMenu);
        btnCustomDialog = findViewById(R.id.btn_customDialog);
        btnCustomDialogAlign = findViewById(R.id.btn_customDialogAlign);
        btnFullScreenDialogWebPage = findViewById(R.id.btn_fullScreenDialog_webPage);
        btnFullScreenDialogLogin = findViewById(R.id.btn_fullScreenDialog_login);
        btnFullScreenDialogFragment = findViewById(R.id.btn_fullScreenDialog_fragment);
        btnContextMenu = findViewById(R.id.btn_contextMenu);
        btnSelectMenu = findViewById(R.id.btn_selectMenu);
        btnShowGuide = findViewById(R.id.btn_showGuide);
        btnShowGuideBaseView = findViewById(R.id.btn_showGuideBaseView);
        btnShowGuideBaseViewRectangle = findViewById(R.id.btn_showGuideBaseViewRectangle);
        btnShowBreak = findViewById(R.id.btn_showBreak);
        btnListDialog = findViewById(R.id.btn_listDialog);
        txtVer = findViewById(R.id.txt_ver);
    }

    @Override
    public void initDatas(JumpParameter parameter) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
            getWindow().getDecorView().setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
                @Override
                public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
                    boxTitle.setPadding(0, insets.getSystemWindowInsetTop(), 0, 0);
                    boxBody.setPadding(dip2px(15), dip2px(15), dip2px(15), insets.getSystemWindowInsetBottom() + dip2px(250));
                    return insets;
                }
            });
        } else {
            getRootView().setFitsSystemWindows(true);
        }

        refreshUIMode();
        boolean showBreak = parameter.getBoolean("showBreak");
        if (showBreak) {
            txtTitle.setText("显示Dialog时关闭Activity演示");
            MessageDialog.show("提示", "接下来会直接运行一个 WaitDialog，2 秒后直接关闭 Activity，并回到原 Activity，保证程序不会出现 WindowLeaked 错误。\n\n" + "Android 原生 AlertDialog 常出现因 Activity 先于 Dialog 关闭而导致此错误引发程序崩溃。\n\n" + "而使用 DialogX 构建的对话框不仅仅不会出现此问题，还可避免因句柄持续持有导致的内存泄漏。", "开始测试", "取消").setOkButton(new OnDialogButtonClickListener() {
                @Override
                public boolean onClick(BaseDialog baseDialog, View v) {
                    WaitDialog.show("请稍后...");
                    runDelayed(new Runnable() {
                        @Override
                        public void run() {
                            finish();       //先结束掉本界面
                        }
                    }, 2000);
                    return false;
                }
            }).setCancelButton(new OnDialogButtonClickListener() {
                @Override
                public boolean onClick(BaseDialog baseDialog, View v) {
                    finish();
                    return false;
                }
            }).setCancelable(false);
        }

        switch (DialogX.implIMPLMode) {
            case VIEW:
                rdoModeView.setChecked(true);
                break;
            case WINDOW:
                rdoModeWindow.setChecked(true);
                break;
            case DIALOG_FRAGMENT:
                rdoModeDialogFragment.setChecked(true);
                break;
            case FLOATING_ACTIVITY:
                rdoModeFloatingActivity.setChecked(true);
                break;
        }

        txtVer.setText("当前版本：" + BuildConfig.VERSION_NAME);

        checkAndroid14InDebugMode();

//        //合并处理演示，在 onDismiss 中获取用户选择进行统一处理，以防止编写大量可能在不同选择下都要处理的重复代码
//        MessageDialog.show("Title", "Ask Question", "OK", "NO", "OTHER").setDialogLifecycleCallback(new DialogLifecycleCallback<MessageDialog>() {
//            @Override
//            public void onDismiss(MessageDialog dialog) {
//                /**
//                 * dialog.getButtonSelectResult() 支持 MessageDialog 和 BottomDialog
//                 * 两种具有选择功能的对话框。
//                 * 包含四种状态：
//                 * NONE,           //未做出选择
//                 * BUTTON_OK,      //选择了确定按钮
//                 * BUTTON_CANCEL,  //选择了取消按钮
//                 * BUTTON_OTHER    //选择了其他按钮
//                 */
//                if (dialog.getButtonSelectResult() == BaseDialog.BUTTON_SELECT_RESULT.BUTTON_OK) {
//                    MessageDialog.show("Title", "You Select OK Button!", "OK");
//                } else {
//                    TipDialog.show("Other Select!", WaitDialog.TYPE.WARNING);
//                }
//            }
//        });

//        //复写事件演示
//        new MessageDialog() {
//            @Override
//            public void onShow(MessageDialog dialog) {
//                //...
//                tip("onShow");
//            }
//
//            @Override
//            public void onDismiss(MessageDialog dialog) {
//                WaitDialog.show("Please Wait...");
//                if (dialog.getButtonSelectResult() == BUTTON_SELECT_RESULT.BUTTON_OK) {
//                    //点击了OK的情况
//                    //...
//                } else {
//                    //其他按钮点击、对话框dismiss的情况
//                    //...
//                }
//                tip("onDismiss");
//            }
//        }
//                .setTitle("Title")
//                .setMessage("message")
//                .setOkButton("OK")
//                .setCancelButton("Cancel")
//                .show();
    }

    //用于模拟进度提示
    private CycleRunner cycleRunner;
    private float progress = 0;
    private int waitId;

    private TextView btnReplyCommit;
    private EditText editReplyCommit;

    private TextView btnCancel;
    private TextView btnSubmit;
    private RelativeLayout boxUserName;
    private EditText editUserName;
    private RelativeLayout boxPassword;
    private EditText editPassword;
    private TextView btnLicense;

    private TextView btnClose;
    private WebView webView;

    private String[] singleSelectMenuText = new String[]{"拒绝", "询问", "始终允许", "仅在使用中允许"};
    private int selectMenuIndex;

    private String[] multiSelectMenuText = new String[]{"上海", "北京", "广州", "深圳"};
    private int[] selectMenuIndexArray;
    private String multiSelectMenuResultCache;

    @Override
    public void setEvents() {
        grpMode.addOnButtonCheckedListener(new MaterialButtonToggleGroup.OnButtonCheckedListener() {
            @Override
            public void onButtonChecked(MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {
                BaseDialog.cleanAll();
                switch (checkedId) {
                    case R.id.rdo_mode_view:
                        DialogX.implIMPLMode = DialogX.IMPL_MODE.VIEW;
                        break;
                    case R.id.rdo_mode_window:
                        DialogX.implIMPLMode = DialogX.IMPL_MODE.WINDOW;
                        break;
                    case R.id.rdo_mode_dialogFragment:
                        DialogX.implIMPLMode = DialogX.IMPL_MODE.DIALOG_FRAGMENT;
                        break;
                    case R.id.rdo_mode_floatingActivity:
                        DialogX.implIMPLMode = DialogX.IMPL_MODE.FLOATING_ACTIVITY;
                        break;
                }
            }
        });

        grpTheme.addOnButtonCheckedListener(new MaterialButtonToggleGroup.OnButtonCheckedListener() {
            @Override
            public void onButtonChecked(MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {
                switch (checkedId) {
                    case R.id.rdo_auto:
                        DialogX.globalTheme = DialogX.THEME.AUTO;
                        break;
                    case R.id.rdo_light:
                        DialogX.globalTheme = DialogX.THEME.LIGHT;
                        break;
                    case R.id.rdo_dark:
                        DialogX.globalTheme = DialogX.THEME.DARK;
                        break;
                }
            }
        });

        grpStyle.addOnButtonCheckedListener(new MaterialButtonToggleGroup.OnButtonCheckedListener() {
            @Override
            public void onButtonChecked(MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {
                DialogX.cancelButtonText = "取消";
                DialogX.titleTextInfo = null;
                DialogX.buttonTextInfo = null;
                switch (checkedId) {
                    case R.id.rdo_material:
                        DialogX.globalStyle = MaterialStyle.style();
                        DialogX.cancelButtonText = "";
                        break;
                    case R.id.rdo_kongzue:
                        DialogX.globalStyle = KongzueStyle.style();
                        break;
                    case R.id.rdo_ios:
                        DialogX.globalStyle = IOSStyle.style();
                        break;
                    case R.id.rdo_miui:
                        DialogX.globalStyle = MIUIStyle.style();
                        break;
                    case R.id.rdo_material_you:
                        DialogX.globalStyle = MaterialYouStyle.style();
                        break;
                }
            }
        });

        btnShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openUrl("https://github.com/kongzue/DialogX");
            }
        });

        btnContextMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopMenu.show("添加", "编辑", "删除", "分享")
                        .disableMenu("编辑", "删除")
                        .setIconResIds(R.mipmap.img_dialogx_demo_add, R.mipmap.img_dialogx_demo_edit, R.mipmap.img_dialogx_demo_delete, R.mipmap.img_dialogx_demo_share)
                        .setOnMenuItemClickListener(new OnMenuItemClickListener<PopMenu>() {
                            @Override
                            public boolean onClick(PopMenu dialog, CharSequence text, int index) {
                                if (index == 0) {
                                    dialog.enableAllMenu();
                                    dialog.setMenuList(new String[]{"产品A", "产品B", "产品C"});
                                    return true;
                                }
                                return false;
                            }
                        });
            }
        });

        btnSelectMenu.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                PopMenu.show(view, new String[]{"选项1", "选项2", "选项3"})
                        .setMenuTextInfo(new TextInfo().setGravity(Gravity.CENTER))
                        .setOnMenuItemClickListener(new OnMenuItemClickListener<PopMenu>() {
                            @Override
                            public boolean onClick(PopMenu dialog, CharSequence text, int index) {
                                btnSelectMenu.setText(text);
                                return false;
                            }
                        }).setItemDivider(new ItemDivider(15, 15, 1));
            }
        });

        btnFullScreenDialogFragment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CustomFragment customFragment = new CustomFragment().setAddButtonClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        btnFullScreenDialogFragment.callOnClick();
                    }
                });
                FullScreenDialog.build(new OnBindView<FullScreenDialog>(customFragment) {
                    @Override
                    public void onBind(FullScreenDialog dialog, View v) {

                    }
                }).hideActivityContentView(true).show();
            }
        });

        btnMessageDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MessageDialog.show("标题", "这里是正文内容。", "确定")
                        .setDialogLifecycleCallback(new DialogLifecycleCallback() {
                            @Override
                            public void onShow(BaseDialog dialog) {
                                tip("onShow");
                                super.onShow(dialog);
                            }

                            @Override
                            public void onDismiss(BaseDialog dialog) {
                                tip("onDismiss");
                                super.onDismiss(dialog);
                            }
                        })
//                        .onShow(new DialogXRunnable<MessageDialog>() {
//                            @Override
//                            public void run(MessageDialog dialog) {
//                                tip("onShow");
//                            }
//                        }).onDismiss(new DialogXRunnable<MessageDialog>() {
//                            @Override
//                            public void run(MessageDialog dialog) {
//                                tip("onDismiss");
//                            }
//                        })
                        .setTitleIcon(R.mipmap.img_demo_avatar)
                        .setOkButton(new OnDialogButtonClickListener<MessageDialog>() {
                            @Override
                            public boolean onClick(MessageDialog dialog, View v) {
                                PopTip.show("点击确定按钮");
                                return true;
                            }
                        });
            }
        });

        btnSelectDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MessageDialog messageDialog = new MessageDialog("多选对话框", "移除App会将它从主屏幕移除并保留其所有数据。", "删除App", "取消", "移至App资源库").setButtonOrientation(LinearLayout.VERTICAL);
                if (!rdoMiui.isChecked()) {
                    messageDialog.setOkTextInfo(new TextInfo().setFontColor(Color.parseColor("#EB5545")).setBold(true));
                }
                messageDialog.show();
            }
        });

        btnInputDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new InputDialog("标题", "正文内容", "确定", "取消", "正在输入的文字").setInputText("Hello World").setOkButton(new OnInputDialogButtonClickListener<InputDialog>() {
                    @Override
                    public boolean onClick(InputDialog baseDialog, View v, String inputStr) {
                        PopTip.show("输入的内容：" + inputStr);
                        return false;
                    }
                }).show();
            }
        });

        btnSelectMessageMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MessageMenu.show(singleSelectMenuText).setShowSelectedBackgroundTips(rdoMiui.isChecked()).setMessage("这里是权限确认的文本说明，这是一个演示菜单").setTitle("获得权限标题").setOnMenuItemClickListener(new OnMenuItemSelectListener<MessageMenu>() {
                    @Override
                    public void onOneItemSelect(MessageMenu dialog, CharSequence text, int index, boolean select) {
                        selectMenuIndex = index;
                    }
                }).setCancelButton("确定", new OnMenuButtonClickListener<MessageMenu>() {
                    @Override
                    public boolean onClick(MessageMenu baseDialog, View v) {
                        PopTip.show("已选择：" + singleSelectMenuText[selectMenuIndex]);
                        return false;
                    }
                }).setSelection(selectMenuIndex);
            }
        });
        btnMutiSelectMessageMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MessageMenu.show(multiSelectMenuText).setMessage("这里是选择城市的模拟范例，这是一个演示菜单").setTitle("请选择城市").setOnMenuItemClickListener(new OnMenuItemSelectListener<MessageMenu>() {
                            @Override
                            public void onMultiItemSelect(MessageMenu dialog, CharSequence[] text, int[] index) {
                                multiSelectMenuResultCache = "";
                                for (CharSequence c : text) {
                                    multiSelectMenuResultCache = multiSelectMenuResultCache + " " + c;
                                }
                                selectMenuIndexArray = index;
                            }
                        }).setOkButton("确定", new OnMenuButtonClickListener<MessageMenu>() {
                            @Override
                            public boolean onClick(MessageMenu dialog, View v) {
                                PopTip.show("已选择：" + multiSelectMenuResultCache);
                                return false;
                            }
                        })
//                        .setCancelButton("确定", new OnDialogButtonClickListener<MessageDialog>() {
//                            @Override
//                            public boolean onClick(MessageDialog baseDialog, View v) {
//                                PopTip.show("已选择：" + multiSelectMenuResultCache);
//                                return false;
//                            }
//                        })
                        .setSelection(selectMenuIndexArray);
            }
        });

        btnWaitDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WaitDialog.show("Please Wait!").setOnBackPressedListener(new OnBackPressedListener<WaitDialog>() {
                    @Override
                    public boolean onBackPressed(WaitDialog dialog) {
                        PopTip.show("按下返回").setButton("取消", new OnDialogButtonClickListener<PopTip>() {
                            @Override
                            public boolean onClick(PopTip dialog, View v) {
                                WaitDialog.dismiss();
                                return false;
                            }
                        });
                        return false;
                    }
                });
                WaitDialog.dismiss(3000);
            }
        });

        btnWaitAndTipDialog.setOnClickListener(new View.OnClickListener() {

            boolean closeFlag = false;

            @Override
            public void onClick(View v) {
                closeFlag = false;
                WaitDialog.show("Please Wait!").setOnBackPressedListener(new OnBackPressedListener<WaitDialog>() {
                    @Override
                    public boolean onBackPressed(WaitDialog dialog) {
                        PopTip.show("按下返回", "关闭").setButton(new OnDialogButtonClickListener<PopTip>() {
                            @Override
                            public boolean onClick(PopTip baseDialog, View v) {
                                closeFlag = true;
                                WaitDialog.dismiss();
                                return false;
                            }
                        });
                        return false;
                    }
                }).setMinWidth(dip2px(200));
                if (!closeFlag) runDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (!closeFlag) TipDialog.show("完成！", WaitDialog.TYPE.SUCCESS);
                    }
                }, 1500 + new Random().nextInt(1000));
            }
        });

        btnTipSuccess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TipDialog.show("Success!", WaitDialog.TYPE.SUCCESS);
            }
        });

        btnTipWarning.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TipDialog.show("Warning!", WaitDialog.TYPE.WARNING);
            }
        });

        btnTipError.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TipDialog.show("Error!", WaitDialog.TYPE.ERROR);
            }
        });

        btnTipProgress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                waitId = 0;
                progress = 0;
                WaitDialog.show("假装连接...").setOnBackPressedListener(new OnBackPressedListener<WaitDialog>() {
                    @Override
                    public boolean onBackPressed(WaitDialog dialog) {
                        MessageDialog.show("正在进行", "是否取消？", "是", "否").setOkButton(new OnDialogButtonClickListener() {
                            @Override
                            public boolean onClick(BaseDialog baseDialog, View v) {
                                waitId = -1;
                                if (cycleRunner != null) {
