package com.tofly.ghrq_route.common.util.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

import androidx.appcompat.app.AlertDialog;

import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.base.URLResponseCode;
import com.tofly.ghrq_route.common.ui.activity.UiLogin;

public class DialogUtil {
    public static AlertDialog showAlertWithConfirmButton(Context context, int titleId, int messageId, boolean cancelable,
                                                         final DialogInterface.OnClickListener listener ){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(titleId);
        builder.setMessage(messageId);
        builder.setPositiveButton(android.R.string.ok, (dialog, which) -> {
            if (listener != null)
                listener.onClick(dialog, which);
        });
        builder.setCancelable(cancelable);
        return builder.show();
    }

    public static void showAlertWithConfirmButton(Context context, String title, String message, boolean cancelable,
                                                  final DialogInterface.OnClickListener listener ){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton(android.R.string.ok, (dialog, which) -> {
            if (listener != null)
                listener.onClick(dialog, which);
        });
        builder.setCancelable(cancelable);
        builder.show();
    }

    public static void showAlertWithConfirmCancelButton(Context context, int titleId, int messageId, boolean cancelable,
                                                        final DialogInterface.OnClickListener listener ){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(titleId);
        builder.setMessage(messageId);
        builder.setPositiveButton(android.R.string.ok, listener);
        builder.setNegativeButton(android.R.string.cancel, null);
        builder.setCancelable(cancelable);
        builder.show();
    }

    public static void showAlertWithConfirmCancelButton(Context context, String title, String message, boolean cancelable,
                                                        final DialogInterface.OnClickListener listener ){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton(android.R.string.ok, listener);
        builder.setNegativeButton(android.R.string.cancel, null);
        builder.setCancelable(cancelable);
        builder.show();
    }

    public static void showAlertDialog(Context context, int title, int msg, DialogInterface.OnClickListener onClickListener) {
        showAlertWithConfirmCancelButton(context, title, msg, false, onClickListener);
    }

    public static void showAlertDialog(Context context, String title, String msg, DialogInterface.OnClickListener onClickListener) {
        showAlertWithConfirmCancelButton(context, title, msg, false, onClickListener);
    }

    public static void showAlertConfirmDialog(Context context, String title, String msg, DialogInterface.OnClickListener onClickListener) {
        showAlertWithConfirmButton(context, title, msg, false, onClickListener);
    }

    public static void showTokenFailedDialog(Context context, int errorCode){
        if (errorCode == URLResponseCode.ErrorCode.TOKEN_VERIFY_FAILED){
            BaseApp.getInstance().stopAllBackgroundService(context);
            showAlertDialogToForward(context, R.string.dialog_token_failed_title,
                    R.string.dialog_token_failed_msg, UiLogin.class);
        }
    }

    public static void showMustUpdateAppDialog(Context context, int errorCode, String  errmsg){
        if (errorCode == URLResponseCode.ErrorCode.APP_MUST_UPDATE){
            BaseApp.getInstance().stopAllBackgroundService(context);
            showAlertConfirmDialog(context, context.getResources().getString(R.string.dialog_app_must_update), errmsg, null);
        }
    }

    public static void showAlertDialogToForward(Context context, int title, int msg, final Class<?> classObj) {
        showAlertWithConfirmButton(context, title, msg, false, (dialog, which) -> {
            Intent intent = new Intent();
            intent.setClass(context, classObj);
            context.startActivity(intent);

            if (context instanceof Activity) {
                Activity activity = (Activity) context;
                activity.finish();
            }
        });
    }
}
