package it.integry.integrywmsnative.core.utility;

import android.app.ProgressDialog;
import android.content.Context;
import android.text.SpannableString;
import android.util.Log;

import com.crashlytics.android.Crashlytics;
import com.orhanobut.logger.Logger;

import it.integry.integrywmsnative.BuildConfig;
import it.integry.integrywmsnative.core.REST.CommonRESTException;
import it.integry.integrywmsnative.view.dialogs.DialogSimpleMessageHelper;

public class UtilityExceptions {


    public static void defaultException(Context context, Exception ex, ProgressDialog progressDialog){
        progressDialog.dismiss();
        defaultException(context, ex);
    }
    public static void defaultException(Context context, Exception ex){
        defaultException(context, ex, false);
    }

    public static void defaultException(Context context, Exception ex, boolean sendEmail){

        Logger.e(ex, ex.getMessage());
        String errorMessage = CommonRESTException.tryRecognizeThenGetMessage(ex);

        if(errorMessage == null) errorMessage = ex.getMessage();
        DialogSimpleMessageHelper.makeErrorDialog(context, new SpannableString(errorMessage), null, null).show();


        if(!BuildConfig.DEBUG) {
            if(sendEmail) UtilityLogger.errorMe(ex);

            Crashlytics.logException(ex.getCause());
        }
    }

}
