package de.beyondsoft.ownchat.utils;

import de.beyondsoft.ownchat.R;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.StringRes;
import android.support.v7.app.AlertDialog;

/**
 * Created by Rares Teodorescu on 22/02/2017.
 */
public final class DialogHandler {

    /**
     * Create a simple dialog with no title
     *
     * @param context        The context from which the dialog will be shown
     * @param message        The string resource for the message
     * @param cancelable     True if the dialog should be cancelable, false otherwise
     * @param positiveButton The string resource for the positive button
     * @param callback       The callback for the positive button action
     *
     * @return The dialog made
     */
    public static Dialog createNoTitleDialog(
            Context context,
            @StringRes int message,
            boolean cancelable,
            @StringRes int positiveButton,
            DialogInterface.OnClickListener callback) {

        return createSimpleDialog(context, 0, message, cancelable, positiveButton, callback);
    }

    public static Dialog createNoTitleDialog(
            Context context,
            @StringRes int message,
            boolean cancelable,
            @StringRes int positiveButton,
            @StringRes int negativeButton,
            DialogInterface.OnClickListener posCallback,
            DialogInterface.OnClickListener negCallback) {
        if (context == null) {
            return null;
        }
        final AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.StyledDialog);
        return builder
                .setMessage(message)
                .setPositiveButton(positiveButton, posCallback)
                .setNegativeButton(negativeButton, negCallback)
                .setCancelable(cancelable)
                .create();
    }

    /**
     * Create a simple dialog
     *
     * @param context        The context from which the dialog will be shown
     * @param title          The string resource for the title, 0 for none
     * @param message        The string resource for the message
     * @param cancelable     True if the dialog should be cancelable, false otherwise
     * @param positiveButton The string resource for the positive button
     * @param callback       The callback for the positive button action
     *
     * @return The dialog that was just shown
     */
    private static Dialog createSimpleDialog(
            Context context,
            @StringRes int title,
            @StringRes int message,
            boolean cancelable,
            @StringRes int positiveButton,
            DialogInterface.OnClickListener callback) {

        final AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.StyledDialog);

        if (title != 0) {
            builder.setTitle(title);
        }

        return builder
                .setMessage(message)
                .setPositiveButton(positiveButton, callback)
                .setCancelable(cancelable)
                .create();
    }

    public static Dialog showSimpleDialog(Context context,
                                          int title,
                                          int text,
                                          int posBtn,
                                          DialogInterface.OnClickListener callback) {

        return createSimpleDialog(context, title, text, true, posBtn, callback);
    }
}
