package com.gh.gamecenter.common.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.gh.gamecenter.common.R;
import com.gh.gamecenter.common.callback.CancelListener;
import com.gh.gamecenter.common.callback.ConfirmListener;
import com.gh.gamecenter.common.constant.RouteConsts;
import com.gh.gamecenter.core.provider.IConfigProvider;
import com.gh.gamecenter.core.provider.IDirectProvider;
import com.lightgame.utils.AppManager;

public class DialogUtils {

    private DialogUtils() {
        throw new IllegalStateException("Utility class");
    }

    /**
     * @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();
    }

    // 网络劫持时 打开QQ客户端，创建临时会话
    public static void showQqSessionDialog(final Context context) {
        IConfigProvider configProvider = (IConfigProvider) ARouter.getInstance().build(RouteConsts.provider.config).navigation();
        IDirectProvider directProvider = (IDirectProvider) ARouter.getInstance().build(RouteConsts.provider.directUtils).navigation();
        String qq = configProvider.getQQ();
        showWarningDialog(context, "警告", "您当前网络环境异常，下载地址可能被运营商恶意替换（网络劫持）" +
                        "，如多次下载失败，请联系客服获取正确的下载地址（客服QQ：" + qq + "）"
                , "取消", "前往QQ", () -> directProvider.directToQqConversation(context, qq), null);
    }

    public static void showWarningDialog(Context context, String title, CharSequence msg, final ConfirmListener listener) {
        if (!(context instanceof Activity)) {
            return;
        }
        showWarningDialog(context, title, msg, "取消", "确定", listener, null);
    }

    public static void showWarningDialog(Context context, String title, CharSequence msg, String cancel, String confirm,
                                         final ConfirmListener cmListener, final CancelListener clListener) {

        showAlertDialog(context, title, msg, confirm, cancel, cmListener, clListener);
    }

    /**
     * Material Design 风格弹窗
     *
     * @param context
     * @param title      标题
     * @param message    内容
     * @param positive   确认按钮文本
     * @param negative   取消按钮文本
     * @param cmListener 确认按钮监听
     * @param clListener 取消按钮监听
     */
    public static Dialog showAlertDialog(Context context, String title, CharSequence 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_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 showAlertDialog(Context context, String title, Spanned 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_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);
        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;
    }
}
