package com.atridad.ascently.utils

import android.util.Log
import com.atridad.ascently.BuildConfig

object AppLogger {

    private const val DEFAULT_TAG = "Ascently"

    enum class Level(val androidLevel: Int) {
        DEBUG(Log.DEBUG),
        INFO(Log.INFO),
        WARN(Log.WARN),
        ERROR(Log.ERROR),
    }

    fun d(tag: String = DEFAULT_TAG, messageProvider: () -> String) {
        log(Level.DEBUG, tag, messageProvider)
    }

    fun i(tag: String = DEFAULT_TAG, messageProvider: () -> String) {
        log(Level.INFO, tag, messageProvider)
    }

    fun w(tag: String = DEFAULT_TAG, throwable: Throwable? = null, messageProvider: () -> String) {
        log(Level.WARN, tag, messageProvider, throwable)
    }

    fun e(tag: String = DEFAULT_TAG, throwable: Throwable? = null, messageProvider: () -> String) {
        log(Level.ERROR, tag, messageProvider, throwable)
    }

    private fun log(
        level: Level,
        tag: String,
        messageProvider: () -> String,
        throwable: Throwable? = null,
    ) {
        if (!BuildConfig.DEBUG) return

        val message = messageProvider()
        if (throwable != null) {
            Log.println(level.androidLevel, tag, "$message\n${Log.getStackTraceString(throwable)}")
        } else {
            Log.println(level.androidLevel, tag, message)
        }
    }
}
