/*
 * Copyright (C) 2022 - Florindo Smilari (spherIC)
 */
package ch.spherIC.recurvebowsight.utilities;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.Toast;

import java.text.DecimalFormat;

import ch.spherIC.recurvebowsight.R;


/**
 * @author FC Smilari
 */
public class PlatformUtils {

    public static final String DEFAULT_SETTINGS_PREF = "DefaultSettings";
    public static final String DEFAULT_SETTING_AIRTEMP = "airTemperature";
    public static final String DEFAULT_SETTING_RELAIRHUMIDITY = "relAirHumidity";
    public static final String DEFAULT_SETTING_SHOOTINGDIST = "shootingDistance";
    private static final DecimalFormat DF_1 = new DecimalFormat("0.#");

    public static Double parseDouble(String doubleVal) {
        if (!TextUtils.isEmpty(doubleVal)) {
            try {
                return Double.parseDouble(doubleVal);
            } catch (NumberFormatException e) {
                return null;
            }
        }
        return null;
    }

    public static AlertDialog getDeleteConfirmDialog(final Context context, final String itemToDelete,
                                                     final DialogInterface.OnClickListener okListener) {


        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        String msg = context.getResources().getString(R.string.caption_DeleteMsg, itemToDelete);
        builder = builder.setTitle(R.string.caption_ConfirmDelete).setMessage(msg);
        builder = builder.setPositiveButton(R.string.caption_Delete, okListener).setNegativeButton(android.R.string.cancel, null);

        // Create the AlertDialog object and return it
        return builder.create();
    }

    public static Toast toast(Context context, String message) {
        Toast toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        return toast;
    }

    public static Toast toast(Context context, String message, int length, int gravity, int xOffset, int yOffset) {
        Toast toast = Toast.makeText(context, message, length);
        toast.setGravity(gravity, xOffset, yOffset);
        return toast;
    }

}
