package kr.co.hanwha.hifive.util;

import android.util.Log;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import kr.co.hanwha.hifive.BuildConfig;
import timber.log.Timber;

/**
 * Created by aaron on 26/10/2018
 */
public class DLog {

    private static final String TAG = "Quest";
    private static final int MAX_LOG_LENGTH = 4000;

    public static void init() {
        if (BuildConfig.DEBUG) {
            Timber.plant(new Timber.DebugTree() {
                String logLocation = "";

                @Override
                protected @Nullable String createStackElementTag(@NotNull StackTraceElement element) {
                    logLocation = "(" + element.getFileName() + ":" + element.getLineNumber() + ")";
                    return TAG;
                }

                @Override
                protected void log(int priority, String tag, @NotNull String message, Throwable t) {
                    String msg = message;
                    while (msg.length() > 0) {
                        if (msg.length() > MAX_LOG_LENGTH) {
                            printLog(priority, tag, msg.substring(0, MAX_LOG_LENGTH), t);
                            msg = msg.substring(MAX_LOG_LENGTH);
                        } else {
                            printLog(priority, tag, msg, t);
                            break;
                        }
                    }
                }

                private void printLog(int priority, String tag, @NotNull String message, Throwable t) {
                    if (priority == Log.ASSERT) Log.wtf(tag, logLocation + message);
                    else Log.println(priority, tag, logLocation + message);
                }
            });
        } else {
            //Crashlytics.start();
//                Timber.plant(ReleaseTree())
        }
    }

    public static void d(String s, Object... objects) {
        Timber.d(s, objects);
    }

    public static void d(Throwable throwable, String s, Object... objects) {
        Timber.d(throwable, s, objects);
    }

    public static void i(String s, Object... objects) {
        Timber.i(s, objects);
    }

    public static void i(Throwable throwable, String s, Object... objects) {
        Timber.i(throwable, s, objects);
    }

    public static void w(String s, Object... objects) {
        Timber.w(s, objects);
    }

    public static void w(Throwable throwable, String s, Object... objects) {
        Timber.w(throwable, s, objects);
    }

    public static void e(String s, Object... objects) {
        Timber.e(s, objects);
    }

    public static void e(Throwable throwable, String s, Object... objects) {
        Timber.e(throwable, s, objects);
    }
}
