package com.kongzue.dialogx.dialogs;

import static android.view.View.OVER_SCROLL_NEVER;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.lifecycle.LifecycleOwner;

import com.kongzue.dialogx.DialogX;
import com.kongzue.dialogx.R;
import com.kongzue.dialogx.interfaces.BottomMenuListViewTouchEvent;
import com.kongzue.dialogx.interfaces.DialogLifecycleCallback;
import com.kongzue.dialogx.interfaces.DialogXAnimInterface;
import com.kongzue.dialogx.interfaces.DialogXRunnable;
import com.kongzue.dialogx.interfaces.DialogXStyle;
import com.kongzue.dialogx.interfaces.MenuItemLayoutRefreshCallback;
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.OnMenuButtonClickListener;
import com.kongzue.dialogx.interfaces.OnDialogButtonClickListener;
import com.kongzue.dialogx.interfaces.OnIconChangeCallBack;
import com.kongzue.dialogx.interfaces.OnMenuItemClickListener;
import com.kongzue.dialogx.interfaces.OnMenuItemSelectListener;
import com.kongzue.dialogx.interfaces.SELECT_MODE;
import com.kongzue.dialogx.util.BottomMenuArrayAdapter;
import com.kongzue.dialogx.util.ItemDivider;
import com.kongzue.dialogx.util.MessageMenuArrayAdapter;
import com.kongzue.dialogx.util.TextInfo;
import com.kongzue.dialogx.util.views.DialogListView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MessageMenu extends MessageDialog {

    protected MessageMenu me = this;
    protected boolean allowInterceptTouch = true;
    protected int selectionIndex = -1;
    protected SELECT_MODE selectMode = SELECT_MODE.NONE;
    protected ArrayList<Integer> selectionItems;

    protected boolean showSelectedBackgroundTips = false;
    protected MenuItemLayoutRefreshCallback<MessageMenu> menuMenuItemLayoutRefreshCallback;
    protected Map<Integer, Boolean> menuUsability = new HashMap<Integer, Boolean>();
    protected ItemDivider itemDivider;

    protected OnMenuItemClickListener<MessageMenu> onMenuItemClickListener;

    public static MessageMenu build() {
        return new MessageMenu();
    }

    public static MessageMenu build(DialogXStyle style) {
        return new MessageMenu().setStyle(style);
    }

    public static MessageMenu build(OnBindView<MessageDialog> onBindView) {
        return new MessageMenu().setCustomView(onBindView);
    }

    protected MessageMenu() {
        super();
    }

    protected OnIconChangeCallBack<MessageMenu> onIconChangeCallBack;
    protected MenuItemTextInfoInterceptor<MessageMenu> menuItemTextInfoInterceptor;
    protected DialogListView listView;
    protected TextInfo menuTextInfo;
    protected BaseAdapter menuListAdapter;
    protected List<CharSequence> menuList;
    protected List<Integer> iconResIds;
    protected boolean autoTintIconInLightOrDarkMode = true;

    public static MessageMenu show(List<CharSequence> menuList) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.setMenuList(menuList);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(List<CharSequence> menuList, OnMenuItemClickListener<MessageMenu> onMenuItemClickListener) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.setMenuList(menuList);
        messageMenu.setOnMenuItemClickListener(onMenuItemClickListener);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu showStringList(List<String> menuList) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.setMenuStringList(menuList);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu showStringList(List<String> menuList, OnMenuItemClickListener<MessageMenu> onMenuItemClickListener) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.setMenuStringList(menuList);
        messageMenu.setOnMenuItemClickListener(onMenuItemClickListener);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(String... menuList) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.setMenuList(menuList);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(String[] menuList, OnMenuItemClickListener<MessageMenu> onMenuItemClickListener) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.setMenuList(menuList);
        messageMenu.setOnMenuItemClickListener(onMenuItemClickListener);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(CharSequence[] menuList) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.setMenuList(menuList);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(CharSequence[] menuList, OnMenuItemClickListener<MessageMenu> onMenuItemClickListener) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.setMenuList(menuList);
        messageMenu.setOnMenuItemClickListener(onMenuItemClickListener);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(CharSequence title, CharSequence message, List<CharSequence> menuList) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = title;
        messageMenu.message = message;
        messageMenu.setMenuList(menuList);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(CharSequence title, CharSequence message, List<CharSequence> menuList, OnMenuItemClickListener<MessageMenu> onMenuItemClickListener) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = title;
        messageMenu.message = message;
        messageMenu.setMenuList(menuList);
        messageMenu.setOnMenuItemClickListener(onMenuItemClickListener);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(CharSequence title, List<CharSequence> menuList) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = title;
        messageMenu.setMenuList(menuList);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(CharSequence title, List<CharSequence> menuList, OnMenuItemClickListener<MessageMenu> onMenuItemClickListener) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = title;
        messageMenu.setMenuList(menuList);
        messageMenu.setOnMenuItemClickListener(onMenuItemClickListener);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu showStringList(CharSequence title, CharSequence message, List<String> menuList) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = title;
        messageMenu.message = message;
        messageMenu.setMenuStringList(menuList);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu showStringList(CharSequence title, CharSequence message, List<String> menuList, OnMenuItemClickListener<MessageMenu> onMenuItemClickListener) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = title;
        messageMenu.message = message;
        messageMenu.setMenuStringList(menuList);
        messageMenu.setOnMenuItemClickListener(onMenuItemClickListener);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(CharSequence title, CharSequence message, String[] menuList) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = title;
        messageMenu.message = message;
        messageMenu.setMenuList(menuList);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(CharSequence title, CharSequence message, String[] menuList, OnMenuItemClickListener<MessageMenu> onMenuItemClickListener) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = title;
        messageMenu.message = message;
        messageMenu.setMenuList(menuList);
        messageMenu.setOnMenuItemClickListener(onMenuItemClickListener);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(CharSequence title, CharSequence message, CharSequence[] menuList) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = title;
        messageMenu.message = message;
        messageMenu.setMenuList(menuList);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(CharSequence title, CharSequence message, CharSequence[] menuList, OnMenuItemClickListener<MessageMenu> onMenuItemClickListener) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = title;
        messageMenu.message = message;
        messageMenu.setMenuList(menuList);
        messageMenu.setOnMenuItemClickListener(onMenuItemClickListener);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(String title, String message, List<CharSequence> menuList) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = title;
        messageMenu.message = message;
        messageMenu.setMenuList(menuList);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(String title, String message, List<CharSequence> menuList, OnMenuItemClickListener<MessageMenu> onMenuItemClickListener) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = title;
        messageMenu.message = message;
        messageMenu.setMenuList(menuList);
        messageMenu.setOnMenuItemClickListener(onMenuItemClickListener);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu showStringList(String title, String message, List<String> menuList) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = title;
        messageMenu.message = message;
        messageMenu.setMenuStringList(menuList);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu showStringList(String title, String message, List<String> menuList, OnMenuItemClickListener<MessageMenu> onMenuItemClickListener) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = title;
        messageMenu.message = message;
        messageMenu.setMenuStringList(menuList);
        messageMenu.setOnMenuItemClickListener(onMenuItemClickListener);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(String title, String message, String[] menuList) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = title;
        messageMenu.message = message;
        messageMenu.setMenuList(menuList);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(String title, String message, String[] menuList, OnMenuItemClickListener<MessageMenu> onMenuItemClickListener) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = title;
        messageMenu.message = message;
        messageMenu.setMenuList(menuList);
        messageMenu.setOnMenuItemClickListener(onMenuItemClickListener);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(String title, String message, CharSequence[] menuList) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = title;
        messageMenu.message = message;
        messageMenu.setMenuList(menuList);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(String title, String message, CharSequence[] menuList, OnMenuItemClickListener<MessageMenu> onMenuItemClickListener) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = title;
        messageMenu.message = message;
        messageMenu.setMenuList(menuList);
        messageMenu.setOnMenuItemClickListener(onMenuItemClickListener);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(int titleResId, int messageResId, List<CharSequence> menuList) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = messageMenu.getString(titleResId);
        messageMenu.message = messageMenu.getString(messageResId);
        messageMenu.setMenuList(menuList);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(int titleResId, List<CharSequence> menuList) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = messageMenu.getString(titleResId);
        messageMenu.setMenuList(menuList);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu showStringList(int titleResId, int messageResId, List<String> menuList) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = messageMenu.getString(titleResId);
        messageMenu.message = messageMenu.getString(messageResId);
        messageMenu.setMenuStringList(menuList);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(int titleResId, int messageResId, String[] menuList) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = messageMenu.getString(titleResId);
        messageMenu.message = messageMenu.getString(messageResId);
        messageMenu.setMenuList(menuList);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(int titleResId, int messageResId, CharSequence[] menuList) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = messageMenu.getString(titleResId);
        messageMenu.message = messageMenu.getString(messageResId);
        messageMenu.setMenuList(menuList);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(int titleResId, int messageResId, List<CharSequence> menuList, OnMenuItemClickListener<MessageMenu> onMenuItemClickListener) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = messageMenu.getString(titleResId);
        messageMenu.message = messageMenu.getString(messageResId);
        messageMenu.setOnMenuItemClickListener(onMenuItemClickListener);
        messageMenu.setMenuList(menuList);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(int titleResId, List<CharSequence> menuList, OnMenuItemClickListener<MessageMenu> onMenuItemClickListener) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = messageMenu.getString(titleResId);
        messageMenu.setOnMenuItemClickListener(onMenuItemClickListener);
        messageMenu.setMenuList(menuList);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu showStringList(int titleResId, int messageResId, List<String> menuList, OnMenuItemClickListener<MessageMenu> onMenuItemClickListener) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = messageMenu.getString(titleResId);
        messageMenu.message = messageMenu.getString(messageResId);
        messageMenu.setMenuStringList(menuList);
        messageMenu.setOnMenuItemClickListener(onMenuItemClickListener);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(int titleResId, int messageResId, String[] menuList, OnMenuItemClickListener<MessageMenu> onMenuItemClickListener) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = messageMenu.getString(titleResId);
        messageMenu.message = messageMenu.getString(messageResId);
        messageMenu.setMenuList(menuList);
        messageMenu.setOnMenuItemClickListener(onMenuItemClickListener);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(int titleResId, int messageResId, CharSequence[] menuList, OnMenuItemClickListener<MessageMenu> onMenuItemClickListener) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = messageMenu.getString(titleResId);
        messageMenu.message = messageMenu.getString(messageResId);
        messageMenu.setMenuList(menuList);
        messageMenu.setOnMenuItemClickListener(onMenuItemClickListener);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(CharSequence title, CharSequence[] menuList) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = title;
        messageMenu.setMenuList(menuList);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(CharSequence title, String[] menuList) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = title;
        messageMenu.setMenuList(menuList);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(CharSequence title, CharSequence[] menuList, OnMenuItemClickListener<MessageMenu> onMenuItemClickListener) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = title;
        messageMenu.setMenuList(menuList);
        messageMenu.setOnMenuItemClickListener(onMenuItemClickListener);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(CharSequence title, String[] menuList, OnMenuItemClickListener<MessageMenu> onMenuItemClickListener) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = title;
        messageMenu.setMenuList(menuList);
        messageMenu.setOnMenuItemClickListener(onMenuItemClickListener);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(int titleResId, CharSequence[] menuList) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = messageMenu.getString(titleResId);
        messageMenu.setMenuList(menuList);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(int titleResId, String[] menuList) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = messageMenu.getString(titleResId);
        messageMenu.setMenuList(menuList);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(int titleResId, CharSequence[] menuList, OnMenuItemClickListener<MessageMenu> onMenuItemClickListener) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = messageMenu.getString(titleResId);
        messageMenu.setMenuList(menuList);
        messageMenu.setOnMenuItemClickListener(onMenuItemClickListener);
        messageMenu.show();
        return messageMenu;
    }

    public static MessageMenu show(int titleResId, String[] menuList, OnMenuItemClickListener<MessageMenu> onMenuItemClickListener) {
        MessageMenu messageMenu = new MessageMenu();
        messageMenu.title = messageMenu.getString(titleResId);
        messageMenu.setMenuList(menuList);
        messageMenu.setOnMenuItemClickListener(onMenuItemClickListener);
        messageMenu.show();
        return messageMenu;
    }


    private float touchDownY;

    public static final int ITEM_CLICK_DELAY = 100;
    private long lastClickTime = 0;
    private int[] resultArray;
    private CharSequence[] selectTextArray;

    @Override
    protected void onDialogShow() {
        if (getDialogImpl() != null && getDialogImpl().boxList != null) {
            getDialogImpl().boxList.setVisibility(View.VISIBLE);

            if (!isAllowInterceptTouch()) {
                getDialogImpl().bkg.setMaxHeight(maxHeight);
                if (maxHeight != 0) {
                    dialogImpl.scrollView.lockScroll(true);
                }
            }

            int dividerDrawableResId = 0;
            int dividerHeight = 1;
            if (style.overrideBottomDialogRes() != null) {
                dividerDrawableResId = style.overrideBottomDialogRes().overrideMenuDividerDrawableRes(isLightTheme());
                dividerHeight = style.overrideBottomDialogRes().overrideMenuDividerHeight(isLightTheme());
            }
            if (dividerDrawableResId == 0) {
                dividerDrawableResId = isLightTheme() ? R.drawable.rect_dialogx_material_menu_split_divider : R.drawable.rect_dialogx_material_menu_split_divider_night;
            }

            if (!isLightTheme()) {
                listView = new DialogListView(getDialogImpl(), getOwnActivity(), R.style.DialogXCompatThemeDark);
            } else {
                listView = new DialogListView(getDialogImpl(), getOwnActivity());
            }
            listView.setOverScrollMode(OVER_SCROLL_NEVER);
            listView.setDivider(getResources().getDrawable(dividerDrawableResId));
            listView.setDividerHeight(dividerHeight);

            listView.setBottomMenuListViewTouchEvent(new BottomMenuListViewTouchEvent() {
                @Override
                public void down(MotionEvent event) {
                    touchDownY = getDialogImpl().bkg.getY();
                }
            });

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if (!isMenuItemEnable(position)) {
                        return;
                    }
                    haptic(view);
                    long currentTime = System.currentTimeMillis();
                    if (currentTime - lastClickTime > ITEM_CLICK_DELAY) {
                        lastClickTime = currentTime;
                        float deltaY = Math.abs(touchDownY - getDialogImpl().bkg.getY());
                        if (deltaY > dip2px(15)) {
                            return;
                        }
                        selectionIndex = position;
                        switch (selectMode) {
                            case NONE:
                                if (onMenuItemClickListener != null) {
                                    if (!onMenuItemClickListener.onClick(me, menuList.get(position), position)) {
                                        dismiss();
                                    }
                                } else {
                                    dismiss();
                                }
                                break;
                            case SINGLE:
                                if (onMenuItemClickListener instanceof OnMenuItemSelectListener) {
                                    OnMenuItemSelectListener<MessageMenu> onMenuItemSelectListener = (OnMenuItemSelectListener<MessageMenu>) onMenuItemClickListener;
                                    if (!onMenuItemSelectListener.onClick(me, menuList.get(position), position)) {
                                        dismiss();
                                    } else {
                                        menuListAdapter.notifyDataSetInvalidated();
                                        onMenuItemSelectListener.onOneItemSelect(me, menuList.get(position), position, true);
                                    }
                                } else {
                                    if (onMenuItemClickListener != null) {
                                        if (!onMenuItemClickListener.onClick(me, menuList.get(position), position)) {
                                            dismiss();
                                        }
                                    } else {
                                        menuListAdapter.notifyDataSetInvalidated();
                                    }
                                }
                                break;
                            case MULTIPLE:
                                if (onMenuItemClickListener instanceof OnMenuItemSelectListener) {
                                    OnMenuItemSelectListener<MessageMenu> onMenuItemSelectListener = (OnMenuItemSelectListener<MessageMenu>) onMenuItemClickListener;
                                    if (!onMenuItemSelectListener.onClick(me, menuList.get(position), position)) {
                                        dismiss();
                                    } else {
                                        if (selectionItems.contains(position)) {
                                            selectionItems.remove(new Integer(position));
                                        } else {
                                            selectionItems.add(position);
                                        }
                                        menuListAdapter.notifyDataSetInvalidated();
                                        resultArray = new int[selectionItems.size()];
                                        selectTextArray = new CharSequence[selectionItems.size()];
                                        for (int i = 0; i < selectionItems.size(); i++) {
                                            resultArray[i] = selectionItems.get(i);
                                            selectTextArray[i] = menuList.get(resultArray[i]);
                                        }
                                        onMenuItemSelectListener.onMultiItemSelect(me, selectTextArray, resultArray);
                                    }
                                } else {
                                    if (onMenuItemClickListener != null) {
                                        if (!onMenuItemClickListener.onClick(me, menuList.get(position), position)) {
                                            dismiss();
                                        }
                                    } else {
                                        if (selectionItems.contains(position)) {
                                            selectionItems.remove(new Integer(position));
                                        } else {
                                            selectionItems.add(position);
                                        }
                                        menuListAdapter.notifyDataSetInvalidated();
                                        resultArray = new int[selectionItems.size()];
                                        selectTextArray = new CharSequence[selectionItems.size()];
                                        for (int i = 0; i < selectionItems.size(); i++) {
                                            resultArray[i] = selectionItems.get(i);
                                            selectTextArray[i] = menuList.get(resultArray[i]);
                                        }
                                    }
                                }
                                break;
                        }
                    }
                }
            });
            if (style.overrideBottomDialogRes() != null) {
                if (style.overrideBottomDialogRes().overrideMenuItemLayout(true, 0, 0, false) != 0) {
                    listView.setSelector(R.color.empty);
                }
            }

            ViewGroup.LayoutParams listViewLp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            getDialogImpl().boxList.addView(listView, listViewLp);
            refreshUI();
        }
    }

    @Override
    public void refreshUI() {
        if (getDialogImpl() == null) return;
        if (listView != null) {
            if (menuListAdapter == null) {
                menuListAdapter = new MessageMenuArrayAdapter(me, getOwnActivity(), menuList);
            }
            if (listView.getAdapter() == null) {
                listView.setAdapter(menuListAdapter);
            } else {
                if (listView.getAdapter() != menuListAdapter) {
                    listView.setAdapter(menuListAdapter);
                } else {
                    menuListAdapter.notifyDataSetChanged();
                }
            }
        }

        // 部分主题下选中项默认按下效果
        if (showSelectedBackgroundTips) {
            if (listView != null) {
                listView.post(new Runnable() {
                    @Override
                    public void run() {
                        if (menuListAdapter instanceof BottomMenuArrayAdapter && showSelectedBackgroundTips) {
                            BottomMenuArrayAdapter bottomMenuArrayAdapter = ((BottomMenuArrayAdapter) menuListAdapter);

                            View selectItemView = listView.getChildAt(getSelection());
                            if (selectItemView != null) {
                                selectItemView.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        selectItemView.setPressed(true);
                                    }
                                });
                            }
                        }
                    }
                });
            }

        }

        if (itemDivider != null && listView != null) {
            listView.setDivider(itemDivider.createDividerDrawable(getOwnActivity(), isLightTheme()));
            listView.setDividerHeight(itemDivider.getWidth());
        }
        super.refreshUI();
    }

    public void preRefreshUI() {
        if (getDialogImpl() == null) return;
        runOnMain(new Runnable() {
            @Override
            public void run() {
                refreshUI();
            }
        });
    }


    @Override
    public String dialogKey() {
        return getClass().getSimpleName() + "(" + Integer.toHexString(hashCode()) + ")";
    }

    public List<CharSequence> getMenuList() {
        return menuList;
    }

    public MessageMenu setMenuList(List<CharSequence> menuList) {
        this.menuList = menuList;
        this.menuListAdapter = null;
        preRefreshUI();
        return this;
    }

    private boolean isSameSize(int menuListSize) {
        if (this.menuList == null || this.menuList.size() == 0) {
            return true;
        }
        return this.menuList.size() == menuListSize;
    }

    public MessageMenu setMenuStringList(List<String> menuList) {
        this.menuList = new ArrayList<>();
        this.menuList.addAll(menuList);
        this.menuListAdapter = null;
        preRefreshUI();
        return this;
    }

    public MessageMenu setMenuList(String[] menuList) {
        this.menuList = new ArrayList<>();
        this.menuList.addAll(Arrays.asList(menuList));
        this.menuListAdapter = null;
        preRefreshUI();
        return this;
    }

    public MessageMenu setMenuList(CharSequence[] menuList) {
        this.menuList = Arrays.asList(menuList);
        this.menuListAdapter = null;
        preRefreshUI();
        return this;
    }

    public MessageMenu setMenus(CharSequence... menuList) {
        this.menuList = Arrays.asList(menuList);
        this.menuListAdapter = null;
        preRefreshUI();
        return this;
    }

    public MessageMenu setMenus(String... menuList) {
        this.menuList = Arrays.asList(menuList);
        this.menuListAdapter = null;
        preRefreshUI();
        return this;
    }

    public OnIconChangeCallBack<MessageMenu> getOnIconChangeCallBack() {
        return onIconChangeCallBack;
    }

    public MessageMenu setOnIconChangeCallBack(OnIconChangeCallBack<MessageMenu> onIconChangeCallBack) {
        this.onIconChangeCallBack = onIconChangeCallBack;
        return this;
    }

    public OnBackPressedListener<MessageDialog> getOnBackPressedListener() {
        return (OnBackPressedListener<MessageDialog>) onBackPressedListener;
    }

