package com.kongzue.dialogx.dialogs;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Outline;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

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

import com.kongzue.dialogx.DialogX;
import com.kongzue.dialogx.R;
import com.kongzue.dialogx.interfaces.BaseDialog;
import com.kongzue.dialogx.interfaces.BlurViewType;
import com.kongzue.dialogx.interfaces.DialogConvertViewInterface;
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.NoTouchInterface;
import com.kongzue.dialogx.interfaces.OnBindView;
import com.kongzue.dialogx.interfaces.OnDialogButtonClickListener;
import com.kongzue.dialogx.interfaces.OnSafeInsetsChangeListener;
import com.kongzue.dialogx.interfaces.PopMoveDisplacementInterceptor;
import com.kongzue.dialogx.util.PopValueAnimator;
import com.kongzue.dialogx.util.TextInfo;
import com.kongzue.dialogx.util.views.DialogXBaseRelativeLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author: Kongzue
 * @github: https://github.com/kongzue/
 * @homepage: http://kongzue.com/
 * @mail: myzcxhh@live.cn
 * @createTime: 2020/10/20 11:59
 */
public class PopNotification extends BaseDialog implements NoTouchInterface {

    public static final int TIME_NO_AUTO_DISMISS_DELAY = -1;
    protected static List<PopNotification> popNotificationList;

    public static long overrideEnterDuration = -1;
    public static long overrideExitDuration = -1;
    public static int overrideEnterAnimRes = 0;
    public static int overrideExitAnimRes = 0;

    public static int maxShowCount = Integer.MAX_VALUE;
    public static PopMoveDisplacementInterceptor<PopNotification> moveDisplacementInterceptor;

    protected OnBindView<PopNotification> onBindView;
    protected DialogLifecycleCallback<PopNotification> dialogLifecycleCallback;
    protected PopNotification me = this;
    protected DialogImpl dialogImpl;
    protected int enterAnimResId = 0;
    protected int exitAnimResId = 0;
    protected DialogXStyle.PopNotificationSettings.ALIGN align;
    protected OnDialogButtonClickListener<PopNotification> onButtonClickListener;
    protected OnDialogButtonClickListener<PopNotification> onPopNotificationClickListener;
    protected boolean autoTintIconInLightOrDarkMode = true;
    protected BOOLEAN tintIcon;
    protected float backgroundRadius = DialogX.defaultPopNotificationBackgroundRadius;
    protected DialogXAnimInterface<PopNotification> dialogXAnimImpl;

    protected int iconResId;
    protected Bitmap iconBitmap;
    protected Drawable iconDrawable;
    protected CharSequence title;
    protected CharSequence message;
    protected CharSequence buttonText;
    protected int iconSize;
    protected boolean slideToClose = true;

    protected TextInfo titleTextInfo;
    protected TextInfo messageTextInfo;
    protected TextInfo buttonTextInfo = new TextInfo().setBold(true);
    protected int[] bodyMargin = new int[]{-1, -1, -1, -1};

    protected PopNotification() {
        super();
    }

    @Override
    public boolean isCancelable() {
        return false;
    }

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

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

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

    public PopNotification(OnBindView<PopNotification> onBindView) {
        this.onBindView = onBindView;
    }

    public PopNotification(CharSequence title) {
        this.title = title;
    }

    public PopNotification(CharSequence title, CharSequence message) {
        this.title = title;
        this.message = message;
    }

    public PopNotification(int titleResId) {
        this.title = getString(titleResId);
    }

    public PopNotification(int titleResId, int messageResId) {
        this.title = getString(titleResId);
        this.message = getString(messageResId);
    }

    public PopNotification(int iconResId, CharSequence title) {
        this.iconResId = iconResId;
        this.title = title;
    }

    public PopNotification(int iconResId, CharSequence title, CharSequence message) {
        this.iconResId = iconResId;
        this.title = title;
        this.message = message;
    }

    public PopNotification(int iconResId, int titleResId, int messageResId) {
        this.iconResId = iconResId;
        this.title = getString(titleResId);
        this.message = getString(messageResId);
    }

    public PopNotification(CharSequence title, OnBindView<PopNotification> onBindView) {
        this.title = title;
        this.onBindView = onBindView;
    }

    public PopNotification(CharSequence title, CharSequence message, OnBindView<PopNotification> onBindView) {
        this.title = title;
        this.message = message;
        this.onBindView = onBindView;
    }

    public PopNotification(int titleResId, OnBindView<PopNotification> onBindView) {
        this.title = getString(titleResId);
        this.onBindView = onBindView;
    }

    public PopNotification(int titleResId, int messageResId, OnBindView<PopNotification> onBindView) {
        this.title = getString(titleResId);
        this.message = getString(messageResId);
        this.onBindView = onBindView;
    }

    public PopNotification(int iconResId, CharSequence title, OnBindView<PopNotification> onBindView) {
        this.iconResId = iconResId;
        this.title = title;
        this.onBindView = onBindView;
    }

    public PopNotification(int iconResId, CharSequence title, CharSequence message, OnBindView<PopNotification> onBindView) {
        this.iconResId = iconResId;
        this.title = title;
        this.message = message;
        this.onBindView = onBindView;
    }

    public PopNotification(int iconResId, int titleResId, int messageResId, OnBindView<PopNotification> onBindView) {
        this.iconResId = iconResId;
        this.title = getString(titleResId);
        this.message = getString(messageResId);
        this.onBindView = onBindView;
    }

    public PopNotification(int iconResId, int titleResId, int messageResId, int buttonTextResId) {
        this.iconResId = iconResId;
        this.title = getString(titleResId);
        this.message = getString(messageResId);
        this.buttonText = getString(buttonTextResId);
    }

    public PopNotification(int iconResId, int titleResId, int messageResId, int buttonTextResId, OnBindView<PopNotification> onBindView) {
        this.iconResId = iconResId;
        this.title = getString(titleResId);
        this.message = getString(messageResId);
        this.buttonText = getString(buttonTextResId);
        this.onBindView = onBindView;
    }

    public PopNotification(int iconResId, CharSequence title, CharSequence message, CharSequence buttonText) {
        this.iconResId = iconResId;
        this.title = title;
        this.message = message;
        this.buttonText = buttonText;
    }

    public PopNotification(int iconResId, CharSequence title, CharSequence message, CharSequence buttonText, OnBindView<PopNotification> onBindView) {
        this.iconResId = iconResId;
        this.title = title;
        this.message = message;
        this.buttonText = buttonText;
        this.onBindView = onBindView;
    }

    public static PopNotification show(OnBindView<PopNotification> onBindView) {
        PopNotification popNotification = new PopNotification(onBindView);
        popNotification.show();
        return popNotification;
    }

    public static PopNotification show(CharSequence title) {
        PopNotification popNotification = new PopNotification(title);
        popNotification.show();
        return popNotification;
    }

    public static PopNotification show(CharSequence title, CharSequence message) {
        PopNotification popNotification = new PopNotification(title, message);
        popNotification.show();
        return popNotification;
    }

    public static PopNotification show(int titleResId) {
        PopNotification popNotification = new PopNotification(titleResId);
        popNotification.show();
        return popNotification;
    }

    public static PopNotification show(int titleResId, int messageResId) {
        PopNotification popNotification = new PopNotification(titleResId, messageResId);
        popNotification.show();
        return popNotification;
    }

    public static PopNotification show(CharSequence title, OnBindView<PopNotification> onBindView) {
        PopNotification popNotification = new PopNotification(title, onBindView);
        popNotification.show();
        return popNotification;
    }

    public static PopNotification show(CharSequence title, CharSequence message, OnBindView<PopNotification> onBindView) {
        PopNotification popNotification = new PopNotification(title, message, onBindView);
        popNotification.show();
        return popNotification;
    }

    public static PopNotification show(int titleResId, OnBindView<PopNotification> onBindView) {
        PopNotification popNotification = new PopNotification(titleResId, onBindView);
        popNotification.show();
        return popNotification;
    }

    public static PopNotification show(int titleResId, int messageResId, OnBindView<PopNotification> onBindView) {
        PopNotification popNotification = new PopNotification(titleResId, messageResId, onBindView);
        popNotification.show();
        return popNotification;
    }

    public static PopNotification show(int iconResId, CharSequence title, OnBindView<PopNotification> onBindView) {
        PopNotification popNotification = new PopNotification(iconResId, title, onBindView);
        popNotification.show();
        return popNotification;
    }

    public static PopNotification show(int iconResId, CharSequence title, CharSequence message, OnBindView<PopNotification> onBindView) {
        PopNotification popNotification = new PopNotification(iconResId, title, message, onBindView);
        popNotification.show();
        return popNotification;
    }

    public static PopNotification show(int iconResId, CharSequence title) {
        PopNotification popNotification = new PopNotification(iconResId, title);
        popNotification.show();
        return popNotification;
    }

    public static PopNotification show(int iconResId, CharSequence title, CharSequence message) {
        PopNotification popNotification = new PopNotification(iconResId, title, message);
        popNotification.show();
        return popNotification;
    }

    public static PopNotification show(int iconResId, int titleResId, int messageResId) {
        PopNotification popNotification = new PopNotification(iconResId, titleResId, messageResId);
        popNotification.show();
        return popNotification;
    }

    public static PopNotification show(int iconResId, int titleResId, int messageResId, OnBindView<PopNotification> onBindView) {
        PopNotification popNotification = new PopNotification(iconResId, titleResId, messageResId, onBindView);
        popNotification.show();
        return popNotification;
    }

    public static PopNotification show(int iconResId, int titleResId, int messageResId, int buttonTextResId) {
        PopNotification popNotification = new PopNotification(iconResId, titleResId, messageResId, buttonTextResId);
        popNotification.show();
        return popNotification;
    }

    public static PopNotification show(int iconResId, int titleResId, int messageResId, int buttonTextResId, OnBindView<PopNotification> onBindView) {
        PopNotification popNotification = new PopNotification(iconResId, titleResId, messageResId, buttonTextResId, onBindView);
        popNotification.show();
        return popNotification;
    }

    public static PopNotification show(int iconResId, CharSequence title, CharSequence message, CharSequence buttonText) {
        PopNotification popNotification = new PopNotification(iconResId, title, message, buttonText);
        popNotification.show();
        return popNotification;
    }

    public static PopNotification show(int iconResId, CharSequence title, CharSequence message, CharSequence buttonText, OnBindView<PopNotification> onBindView) {
        PopNotification popNotification = new PopNotification(iconResId, title, message, buttonText, onBindView);
        popNotification.show();
        return popNotification;
    }

    public PopNotification show() {
        if (isHide && getDialogView() != null) {
            getDialogView().setVisibility(View.VISIBLE);
            return this;
        }
        super.beforeShow();
        if (getDialogView() == null) {
            if (DialogX.onlyOnePopNotification) {
                if (popNotificationList != null && !popNotificationList.isEmpty()) {
                    popNotificationList.get(popNotificationList.size() - 1).dismiss();
                }
            } else {
                if (popNotificationList != null) {
                    CopyOnWriteArrayList<PopNotification> popNotificationCopyOnWriteArrayList = new CopyOnWriteArrayList<>(popNotificationList);
                    for (int i = 0; i < popNotificationCopyOnWriteArrayList.size(); i++) {
                        PopNotification popInstance = popNotificationCopyOnWriteArrayList.get(i);
                        if (popNotificationCopyOnWriteArrayList.size() >= maxShowCount) {
                            if (i <= popNotificationCopyOnWriteArrayList.size() - maxShowCount) {
                                popInstance.dismiss();
                                popNotificationList.remove(popInstance);
                            }
                        }
                    }
                }
            }
            if (popNotificationList == null) popNotificationList = new ArrayList<>();
            popNotificationList.add(PopNotification.this);
            int layoutResId = isLightTheme() ? R.layout.layout_dialogx_popnotification_material : R.layout.layout_dialogx_popnotification_material_dark;
            if (style.popNotificationSettings() != null) {
                if (style.popNotificationSettings().layout(isLightTheme()) != 0) {
                    layoutResId = style.popNotificationSettings().layout(isLightTheme());
                }
                align = style.popNotificationSettings().align();
                if (align == null) align = DialogXStyle.PopNotificationSettings.ALIGN.TOP;
                int styleEnterAnimResId = style.popNotificationSettings().enterAnimResId(isLightTheme());
                int styleExitAnimResId = style.popNotificationSettings().exitAnimResId(isLightTheme());
                enterAnimResId = enterAnimResId == 0 ? (
                        overrideEnterAnimRes == 0 ? (styleEnterAnimResId != 0 ? styleEnterAnimResId : R.anim.anim_dialogx_notification_enter) : overrideEnterAnimRes
                ) : enterAnimResId;
                exitAnimResId = exitAnimResId == 0 ? (
                        overrideExitAnimRes == 0 ? (styleExitAnimResId != 0 ? styleExitAnimResId : R.anim.anim_dialogx_notification_exit) : overrideExitAnimRes
                ) : exitAnimResId;
                enterAnimDuration = enterAnimDuration == -1 ? (
                        overrideEnterDuration
                ) : enterAnimDuration;
                exitAnimDuration = exitAnimDuration == -1 ? (
                        overrideExitDuration
                ) : exitAnimDuration;
            }
            layoutResId = getCustomDialogLayoutResId(isLightTheme()) != 0 ? getCustomDialogLayoutResId(isLightTheme()) : layoutResId;
            View dialogView = createView(layoutResId);
            dialogImpl = new DialogImpl(dialogView);
            if (dialogView != null) dialogView.setTag(me);
            show(dialogView);
        } else {
            show(getDialogView());
        }
        return this;
    }

    public PopNotification show(Activity activity) {
        super.beforeShow();
        if (getDialogView() == null) {
            if (DialogX.onlyOnePopNotification) {
                PopNotification oldInstance = null;
                if (popNotificationList != null && !popNotificationList.isEmpty()) {
                    oldInstance = popNotificationList.get(popNotificationList.size() - 1);
                }
                if (oldInstance != null) {
                    oldInstance.dismiss();
                }
            }
            if (popNotificationList == null) popNotificationList = new ArrayList<>();
            popNotificationList.add(PopNotification.this);
            int layoutResId = isLightTheme() ? R.layout.layout_dialogx_popnotification_material : R.layout.layout_dialogx_popnotification_material_dark;
            if (style.popNotificationSettings() != null) {
                if (style.popNotificationSettings().layout(isLightTheme()) != 0) {
                    layoutResId = style.popNotificationSettings().layout(isLightTheme());
                }
                align = style.popNotificationSettings().align();
                if (align == null) align = DialogXStyle.PopNotificationSettings.ALIGN.TOP;
                int styleEnterAnimResId = style.popNotificationSettings().enterAnimResId(isLightTheme());
                int styleExitAnimResId = style.popNotificationSettings().exitAnimResId(isLightTheme());
                enterAnimResId = enterAnimResId == 0 ? (
                        overrideEnterAnimRes == 0 ? (styleEnterAnimResId != 0 ? styleEnterAnimResId : R.anim.anim_dialogx_notification_enter) : overrideEnterAnimRes
                ) : enterAnimResId;
                exitAnimResId = exitAnimResId == 0 ? (
                        overrideExitAnimRes == 0 ? (styleExitAnimResId != 0 ? styleExitAnimResId : R.anim.anim_dialogx_notification_exit) : overrideExitAnimRes
                ) : exitAnimResId;
                enterAnimDuration = enterAnimDuration == -1 ? (
                        overrideEnterDuration
                ) : enterAnimDuration;
                exitAnimDuration = exitAnimDuration == -1 ? (
                        overrideExitDuration
                ) : exitAnimDuration;
            }
            layoutResId = getCustomDialogLayoutResId(isLightTheme()) != 0 ? getCustomDialogLayoutResId(isLightTheme()) : layoutResId;
            View dialogView = createView(layoutResId);
            dialogImpl = new DialogImpl(dialogView);
            if (dialogView != null) dialogView.setTag(me);
            show(activity, dialogView);
        } else {
            show(activity, getDialogView());
        }
        return this;
    }

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

    protected Timer autoDismissTimer;
    protected long autoDismissDelay = Long.MIN_VALUE;

    public PopNotification autoDismiss(long delay) {
        autoDismissDelay = delay;
        if (autoDismissTimer != null) {
            autoDismissTimer.cancel();
        }
        if (delay < 0) return this;
        autoDismissTimer = new Timer();
        autoDismissTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                dismiss();
            }
        }, delay);
        return this;
    }

    public void resetAutoDismissTimer() {
        autoDismiss(autoDismissDelay);
    }

    private boolean isNoSetCustomDelay() {
        return autoDismissDelay == Long.MIN_VALUE;
    }

    public PopNotification showShort() {
        if (isNoSetCustomDelay()) autoDismiss(2000);
        if (!preShow && !isShow) {
            show();
        }
        return this;
    }

    public PopNotification showLong() {
        autoDismiss(3500);
        if (!preShow && !isShow) {
            show();
        }
        return this;
    }

    public PopNotification showAlways() {
        return noAutoDismiss();
    }

    public PopNotification noAutoDismiss() {
        autoDismiss(TIME_NO_AUTO_DISMISS_DELAY);
        return this;
    }

    private float defaultTop;

    public class DialogImpl implements DialogConvertViewInterface {

        public DialogXBaseRelativeLayout boxRoot;
        public ViewGroup boxBody;
        public ImageView imgDialogxPopIcon;
        public TextView txtDialogxPopTitle;
        public TextView txtDialogxPopMessage;
        public TextView txtDialogxButton;
        public RelativeLayout boxCustom;

        private List<View> blurViews;

        public DialogImpl(View convertView) {
            if (convertView == null) return;
            setDialogView(convertView);
            boxRoot = convertView.findViewById(R.id.box_root);
            boxBody = convertView.findViewById(R.id.box_body);
            imgDialogxPopIcon = convertView.findViewById(R.id.img_dialogx_pop_icon);
            txtDialogxPopTitle = convertView.findViewById(R.id.txt_dialogx_pop_title);
            txtDialogxPopMessage = convertView.findViewById(R.id.txt_dialogx_pop_message);
            txtDialogxButton = convertView.findViewById(R.id.txt_dialogx_button);
            boxCustom = convertView.findViewById(R.id.box_custom);

            blurViews = findAllBlurView(convertView);

            init();
            dialogImpl = this;
            refreshView();
        }

        @Override
        public void init() {
            if (titleTextInfo == null) titleTextInfo = DialogX.titleTextInfo;
            if (messageTextInfo == null) messageTextInfo = DialogX.messageTextInfo;
            if (buttonTextInfo == null) buttonTextInfo = DialogX.buttonTextInfo;
            if (backgroundColor == null) backgroundColor = DialogX.backgroundColor;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getDialogView().setTranslationZ(getThisOrderIndex());
            }

            if (autoDismissTimer == null) {
                showShort();
            }

            boxRoot.setClickable(false);
            boxRoot.setFocusable(false);
            boxRoot.setParentDialog(me);
            boxRoot.setAutoUnsafePlacePadding(false);
            boxRoot.setOnLifecycleCallBack(new DialogXBaseRelativeLayout.OnLifecycleCallBack() {
                @Override
                public void onShow() {
                    isShow = true;
                    preShow = false;

                    setLifecycleState(Lifecycle.State.CREATED);
                    boxRoot.setAlpha(0f);

                    onDialogShow();
                    getDialogLifecycleCallback().onShow(me);
                    PopNotification.this.onShow(me);
                }

                @Override
                public void onDismiss() {
                    if (popNotificationList != null) {
                        popNotificationList.remove(PopNotification.this);
                        if (popNotificationList.isEmpty()) {
                            popNotificationList = null;
                        }
                    }
                    if (autoDismissTimer != null) {
                        autoDismissTimer.cancel();
                    }
                    isShow = false;
                    getDialogLifecycleCallback().onDismiss(me);
                    PopNotification.this.onDismiss(me);
                    setLifecycleState(Lifecycle.State.DESTROYED);
                    dialogImpl = null;
                    BaseDialog.gc();
                }
            });

            RelativeLayout.LayoutParams rlp;
            rlp = ((RelativeLayout.LayoutParams) boxBody.getLayoutParams());
            if (align == null) align = DialogXStyle.PopNotificationSettings.ALIGN.TOP;
            switch (align) {
                case TOP:
                    rlp.removeRule(RelativeLayout.CENTER_IN_PARENT);
                    rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                    break;
                case BOTTOM:
                    rlp.removeRule(RelativeLayout.CENTER_IN_PARENT);
                    rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                    boxRoot.setAutoUnsafePlacePadding(true);
                    break;
                case CENTER:
                    rlp.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
                    rlp.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                    rlp.addRule(RelativeLayout.CENTER_IN_PARENT);
                    break;
            }
            boxBody.setLayoutParams(rlp);

            boxRoot.setOnSafeInsetsChangeListener(new OnSafeInsetsChangeListener() {
                @Override
                public void onChange(Rect unsafeRect) {
                    if (align == DialogXStyle.PopNotificationSettings.ALIGN.TOP) {
                        boxBody.setY(defaultTop <= 0 ? defaultTop = (unsafeRect.top + bodyMargin[1]) : defaultTop);
                    } else if (align == DialogXStyle.PopNotificationSettings.ALIGN.TOP_INSIDE) {
                        boxBody.setPadding(0, unsafeRect.top, 0, 0);
                    }
                }
            });

            boxRoot.setOnBackPressedListener(new DialogXBaseRelativeLayout.PrivateBackPressedListener() {
                @Override
                public boolean onBackPressed() {
                    return false;
                }
            });

            boxRoot.post(new Runnable() {
                @Override
                public void run() {
                    getDialogXAnimImpl().doShowAnim(me, boxBody);

                    if (!DialogX.onlyOnePopNotification) {
                        if (popNotificationList != null) {
                            for (int i = 0; i < popNotificationList.size() - 1; i++) {
                                PopNotification popInstance = popNotificationList.get(i);
                                popInstance.moveBack(boxBody.getHeight());
                            }
                        }
                    }

                    Integer blurFrontColor = null;
                    Float popNotificationRadius = null;
                    if (getStyle().popNotificationSettings() != null && getStyle().popNotificationSettings().blurBackgroundSettings() != null &&
                            getStyle().popNotificationSettings().blurBackgroundSettings().blurBackground()) {
                        blurFrontColor = backgroundColor == null ?
                                getColorNullable(getIntStyleAttr(getStyle().popNotificationSettings().blurBackgroundSettings().blurForwardColorRes(isLightTheme()))) :
                                backgroundColor;
                        popNotificationRadius = getFloatStyleAttr((float) getStyle().popNotificationSettings().blurBackgroundSettings().blurBackgroundRoundRadiusPx());
                    }

                    if (blurViews != null) {
                        for (View blurView : blurViews) {
                            ((BlurViewType) blurView).setOverlayColor(backgroundColor == null ? blurFrontColor : backgroundColor);
                            ((BlurViewType) blurView).setRadiusPx(popNotificationRadius);
                        }
                    }
                    setLifecycleState(Lifecycle.State.RESUMED);
                }
            });

            boxBody.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (dismissAnimFlag) {
                        log("skip click @ A");
                        return;
                    }
                    haptic(v);
                    if (onPopNotificationClickListener != null) {
                        if (!onPopNotificationClickListener.onClick(me, v)) {
                            dismiss();
                        }
                    } else {
                        dismiss();
                    }
                }
            });

            txtDialogxButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (dismissAnimFlag) {
                        return;
                    }
                    haptic(v);
                    if (onButtonClickListener != null) {
                        if (!onButtonClickListener.onClick(me, v)) {
                            doDismiss(v);
                        }
                    } else {
                        doDismiss(v);
                    }
                }
            });
            onDialogInit();
        }

        @Override
        public void refreshView() {
            if (boxRoot == null) {
                return;
            }
            boxRoot.setRootPadding(screenPaddings[0], screenPaddings[1], screenPaddings[2], screenPaddings[3]);
            if (backgroundColor != null) {
                tintColor(boxBody, backgroundColor);

                if (blurViews != null) {
                    for (View blurView : blurViews) {
                        ((BlurViewType) blurView).setOverlayColor(backgroundColor);
                    }
                }
            }

            if (onBindView != null && onBindView.getCustomView() != null) {
                onBindView.bindParent(boxCustom, me);
                boxCustom.setVisibility(View.VISIBLE);
            } else {
                boxCustom.setVisibility(View.GONE);
            }

            if (backgroundRadius > -1) {
                if (boxBody.getBackground() instanceof GradientDrawable) {
                    GradientDrawable gradientDrawable = (GradientDrawable) boxBody.getBackground();
                    if (gradientDrawable != null)
                        gradientDrawable.setCornerRadius(backgroundRadius);
                }
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                    boxBody.setOutlineProvider(new ViewOutlineProvider() {
                        @Override
                        public void getOutline(View view, Outline outline) {
                            outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), backgroundRadius);
                        }
                    });
                    boxBody.setClipToOutline(true);
                }

                if (blurViews != null) {
                    for (View blurView : blurViews) {
                        ((BlurViewType) blurView).setRadiusPx(backgroundRadius);
                    }
                }
            }

            showText(txtDialogxPopTitle, title);
            showText(txtDialogxPopMessage, message);
            showText(txtDialogxButton, buttonText);

            useTextInfo(txtDialogxPopTitle, titleTextInfo);
            useTextInfo(txtDialogxPopMessage, messageTextInfo);
            useTextInfo(txtDialogxButton, buttonTextInfo);

            if (iconBitmap != null && !iconBitmap.isRecycled()) {
                imgDialogxPopIcon.setVisibility(View.VISIBLE);
                imgDialogxPopIcon.setImageBitmap(iconBitmap);
            } else {
                if (iconDrawable != null) {
                    imgDialogxPopIcon.setVisibility(View.VISIBLE);
                    imgDialogxPopIcon.setImageDrawable(iconDrawable);
                } else {
                    if (iconResId != 0) {
                        imgDialogxPopIcon.setVisibility(View.VISIBLE);
                        imgDialogxPopIcon.setImageResource(iconResId);
                    } else {
                        imgDialogxPopIcon.setVisibility(View.GONE);
                    }
                }
            }
