package com.gh.common.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.CountDownTimer;
import android.provider.Settings;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.view.SimpleDraweeView;
import com.gh.common.constant.Config;
import com.gh.common.filter.RegionSetting;
import com.gh.common.xapk.XapkDialogHelper;
import com.gh.gamecenter.R;
import com.gh.gamecenter.ShellActivity;
import com.gh.gamecenter.adapter.ReportReasonAdapter;
import com.gh.gamecenter.adapter.viewholder.PrivacyPolicyItemViewHolder;
import com.gh.gamecenter.common.base.TrackableDialog;
import com.gh.gamecenter.common.callback.CancelListener;
import com.gh.gamecenter.common.callback.ConfirmListener;
import com.gh.gamecenter.common.callback.SimpleCallback;
import com.gh.gamecenter.common.databinding.DialogAlertDefaultBinding;
import com.gh.gamecenter.common.entity.SimpleGameEntity;
import com.gh.gamecenter.common.entity.SuggestType;
import com.gh.gamecenter.common.utils.DialogHelper;
import com.gh.gamecenter.common.utils.ExtensionsKt;
import com.gh.gamecenter.common.utils.ImageUtils;
import com.gh.gamecenter.common.utils.NetworkUtils;
import com.gh.gamecenter.common.utils.SensorsBridge;
import com.gh.gamecenter.common.view.CustomLinkMovementMethod;
import com.gh.gamecenter.common.view.DrawableView;
import com.gh.gamecenter.common.view.FixLinearLayoutManager;
import com.gh.gamecenter.common.view.MaxHeightLinearLayout;
import com.gh.gamecenter.common.view.MaxHeightNestedScrollView;
import com.gh.gamecenter.common.view.VerticalItemDecoration;
import com.gh.gamecenter.core.AppExecutor;
import com.gh.gamecenter.core.utils.DisplayUtils;
import com.gh.gamecenter.core.utils.EmptyCallback;
import com.gh.gamecenter.core.utils.MtaHelper;
import com.gh.gamecenter.core.utils.SPUtils;
import com.gh.gamecenter.core.utils.SpanBuilder;
import com.gh.gamecenter.core.utils.ToastUtils;
import com.gh.gamecenter.databinding.DialogBindPhoneBinding;
import com.gh.gamecenter.databinding.DialogOverseaConfirmationBinding;
import com.gh.gamecenter.databinding.DialogPackageParseErrorBinding;
import com.gh.gamecenter.databinding.DialogRelievePhoneBinding;
import com.gh.gamecenter.databinding.DialogReportReasonBinding;
import com.gh.gamecenter.databinding.DialogWechatReserveSuccessBinding;
import com.gh.gamecenter.databinding.ImprintContentItemBinding;
import com.gh.gamecenter.databinding.PrivacyItemBinding;
import com.gh.gamecenter.entity.BadgeEntity;
import com.gh.gamecenter.entity.PermissionsEntity;
import com.gh.gamecenter.entity.PrivacyPolicyEntity;
import com.gh.gamecenter.entity.TrackableEntity;
import com.gh.gamecenter.feature.entity.ApkEntity;
import com.gh.gamecenter.feature.entity.Badge;
import com.gh.gamecenter.feature.entity.GameEntity;
import com.gh.gamecenter.feature.entity.SettingsEntity;
import com.gh.gamecenter.help.HelpAndFeedbackBridge;
import com.gh.gamecenter.setting.SettingBridge;
import com.lightgame.adapter.BaseRecyclerAdapter;
import com.lightgame.download.DownloadEntity;
import com.lightgame.utils.AppManager;
import com.lightgame.utils.Util_System_Keyboard;
import com.lightgame.utils.Utils;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;

import kotlin.Unit;
import kotlin.jvm.functions.Function0;

public class DialogUtils {

    public static Dialog showWaitDialog(Context context, String msg) {
        context = checkDialogContext(context);

        Dialog dialog = new Dialog(context);
        View view = View.inflate(context, R.layout.set_wait_dialog, null);
        TextView message = view.findViewById(R.id.set_wait_message);
        message.setText(msg);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false);
        dialog.getWindow().setBackgroundDrawableResource(R.drawable.background_shape_white_radius_8);
        WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
        layoutParams.width = DisplayUtils.dip2px(160);
        dialog.getWindow().setAttributes(layoutParams);
        dialog.show();
        return dialog;
    }

    public static void checkDownload(Context context, String size, CheckDownloadCallBack callBack) {
        if (!NetworkUtils.isNetworkConnected(context)) {
            showNoConnectionDownloadDialog(context, () -> {
                    },
                    () -> callBack.onResponse(true));
        } else if (NetworkUtils.isWifiConnected(context)
                || filter4GorSize(context, size)) {
            callBack.onResponse(false);
        } else if (!SPUtils.getBoolean(getTrafficDownloadHintKey(), true)) {
            AppExecutor.getUiExecutor().executeWithDelay(() -> Utils.toast(context, "当前使用移动网络下载，请注意流量消耗"), 500);
            callBack.onResponse(false);
        } else {
//            MtaHelper.onEvent("移动网络下载", NetworkUtils.getMobileNetworkType(context), "出现弹窗提示");
            showDownloadDialog(context,
                    () -> callBack.onResponse(false),
                    () -> callBack.onResponse(true));
        }
    }

    private static String getTrafficDownloadHintKey() {
        return PackageUtils.getGhVersionName() + "traffic_download_hint";
    }

    private static boolean filter4GorSize(Context context, String size) {
        try {
            if (TextUtils.isEmpty(size)) {
                return false;
            }
            String mb = size.toUpperCase().replace("MB", "").trim();
            Float i = Float.valueOf(mb);
            if (NetworkUtils.isWifiOr4GConnected(context) && i <= 50) {
                AppExecutor.getUiExecutor().executeWithDelay(() -> Utils.toast(context, "当前使用移动网络下载，请注意流量消耗"), 500);

                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    public static void showNoConnectionDownloadDialog(Context context, ConfirmListener listener, CancelListener cancelListener) {
        DialogHelper.showDialog(context, "下载提示", "网络异常，请检查手机网络状态", "知道了", "WiFi自动下载", listener::onConfirm, cancelListener::onCancel, false, "", "");
    }

    public static void showDownloadDialog(Context context, ConfirmListener listener, CancelListener cancelListener) {
        context = checkDialogContext(context);

        final Dialog dialog = new Dialog(context, R.style.GhAlertDialog);

        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_download_traffic, null);
        View allowOnce = contentView.findViewById(R.id.allow_once);
        View allowAlways = contentView.findViewById(R.id.allow_always);
        View wifiAuto = contentView.findViewById(R.id.wifi_auto);

        Context finalContext = context;
        allowOnce.setOnClickListener(v -> {
            AppExecutor.getUiExecutor().executeWithDelay(() -> {
                Utils.toast(finalContext, "已使用移动网络下载，请注意流量消耗");
            }, 500);
            listener.onConfirm();
            dialog.dismiss();
//            MtaHelper.onEvent("移动网络下载", NetworkUtils.getMobileNetworkType(finalContext), "本次允许");
        });
        wifiAuto.setOnClickListener(v -> {
            cancelListener.onCancel();
            dialog.dismiss();
//            MtaHelper.onEvent("移动网络下载", NetworkUtils.getMobileNetworkType(finalContext), "连上WiFi后自动下载");
        });
        allowAlways.setOnClickListener(v -> {
            SPUtils.setBoolean(getTrafficDownloadHintKey(), false);
            AppExecutor.getUiExecutor().executeWithDelay(() -> {
                // 显示了弹窗以后，即便下面这个 toast 放在 listener.onConfirm 后调用也是显示 listener.onConfirm 里的 toast
                // 喷了，延时包治疑难杂症
                Utils.toast(finalContext, "已使用移动网络下载，请注意流量消耗");
            }, 500);
            listener.onConfirm();
            dialog.dismiss();
//            MtaHelper.onEvent("移动网络下载", NetworkUtils.getMobileNetworkType(finalContext), "总是允许");
        });

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(contentView);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.show();
    }

    public static void showSignDialog(Context context, String title, CharSequence message, CharSequence message2
            , String positive, final ConfirmListener cmListener) {
        context = checkDialogContext(context);

        final Dialog dialog = new Dialog(context);

        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_sign, null);
        TextView contentTv = contentView.findViewById(R.id.dialog_content);
        TextView titleTv = contentView.findViewById(R.id.dialog_title);
        TextView negativeTv = contentView.findViewById(R.id.dialog_negative);
        TextView positiveTv = contentView.findViewById(R.id.dialog_positive);
        TextView content2Tv = contentView.findViewById(R.id.dialog_content2);

        contentTv.setText(Html.fromHtml(message.toString()));
        content2Tv.setText(Html.fromHtml(message2.toString()));
        titleTv.setText(Html.fromHtml(title));
        positiveTv.setText(positive);

        negativeTv.setOnClickListener(view -> dialog.dismiss());

        positiveTv.setOnClickListener(view -> {
            if (cmListener != null) {
                cmListener.onConfirm();
            }
            dialog.dismiss();
        });

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(contentView);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.show();
    }

    /**
     * 特殊：目前只在提交问题错误返回时弹出
     */
    public static Dialog showCommunityDialog(Context context,
                                             String title,
                                             String contentTitle,
                                             String contentDes,
                                             String negative,
                                             String positive,
                                             final CancelListener clListener,
                                             final ConfirmListener cmListener) {
        context = checkDialogContext(context);

        final Dialog dialog = new Dialog(context, R.style.GhAlertDialog);

        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_community, null);
        View contentContainer = contentView.findViewById(R.id.content_container);
        TextView titleTv = contentView.findViewById(R.id.title);
        TextView contentTitleTv = contentView.findViewById(R.id.content_title);
        TextView contentDesTv = contentView.findViewById(R.id.content_des);
        TextView negativeTv = contentView.findViewById(R.id.negative);
        TextView positiveTv = contentView.findViewById(R.id.positive);

        titleTv.setText(title);
        contentTitleTv.setText(contentTitle);
        contentDesTv.setText(contentDes);

        if (TextUtils.isEmpty(negative)) {
            negativeTv.setVisibility(View.GONE);
        } else {
            negativeTv.setVisibility(View.VISIBLE);
            negativeTv.setText(negative);
        }

        if (TextUtils.isEmpty(positive)) {
            positiveTv.setVisibility(View.GONE);
        } else {
            positiveTv.setText(positive);
            positiveTv.setVisibility(View.VISIBLE);
        }

        negativeTv.setOnClickListener(view -> {
            if (clListener != null) {
                clListener.onCancel();
            }
            dialog.dismiss();
        });

        positiveTv.setOnClickListener(view -> {
            if (cmListener != null) {
                cmListener.onConfirm();
            }
            dialog.dismiss();
        });

        contentContainer.setOnClickListener(v -> {
            if (cmListener != null) {
                cmListener.onConfirm();
            }
            dialog.dismiss();
        });

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(contentView);
        dialog.show();
        return dialog;
    }

    public static void showPrivacyPolicyDialog(Context context,
                                               @NonNull PrivacyPolicyEntity entity,
                                               SimpleCallback<Boolean> callback) {

        final Context activityContext = checkDialogContext(context);

        // 区分此 dialog 是点击 dialog 外部取消的还是点击返回取消的
        AtomicBoolean isCanceledByClickOutsideOfDialog = new AtomicBoolean(true);

        final Dialog dialog = new Dialog(activityContext, R.style.GhAlertDialog);
        View contentView = LayoutInflater.from(activityContext).inflate(R.layout.dialog_privacy_policy, null);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(contentView);

        Window window = dialog.getWindow();
        if (window != null) {
            window.setBackgroundDrawableResource(android.R.color.transparent);
            WindowManager.LayoutParams params = window.getAttributes();
            params.horizontalMargin = 0;
            params.width = context.getResources().getDisplayMetrics().widthPixels - DisplayUtils.dip2px(40);
//            int height = context.getResources().getDisplayMetrics().heightPixels - DisplayUtils.dip2px(120);
//            int maxHeight = DisplayUtils.dip2px(446);
//            if (height > maxHeight) {
//                params.height = maxHeight;
//            } else {
//                params.height = height;
//            }
            window.setAttributes(params);
        }

        TextView title = contentView.findViewById(R.id.title);
        TextView bottomContent = contentView.findViewById(R.id.bottom_content);
        TextView topContent = contentView.findViewById(R.id.top_content);
        TextView allowButton = contentView.findViewById(R.id.allow_button);
        TextView disallowButton = contentView.findViewById(R.id.disallow_button);
        TextView linkContent = contentView.findViewById(R.id.link_content);
        RecyclerView permissions = contentView.findViewById(R.id.permissions_content);

        permissions.setLayoutManager(new FixLinearLayoutManager(context));
        permissions.setAdapter(new BaseRecyclerAdapter(context) {
            @NonNull
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = mLayoutInflater.inflate(R.layout.privacy_item, parent, false);
                return new PrivacyPolicyItemViewHolder(PrivacyItemBinding.bind(view));
            }

            @Override
            public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
                if (holder instanceof PrivacyPolicyItemViewHolder) {
                    PrivacyPolicyItemViewHolder viewHolder = (PrivacyPolicyItemViewHolder) holder;
                    PrivacyItemBinding binding = viewHolder.getBinding();
                    final PermissionsEntity permissionsEntity = entity.getPermissions().get(position);
                    ImageUtils.display(binding.icon, permissionsEntity.getIcon());
                    binding.name.setText(permissionsEntity.getName());
                    binding.intro.setText(permissionsEntity.getIntro());
                    GenericDraweeHierarchy hierarchy = binding.icon.getHierarchy();
                    if (hierarchy != null) {
                        if (position == 0) {
                            hierarchy.setPlaceholderImage(R.drawable.permission_storage);
                        } else if (position == 1) {
                            hierarchy.setPlaceholderImage(R.drawable.permission_phone_state);
                        }
                    }
                }
            }

            @Override
            public int getItemCount() {
                return entity.getPermissions().size();
            }
        });

        SpannableStringBuilder skipText = new SpannableStringBuilder("查看完整版的隐私政策和用户协议");
        skipText.setSpan(new ClickableSpan() {
            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(ContextCompat.getColor(activityContext, R.color.theme_font));
                ds.setUnderlineText(false);
            }

            @Override
            public void onClick(@NonNull View widget) {
//                MtaHelper.onEvent("隐私政策弹窗", "隐私政策弹窗", "点击隐私政策");
                DirectUtils.directToExternalBrowser(context, context.getString(R.string.privacy_policy_url));
            }
        }, skipText.length() - 9, skipText.length() - 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        skipText.setSpan(new ClickableSpan() {
            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(ContextCompat.getColor(activityContext, R.color.theme_font));
                ds.setUnderlineText(false);
            }

            @Override
            public void onClick(@NonNull View widget) {
//                MtaHelper.onEvent("隐私政策弹窗", "隐私政策弹窗", "点击用户协议");
                DirectUtils.directToExternalBrowser(context, context.getString(R.string.disclaimer_url));
            }
        }, skipText.length() - 4, skipText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        /*skipText.setSpan(new ClickableSpan() {
            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(ContextCompat.getColor(activityContext, R.color.theme_font));
                ds.setUnderlineText(false);
            }

            @Override
            public void onClick(@NonNull View widget) {
//                MtaHelper.onEvent("隐私政策弹窗", "隐私政策弹窗", "点击第三方SDK接入说明");
                activityContext.startActivity(WebActivity.getThirdPartySdkStatementIntent(activityContext));
            }
        }, skipText.length() - 10, skipText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);*/

        title.setText(entity.getTitle());
        linkContent.setText(skipText);
        linkContent.setMovementMethod(LinkMovementMethod.getInstance());
        topContent.setText(entity.getTopContent());
        bottomContent.setText(ExtensionsKt.fromHtml(entity.getBottomContent()));
        bottomContent.setMovementMethod(CustomLinkMovementMethod.getInstance());

        // Remove underline
        Spannable sa = (Spannable) bottomContent.getText();
        for (URLSpan u : sa.getSpans(0, sa.length(), URLSpan.class)) {
            sa.setSpan(new UnderlineSpan() {
                public void updateDrawState(TextPaint tp) {
                    tp.setUnderlineText(false);
                }
            }, sa.getSpanStart(u), sa.getSpanEnd(u), 0);
        }

        allowButton.setOnClickListener(view -> {
            dialog.dismiss();
            callback.onCallback(true);
//            MtaHelper.onEvent("隐私政策弹窗", "隐私政策弹窗", "点击同意");
        });

        disallowButton.setOnClickListener(v -> {
            dialog.dismiss();
            callback.onCallback(false);
//            showPrivacyPolicyDisallowDialog(activityContext, entity, callback);
//            MtaHelper.onEvent("隐私政策弹窗", "隐私政策弹窗", "不同意并退出App");
        });

        dialog.setOnCancelListener(cd -> {
            if (isCanceledByClickOutsideOfDialog.get()) {
//                MtaHelper.onEvent("隐私政策弹窗", "隐私政策弹窗", "点击空白");
            }
        });

        dialog.setOnKeyListener((dialog1, keyCode, event) -> {
            if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
                isCanceledByClickOutsideOfDialog.set(false);
//                MtaHelper.onEvent("隐私政策弹窗", "隐私政策弹窗", "点击返回");
            }
            return false;
        });
        // 用户未同意隐私策略不应该触发 MTA 事件
//        MtaHelper.onEvent("隐私政策弹窗", "隐私政策弹窗", "出现弹窗");

        try {
            dialog.setCancelable(false);
            dialog.show();
        } catch (Exception ignored) {
        }
    }

    public static void showPrivacyPolicyDisallowDialog(Context context,
                                                       @NonNull PrivacyPolicyEntity entity,
                                                       EmptyCallback callback) {
        final Context activityContext = checkDialogContext(context);

        final Dialog dialog = new Dialog(activityContext, R.style.DialogWindowTransparent);

        View contentView = LayoutInflater.from(activityContext).inflate(R.layout.dialog_disallow_privacy_policy, null);
        View backButton = contentView.findViewById(R.id.back_button);
        View reviewButton = contentView.findViewById(R.id.review_button);

        backButton.setOnClickListener(v -> {
            // 用户未同意隐私策略不应该触发 MTA 事件
//            MtaHelper.onEvent("隐私政策弹窗", "退出提示弹窗", "退出应用");
            dialog.dismiss();
            AppManager.getInstance().appExit(activityContext);
        });
        reviewButton.setOnClickListener(v -> {
//            MtaHelper.onEvent("隐私政策弹窗", "退出提示弹窗", "再次查看");
            callback.onCallback();
            dialog.dismiss();
            //showPrivacyPolicyDialog(activityContext, entity, callback);
        });

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(false);
        dialog.setContentView(contentView);
        try {
            dialog.show();
        } catch (Exception ignored) {
        }
    }

    /**
     * 特殊：目前只在提交问题错误返回时弹出
     */
    public static Dialog showUploadDraftDialog(Context context,
                                               final CancelListener clListener,
                                               final ConfirmListener cmListener) {
        context = checkDialogContext(context);

        final Dialog dialog = new Dialog(context, R.style.GhAlertDialog);

        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_video_upload_draft, null);
        TextView negativeTv = contentView.findViewById(R.id.negative);
        TextView positiveTv = contentView.findViewById(R.id.positive);
        TextView content = contentView.findViewById(R.id.content);
        positiveTv.setBackground(DrawableView.getOvalDrawable(R.color.background, 999));
        negativeTv.setBackground(DrawableView.getOvalDrawable(R.color.theme, 999));
        content.setText(Html.fromHtml(context.getString(R.string.video_upload_draft_dialog_content)));

        negativeTv.setOnClickListener(view -> {
            if (clListener != null) {
                clListener.onCancel();
            }
            dialog.dismiss();
        });

        positiveTv.setOnClickListener(view -> {
            if (cmListener != null) {
                cmListener.onConfirm();
            }
            dialog.dismiss();
        });


        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(contentView);
        dialog.show();
        return dialog;
    }

    public static Dialog showTrackableDialog(Context context,
                                             String title,
                                             CharSequence message,
                                             String positive,
                                             String negative,
                                             final ConfirmListener cmListener,
                                             final CancelListener clListener,
                                             TrackableEntity trackableEntity) {
        return showTrackableDialog(context, false, title, message, positive, negative, cmListener, clListener, trackableEntity);
    }

    /**
     * @param useRoundedCornerStyle 使用圆角样式
     */
    public static Dialog showTrackableDialog(Context context,
                                             boolean useRoundedCornerStyle,
                                             String title,
                                             CharSequence message,
                                             String positive,
                                             String negative,
                                             final ConfirmListener cmListener,
                                             final CancelListener clListener,
                                             TrackableEntity trackableEntity) {
        context = checkDialogContext(context);

        final TrackableDialog dialog = new TrackableDialog(context,
                R.style.GhAlertDialog,
                trackableEntity.getEvent(),
                trackableEntity.getKey(),
                trackableEntity.getValue(),
                trackableEntity.getCancelValue(),
                trackableEntity.getKeyBackValue(),
                trackableEntity.getLogShowEvent());

        View contentView;
        if (useRoundedCornerStyle) {
            contentView = LayoutInflater.from(context).inflate(R.layout.dialog_alert_with_rounded_corner, null);
            Window window = dialog.getWindow();
            if (window != null) {
                window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            }
        } else {
            contentView = LayoutInflater.from(context).inflate(R.layout.dialog_alert, null);
        }

        TextView contentTv = contentView.findViewById(R.id.dialog_content);
        TextView titleTv = contentView.findViewById(R.id.dialog_title);
        TextView negativeTv = contentView.findViewById(R.id.dialog_negative);
        TextView positiveTv = contentView.findViewById(R.id.dialog_positive);
        if (message.toString().contains("红包奖励")) {//将红包奖励四个字标红
            String str = message.toString().substring(0, message.toString().indexOf("红包奖励")) + "<font color='#FF0000'>红包奖励</font>";
            contentTv.setText(Html.fromHtml(str));
        } else {
            contentTv.setText(message);
        }
        titleTv.setText(title);
        negativeTv.setText(negative);
        positiveTv.setText(positive);

        negativeTv.setOnClickListener(view -> {
            if (clListener != null) {
                clListener.onCancel();
            }
            dialog.dismiss();
        });

        positiveTv.setOnClickListener(view -> {
            if (cmListener != null) {
                cmListener.onConfirm();
            }
            dialog.dismiss();
        });

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(contentView);
        dialog.show();
        return dialog;
    }

    public static Dialog fixWebViewKeyboardNotWorking(Activity activity) {
        final Dialog dialog = new Dialog(activity, R.style.TransparentDialog);
        View view = new View(activity);
        view.setOnClickListener(v -> dialog.dismiss());
        view.postDelayed(() -> {
            if (!activity.isFinishing()) {
                dialog.show();
                dialog.dismiss();
            }
        }, 500);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(view);
        return dialog;
    }

    public static Dialog showUsageStatsDialog(Context context, final ConfirmListener cmListener, final CancelListener clListener) {
        context = checkDialogContext(context);

        final Dialog dialog = new Dialog(context, R.style.GhAlertDialog);

        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_usage_stats, null);
        TextView negativeTv = contentView.findViewById(R.id.dialog_negative);
        TextView positiveTv = contentView.findViewById(R.id.dialog_positive);

        Window window = dialog.getWindow();
        if (window != null) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }

        negativeTv.setOnClickListener(view -> {
            if (clListener != null) {
                clListener.onCancel();
            }
            dialog.dismiss();
        });

        positiveTv.setOnClickListener(view -> {
            if (cmListener != null) {
                cmListener.onConfirm();
            }
            dialog.dismiss();
        });

        dialog.setOnDismissListener(dialog1 -> {
            if (clListener != null) {
                clListener.onCancel();
            }
        });

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(contentView);
        dialog.show();
        return dialog;
    }

    public static void showDownloadMutexDialog(Context context) {
        context = checkDialogContext(context);

        final Dialog dialog = new Dialog(context, R.style.GhAlertDialog);

        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_downlaod_mutex, null);
        TextView positive = contentView.findViewById(R.id.dialog_positive);

        CountDownTimer timer = new CountDownTimer(6000, 1000) {
            public void onTick(long millisUntilFinished) {
                positive.setText(("我知道了（" + millisUntilFinished / 1000 + "）"));
            }

            public void onFinish() {
                dialog.dismiss();
            }
        };
        timer.start();

        positive.setOnClickListener(v -> {
            dialog.dismiss();
        });

        Window window = dialog.getWindow();
        if (window != null) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(contentView);
        dialog.show();
    }

    // 海外下载地址弹窗
    public static void showOverseaDownloadDialog(Context context, GameEntity gameEntity, @NonNull ConfirmListener listener) {
        context = checkDialogContext(context);

        if (gameEntity.getOverseasAddressDialog() == null
                || gameEntity.getApk().size() == 0
                || !gameEntity.getOverseasAddressDialog().isEnable()) {
            listener.onConfirm();
        } else {
            final Dialog dialog = new Dialog(context, R.style.GhAlertDialog);

            DialogOverseaConfirmationBinding binding = DialogOverseaConfirmationBinding.inflate(LayoutInflater.from(context), null, false);

            View contentView = binding.getRoot();

            binding.gameIcon.displayGameIcon(gameEntity);
            binding.gameNameTv.setText(context.getString(R.string.dialog_oversea_hint, gameEntity.getName()));
            binding.closeIv.setOnClickListener(v -> dialog.dismiss());

            if ("show&download".equals(gameEntity.getOverseasAddressDialog().getStatus())) {
                gameEntity.getApk().get(0).setUrl(gameEntity.getOverseasAddressDialog().getLink());
            }
            binding.urlTv.setText(gameEntity.getOverseasAddressDialog().getLink());
            binding.downloadBtn.setText("下载（" + gameEntity.getApk().get(0).getSize() + "）");
            binding.downloadBtn.setOnClickListener(v -> {
                listener.onConfirm();
                dialog.dismiss();
            });

            Window window = dialog.getWindow();
            if (window != null) {
                window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            }

            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(contentView);
            dialog.show();
        }
    }

    public static void showGameH5DownloadDialog(Context context, GameEntity gameEntity, RegionSetting.GameH5Download gameH5Download) {
        context = checkDialogContext(context);

        final Dialog dialog = new Dialog(context, R.style.GhAlertDialog);

        DialogOverseaConfirmationBinding binding = DialogOverseaConfirmationBinding.inflate(LayoutInflater.from(context), null, false);

        View contentView = binding.getRoot();

        binding.gameIcon.displayGameIcon(gameEntity);
        binding.gameNameTv.setText(context.getString(R.string.dialog_oversea_hint, gameEntity.getName()));
        binding.closeIv.setOnClickListener(v -> dialog.dismiss());

        Context finalContext = context;
        binding.urlTv.setText(gameH5Download.getDownloadLink());
        binding.downloadBtn.setText(gameH5Download.getButtonText());
        binding.downloadBtn.setOnClickListener(v -> {
            DirectUtils.directToExternalBrowser(finalContext, gameH5Download.getH5Link());
            dialog.dismiss();
        });

        Window window = dialog.getWindow();
        if (window != null) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(contentView);
        dialog.show();
    }

    public static void showImprintDialog(Context context, GameEntity gameEntity, String titleName) {
        context = checkDialogContext(context);
        Dialog dialog = new Dialog(context, R.style.full_dialog);
        View inflate = LayoutInflater.from(context).inflate(R.layout.imprint_dialog, null);
        dialog.setContentView(inflate);
        dialog.show();

        Window window = dialog.getWindow();
        WindowManager.LayoutParams params;
        if (window != null) {
            params = window.getAttributes();
            params.width = (int) (context.getResources().getDisplayMetrics().widthPixels * 0.9);
            window.setAttributes(params);
            window.setBackgroundDrawableResource(R.drawable.textview_white_up);
        }

        inflate.findViewById(R.id.imprint_close).setOnClickListener(v -> dialog.dismiss());
        LinearLayout content = inflate.findViewById(R.id.imprint_content);
        ((TextView) inflate.findViewById(R.id.imprint_title)).setText(titleName);
        View head = LayoutInflater.from(context).inflate(R.layout.imprint_content_item, null);
        content.addView(head, LinearLayout.LayoutParams.MATCH_PARENT, DisplayUtils.dip2px(30));
        MaxHeightLinearLayout imprintContainer = inflate.findViewById(R.id.imprint_container);
        imprintContainer.setMaxHeight((int) (context.getResources().getDisplayMetrics().heightPixels * 0.8));

        ArrayList<ApkEntity> list = gameEntity.getApk();
        SettingsEntity settings = Config.getSettings();
        for (int i = 0; i < list.size(); i++) {
            ApkEntity apk = gameEntity.getApk().get(i);
            if (settings != null && settings.getGameDownloadBlackList().contains(apk.getPackageName())) {
                continue;
            }
            View item = LayoutInflater.from(context).inflate(R.layout.imprint_content_item, null);
            ImprintContentItemBinding bind = ImprintContentItemBinding.bind(item);
            String platform = TextUtils.isEmpty(apk.getRemark()) ? apk.getPlatformName() : apk.getPlatformName() + "\n" + apk.getRemark();
            bind.imprintPlatform.setText(platform);
            bind.imprintVersion.setText(apk.getVersion());
            bind.imprintTime.setText(NewsUtils.getFormattedTime(apk.getTime() != null ? apk.getTime() : 0));
            content.addView(item, LinearLayout.LayoutParams.MATCH_PARENT, DisplayUtils.dip2px(40));
        }


        // close line
        View view = new View(context);
        view.setBackgroundColor(context.getResources().getColor(R.color.text_5d5d5d));
        view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DisplayUtils.dip2px(1)));
        content.addView(view);
    }

    public static void showKaifuRemindDialog(Context context, String content, String gameName) {
        context = checkDialogContext(context);

        final Dialog dialog = new TrackableDialog(context, R.style.GhAlertDialog, "开服说明弹窗", "弹窗", gameName, null, null, true);

        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_kaifu_remind, null);

        TextView contentTv = contentView.findViewById(R.id.contentTv);
        MaxHeightNestedScrollView scrollView = contentView.findViewById(R.id.scrollView);
        contentTv.setText(Html.fromHtml(content));
        TextView ok = contentView.findViewById(R.id.dialog_ok);
        scrollView.setScrollChangedListener((l, t, oldl, oldt) -> {
            MtaHelper.onEvent("开服说明弹窗", "滑动内容", gameName);
        });
        ok.setOnClickListener(v -> {
            MtaHelper.onEvent("开服说明弹窗", "弹窗", "点击我知道了");
            MtaHelper.onEvent("开服说明弹窗", "点击我知道了", gameName);
            dialog.dismiss();
        });

        Window window = dialog.getWindow();
        if (window != null) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(contentView);
        dialog.show();
    }

    public static void showStopServerExplanationDialog(Context context, String content, String gameName) {
        context = checkDialogContext(context);

        final Dialog dialog = new TrackableDialog(context, R.style.GhAlertDialog, "评论说明弹窗", "弹窗", gameName, null, null, true);

        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_stop_service_explanation, null);

        TextView contentTv = contentView.findViewById(R.id.contentTv);
        MaxHeightNestedScrollView scrollView = contentView.findViewById(R.id.scrollView);
        if (TextUtils.isEmpty(content)) {
            content = context.getString(R.string.rating_protection);
        }
        contentTv.setText(content);
        TextView ok = contentView.findViewById(R.id.dialog_ok);
        scrollView.setScrollChangedListener((l, t, oldl, oldt) -> {
            MtaHelper.onEvent("评论说明弹窗", "滑动内容", gameName);
        });
        ok.setOnClickListener(v -> {
            MtaHelper.onEvent("评论说明弹窗", "弹窗", "点击我知道了");
            MtaHelper.onEvent("评论说明弹窗", "点击我知道了", gameName);
            dialog.dismiss();
        });

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(contentView);
        dialog.show();
        Window window = dialog.getWindow();
        if (window != null) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = context.getResources().getDisplayMetrics().widthPixels - DisplayUtils.dip2px(60f);
            window.setAttributes(params);
        }
    }

    public static void showPluggableNeverRemindDialog(Context context, String nameAndPlatform, @NonNull ConfirmListener listener) {
        String content = "助手首页将不再提示《" + nameAndPlatform + "》的所有插件化消息，确定吗？";
        DialogHelper.showDialog(context, "温馨提醒", content, "确定", "取消", listener::onConfirm, () -> {
        }, false, "", "");
    }

    public static void showRegulationTestDialog(Context context, @NonNull ConfirmListener confirmListener, @NonNull CancelListener cancelListener) {
        context = checkDialogContext(context);

        SensorsBridge.trackEvent("EtiquetteTestDialogShow");

        final Dialog dialog = new TrackableDialog(
                context,
                R.style.GhAlertDialog,
                "礼仪考试",
                "礼仪考试弹窗",
                null, null, null,
                true);

        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_regulation_test, null);

        View cancelBtn = contentView.findViewById(R.id.cancel);
        View confirmBtn = contentView.findViewById(R.id.confirm);
        View linkTv = contentView.findViewById(R.id.link);

        Context finalContext = context;
        linkTv.setOnClickListener(v -> {

            MtaHelper.onEvent("礼仪考试", "礼仪考试弹窗", "了解更多");
            DirectUtils.directToArticle(finalContext, "5f4477be25e07e19be4e2a22", "(礼仪测试弹窗)");
        });

        cancelBtn.setOnClickListener(v -> {
            cancelListener.onCancel();

            MtaHelper.onEvent("礼仪考试", "礼仪考试弹窗", "跳过");
            SensorsBridge.trackEvent("EtiquetteTestDialogClick", "button_name", "跳过");
            dialog.dismiss();
        });

        Context finalContext1 = context;
        confirmBtn.setOnClickListener(v -> {
            Util_System_Keyboard.hideSoftKeyboard((Activity) finalContext1);

            MtaHelper.onEvent("礼仪考试", "礼仪考试弹窗", "确定");
            SensorsBridge.trackEvent("EtiquetteTestDialogClick", "button_name", "进入考试");
            confirmListener.onConfirm();
            dialog.dismiss();
        });

        Window window = dialog.getWindow();
        if (window != null) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(contentView);
        dialog.show();
    }

    public static void showUnzipFailureDialog(Context context, DownloadEntity downloadEntity) {
        XapkDialogHelper.INSTANCE.showUnzipFailureDialog(context, downloadEntity, false);
    }

    public static void showNoticeDialog(Context context, String title, String content, @NonNull ConfirmListener listener) {
        context = checkDialogContext(context);

        final Dialog dialog = new Dialog(context, R.style.DialogWindowTransparent);

        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_notice, null);
        TextView titleTv = contentView.findViewById(R.id.dialog_title);
        TextView contentTv = contentView.findViewById(R.id.dialog_content);

        if (title == null) {
            titleTv.setVisibility(View.GONE);
        } else {
            titleTv.setVisibility(View.VISIBLE);
            titleTv.setText(title);
        }
        contentTv.setText(content);

        contentView.findViewById(R.id.dialog_ok).setOnClickListener(v -> {
            listener.onConfirm();
            dialog.dismiss();
        });

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(contentView);
        dialog.show();
    }

    public static void showViewBadgeDialog(Context context, Badge badge, ConfirmListener listener) {
        context = checkDialogContext(context);

        final Dialog dialog = new Dialog(context, R.style.DialogWindowTransparent);

        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_view_badge, null);
        TextView titleTv = contentView.findViewById(R.id.dialog_title);
        TextView contentTv = contentView.findViewById(R.id.dialog_content);
        SimpleDraweeView badgeView = contentView.findViewById(R.id.dialog_badge);
        ImageView badgeLightBg = contentView.findViewById(R.id.dialog_badge_light_bg);

        ImageUtils.display(badgeView, badge.getMIcon());
        titleTv.setText(badge.getName());

        // 领取条件文本超过一行就不显示
        if (badge.getActions() == null || badge.getActions().isEmpty() || badge.getActions().size() > 1) {
            contentTv.setVisibility(View.INVISIBLE);
        } else {
            contentTv.setText(badge.getActions().get(0).getText());
            contentTv.post(() -> {
                if (contentTv.getLineCount() > 1) contentTv.setVisibility(View.INVISIBLE);
            });
        }

        Animation animation = AnimationUtils.loadAnimation(context, R.anim.anim_badge_light_bg);
        badgeLightBg.startAnimation(animation);

        contentView.findViewById(R.id.dialog_ok).setOnClickListener(v -> {
            listener.onConfirm();
            badgeLightBg.clearAnimation();
            dialog.dismiss();
        });

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(contentView);
        dialog.show();
    }

    public static void showReceiveBadgeDialog(Context context, BadgeEntity badge, ConfirmListener listener) {
        context = checkDialogContext(context);

        final Dialog dialog = new Dialog(context, R.style.DialogWindowTransparent);

        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_receive_badge, null);
        TextView titleTv = contentView.findViewById(R.id.dialog_title);
        TextView contentTv = contentView.findViewById(R.id.dialog_content);
        SimpleDraweeView badgeView = contentView.findViewById(R.id.dialog_badge);
        ImageView badgeLightBg = contentView.findViewById(R.id.dialog_badge_light_bg);
        TextView receiveTv = contentView.findViewById(R.id.dialog_ok);

        ImageUtils.display(badgeView, badge.getIcon());
        titleTv.setText(badge.getName());

        // 领取条件文本超过一行就不显示
        if (badge.getActions() == null || badge.getActions().isEmpty() || badge.getActions().size() > 1) {
            contentTv.setVisibility(View.INVISIBLE);
        } else {
            contentTv.setText(badge.getActions().get(0).getText());
            contentTv.post(() -> {
                if (contentTv.getLineCount() > 1) contentTv.setVisibility(View.INVISIBLE);
            });
        }

        if ("self".equals(badge.getReceive().getType())) {
            receiveTv.setText(R.string.receive_badge);
        } else if ("auto".equals(badge.getReceive().getType())) {
            receiveTv.setText(R.string.view_badge);
        } else {
            receiveTv.setText(R.string.apply_badge);
        }

        Animation animation = AnimationUtils.loadAnimation(context, R.anim.anim_badge_light_bg);
        badgeLightBg.startAnimation(animation);

        contentView.findViewById(R.id.dialog_ok).setOnClickListener(v -> {
            listener.onConfirm();
            badgeLightBg.clearAnimation();
            dialog.dismiss();
        });

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(contentView);
        dialog.show();
    }

    public static void showPassRegulationDialog(Context context, String icon) {
        context = checkDialogContext(context);

        final Dialog dialog = new Dialog(context, R.style.DialogWindowTransparent);

        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_pass_regulation, null);
        SimpleDraweeView userIcon = contentView.findViewById(R.id.dialog_icon);

        ImageUtils.display(userIcon, icon);

        contentView.findViewById(R.id.dialog_ok).setOnClickListener(v -> {
            dialog.dismiss();
        });

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(contentView);
        dialog.show();
    }

    public static void showShortCutPermissionDialog(Context context) {
        context = checkDialogContext(context);

        final Dialog dialog = new Dialog(context, R.style.DialogWindowTransparent);

        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_shortcut_permission, null);

        Context finalContext = context;
        contentView.findViewById(R.id.dialog_positive).setOnClickListener(v -> {
            dialog.dismiss();
            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.setData(Uri.parse("package:" + finalContext.getPackageName()));
            finalContext.startActivity(intent);
        });

        contentView.findViewById(R.id.dialog_negative).setOnClickListener(v -> {
            dialog.dismiss();
        });

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(contentView);
        dialog.show();
    }

    public static Dialog showCancelOrDeleteReservationDialog(Context context, String title, String message
            , String positive, String negative, final ConfirmListener cmListener, final CancelListener clListener) {
        context = checkDialogContext(context);

        final Dialog dialog = new Dialog(context, R.style.GhAlertDialog);

        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_cancel_reservation, null);
        TextView contentTv = contentView.findViewById(R.id.dialog_content);
        TextView titleTv = contentView.findViewById(R.id.dialog_title);
        TextView negativeTv = contentView.findViewById(R.id.dialog_negative);
        TextView positiveTv = contentView.findViewById(R.id.dialog_positive);
        contentTv.setText(message);
        titleTv.setText(title);
        negativeTv.setText(negative);
        positiveTv.setText(positive);

        negativeTv.setOnClickListener(view -> {
            if (clListener != null) {
                clListener.onCancel();
            }
            dialog.dismiss();
        });

        positiveTv.setOnClickListener(view -> {
            if (cmListener != null) {
                cmListener.onConfirm();
            }
            dialog.dismiss();
        });

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(contentView);
        dialog.show();
        Window window = dialog.getWindow();
        if (window != null) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = context.getResources().getDisplayMetrics().widthPixels - DisplayUtils.dip2px(60f);
            window.setAttributes(params);
        }
        return dialog;
    }

    public static void showSimulatorParseErrorDialog(Context context, String gameId, String gameName, ConfirmListener confirmListener) {
        context = checkDialogContext(context);
        final Dialog dialog = new Dialog(context, R.style.GhAlertDialog);

        DialogPackageParseErrorBinding binding = DialogPackageParseErrorBinding.inflate(LayoutInflater.from(context), null, false);
        Context finalContext = context;
        SpannableStringBuilder builder = new SpanBuilder("您也可以点击提交反馈跟我们联系").click(context, 6, 10, R.color.theme_font, true, new Function0<Unit>() {
            @Override
            public Unit invoke() {
                SimpleGameEntity entity = new SimpleGameEntity(gameId, gameName, "");
                HelpAndFeedbackBridge.startSuggestionActivity(finalContext, SuggestType.gameQuestion, "notfound", "模拟器安装包解析错误", entity, "-");
                dialog.dismiss();
                return null;
            }
        }).build();
        binding.feedbackTv.setText(builder);
        binding.feedbackTv.setMovementMethod(new LinkMovementMethod());

        binding.cancel.setOnClickListener(v -> {
            dialog.dismiss();
        });
        binding.confirm.setOnClickListener(v -> {
            confirmListener.onConfirm();
            dialog.dismiss();
        });

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(binding.getRoot());
        dialog.show();
        Window window = dialog.getWindow();
        if (window != null) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = context.getResources().getDisplayMetrics().widthPixels - DisplayUtils.dip2px(60f);
            window.setAttributes(params);
        }
    }

    public static void showReportReasonDialog(Context context, ArrayList<String> items, ReportReasonCallBack callBack) {
        showReportReasonDialog(context, items, "", callBack);
    }

    @SuppressLint("SetTextI18n")
    public static void showReportReasonDialog(Context context, ArrayList<String> items, String title, ReportReasonCallBack callBack) {
        context = checkDialogContext(context);

        final Dialog dialog = new Dialog(context, R.style.DialogWindowTransparent);
        DialogReportReasonBinding binding = DialogReportReasonBinding.inflate(LayoutInflater.from(context));

        if (!title.isEmpty()) {
            binding.reasonTitle.setText(title);
        }

        ReportReasonAdapter reportReasonAdapter = new ReportReasonAdapter(context, items, reason -> {
            if (reason.equals("其他原因")) {
                binding.reasonTitle.setText(R.string.report_reason_other_title);
                binding.normalReasonContainer.setVisibility(View.GONE);
                binding.otherReasonContainer.setVisibility(View.VISIBLE);
            } else {
                dialog.dismiss();
                callBack.onResponse(reason, "");
            }
            return null;
        });
        binding.reasonRv.setLayoutManager(new LinearLayoutManager(context));
        binding.reasonRv.addItemDecoration(new VerticalItemDecoration(context, 1F, false, R.color.background));
        binding.reasonRv.setAdapter(reportReasonAdapter);

        binding.negativeBtn.setOnClickListener(v -> {
            binding.reasonTitle.setText(R.string.report_reason_title);
            binding.normalReasonContainer.setVisibility(View.VISIBLE);
            binding.otherReasonContainer.setVisibility(View.GONE);
        });

        binding.positiveBtn.setOnClickListener(v -> {
            if (TextUtils.isEmpty(binding.otherReasonEt.getText().toString().trim())) {
                ToastUtils.showToast("请填写举报原因");
            } else {
                dialog.dismiss();
                callBack.onResponse("其他原因", binding.otherReasonEt.getText().toString());
            }
        });

        Context finalContext = context;
        ExtensionsKt.setTextChangedListener(binding.otherReasonEt, (s, start, before, count) -> {
            int tvCount = s.length();
            if (tvCount >= 500) {
                binding.tvCount.setTextColor(ContextCompat.getColor(finalContext, R.color.theme_red));
            }
            binding.tvCount.setText(tvCount + "/500");
            return null;
        });

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(binding.getRoot());
        dialog.show();
    }

    public static Dialog showBindPhoneDialog(Context context, ConfirmListener listener) {
        context = checkDialogContext(context);

        final Dialog dialog = new Dialog(context, R.style.DialogWindowTransparent);
        DialogBindPhoneBinding binding = DialogBindPhoneBinding.inflate(LayoutInflater.from(context));

        binding.positiveTv.setOnClickListener(v -> {
            dialog.dismiss();
            listener.onConfirm();
        });

        binding.closeIv.setOnClickListener(v -> {
            dialog.dismiss();
        });

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(binding.getRoot());
        dialog.show();
        return dialog;
    }

    public static void showReserveSuccess2WechatBindDialog(Context context, ConfirmListener confirmListener, CancelListener cancelListener) {
        context = checkDialogContext(context);

        final Dialog dialog = new Dialog(context, R.style.DialogWindowTransparent);
        DialogWechatReserveSuccessBinding binding = DialogWechatReserveSuccessBinding.inflate(LayoutInflater.from(context));
        binding.closeBtn.setOnClickListener(v -> {
            cancelListener.onCancel();
            dialog.dismiss();
        });
        binding.openBtn.setOnClickListener(v -> {
            confirmListener.onConfirm();
            dialog.dismiss();
        });
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(binding.getRoot());
        dialog.show();
        Window window = dialog.getWindow();
        if (window != null) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = DisplayUtils.dip2px(300f);
            window.setAttributes(params);
        }
    }

    public static void showReserveSuccessDialog(Context context) {
        context = checkDialogContext(context);

        final Dialog dialog = new Dialog(context, R.style.DialogWindowTransparent);
        DialogAlertDefaultBinding binding = DialogAlertDefaultBinding.inflate(LayoutInflater.from(context));
        binding.titleTv.setText("游戏预约成功");
        binding.contentTv.setText("游戏上线时，您将在消息中心和微信公众号收到通知，不会错过任何预约的游戏");
        binding.confirmTv.setText("我知道了");
        binding.centerDivider.setVisibility(View.GONE);
        binding.cancelTv.setVisibility(View.GONE);
        ExtensionsKt.setDrawableStart(binding.titleTv, R.drawable.ic_reserve_success, null, null);
        binding.confirmTv.setOnClickListener(v -> dialog.dismiss());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(binding.getRoot());
        dialog.show();
        Window window = dialog.getWindow();
        if (window != null) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = DisplayUtils.dip2px(300f);
            window.setAttributes(params);
        }
    }

    public static void showRelievePhoneDialog(Context context) {
        context = checkDialogContext(context);

        final Dialog dialog = new Dialog(context, R.style.DialogWindowTransparent);
        DialogRelievePhoneBinding binding = DialogRelievePhoneBinding.inflate(LayoutInflater.from(context));

        Context finalContext = context;
        binding.realNameBtn.setOnClickListener(v -> {
            dialog.dismiss();
            Intent intent = ShellActivity.getIntent(finalContext, ShellActivity.Type.REAL_NAME_INFO);
            finalContext.startActivity(intent);
        });

        binding.bindPhoneBtn.setOnClickListener(v -> {
            dialog.dismiss();
            Intent intent = SettingBridge.INSTANCE.getBindPhoneNormalIntent(finalContext, false);
            finalContext.startActivity(intent);
        });

        binding.cancelBtn.setOnClickListener(v -> dialog.dismiss());

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(binding.getRoot());
        dialog.show();
    }

    /**
     * @param context may be is application context
     * @return activity context
     */
    public static Context checkDialogContext(Context context) {
        if (context == null) {
            throw new NullPointerException("dialog context is null");
        }

        if (context instanceof Activity) {
            return context;
        }

        // currentActivity 是否存在 isDestroyed 的情况?
        return AppManager.getInstance().currentActivity();
    }

    public interface OptionCallback {
        void onClicked(String text);
    }

    public interface CheckDownloadCallBack {
        void onResponse(boolean isSubscribe);
    }

    public interface ReportReasonCallBack {
        void onResponse(String reason, String desc);
    }
}
