package it.integry.integrywmsnative.core.utility;

import com.google.firebase.crashlytics.FirebaseCrashlytics;
import com.orhanobut.logger.Logger;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.SocketException;
import java.net.SocketTimeoutException;

import it.integry.integrywmsnative.BuildConfig;
import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.rest.consumers.SystemRESTConsumer;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.settings.SettingsManager;

public class UtilityLogger {

    public static void logMe(String message) {
        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();

        String caller = "UtilityLogger";

        for (int i = 0; i < stackTraceElements.length; i++) {

            if (stackTraceElements[i].getMethodName().equalsIgnoreCase("logMe")) {
                caller = stackTraceElements[i + 1].getFileName() + "->" + stackTraceElements[i + 1].getMethodName();
            }

        }

        Logger.d(message);
    }

    public static void errorMe(Exception ex) {
        errorMe(ex, null, null, null);
    }

    public static void errorMe(Exception ex, String additionalText) {
        errorMe(ex, additionalText, null, null);
    }

    public static void errorMe(Exception ex, String additionalText, Runnable onComplete, RunnableArgs<Exception> onFailed) {
        if(!BuildConfig.DEBUG) FirebaseCrashlytics.getInstance().recordException(ex);

        logMe(ex.getMessage());

//        if ((!(ex instanceof SocketTimeoutException) && !(ex.getCause() instanceof SocketTimeoutException)) ||
//                (!(ex instanceof SocketException) && !(ex.getCause() instanceof SocketException)) ||
//                (!(ex instanceof IOException) && !(ex.getCause() instanceof IOException))) {
//
//            if(ex.getMessage().contains("SocketTimeoutException")) return;
//
//
//            String message = UtilityResources.readRawTextFile(R.raw.error_mail);
//
//            message = message.replace("__APP_NAME__", UtilityResources.getString(R.string.app_name));
//            message = message.replace("__APP_PACKAGE__", UtilityResources.getPackageName());
//
//            String appVersion = UtilityResources.getAppVersion();
//            message = message.replace("__APP_VERSION__", appVersion == null ? "" : appVersion);
//            message = message.replace("__APP_VERSION_CODE__", "" + UtilityResources.getAppVersionCode());
//
//
//            message = message.replaceAll("__ERROR_TITLE__", ex.getMessage());
//
//            message = message.replaceAll("__ERROR_DETAIL__", additionalText == null ? "" : additionalText);
//
//
//            StringWriter sw = new StringWriter();
//            ex.printStackTrace(new PrintWriter(sw));
//
//            message = message.replace("__ERROR_STACKTRACE__", sw.toString());
//
//            String currentAzienda = UtilityString.isNullOrEmpty(SettingsManager.i().getUserSession().getProfileDB()) ? "" : SettingsManager.i().getUserSession().getProfileDB();
//            message = message.replace("__CUSTOMER_NAME__", currentAzienda);
//
//            message = message.replace("__ERROR_DATE__", UtilityDate.formatDate(UtilityDate.getDateInstance(), UtilityDate.COMMONS_DATE_FORMATS.DMY_TIME_SLASH));
//
//            SystemRESTConsumer.sendErrorLogMail(message, onComplete, onFailed);
//        }
    }

}
