package it.integry.integrywmsnative.core.utility;

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

import it.integry.integrywmsnative.BuildConfig;

public class UtilityLogger {

    public static void info(String message) {
        Logger.i(message);
    }

    public static void debug(String message) {
        Logger.d(message);
    }

    public static void error(Exception ex) {
        error(ex, "");
    }

    public static void error(Exception ex, String additionalText) {
        if (!BuildConfig.DEBUG) FirebaseCrashlytics.getInstance().recordException(ex);

        Logger.e(ex, additionalText);

//        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);
//        }
    }

    private static String getPreviousStackTraceElement() {
        StackTraceElement[] stackTraceElementsArr = Thread.currentThread().getStackTrace();
        String caller = "UtilityLogger";
        StringBuilder callerNew = new StringBuilder();

        int found = 0;

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

            if(!stackTraceElementsArr[i].getClassName().startsWith("it.integry"))
                continue;

            if (stackTraceElementsArr[i].getMethodName().equalsIgnoreCase("getPreviousStackTraceElement")) {
                found = i;
            }

            if (found > 0 && i >= found + 2) {
                callerNew
                        .append(stackTraceElementsArr[i].getClassName())
                        .append("->")
                        .append(stackTraceElementsArr[i].getMethodName())
                        .append(":")
                        .append(stackTraceElementsArr[i].getLineNumber())
                        .append("\n");
            }

        }

        return callerNew.length() > 0 ? callerNew.toString() : caller;
    }


//    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);
//    }

}
