package cn.org.bjca.trust.push.common

import android.util.Log

object LogHelper {
    private const val TAG = "=====>SZYX_PUSH<====="
    var showLog = true

    /**
     * e
     */
    @JvmStatic
    fun e(message: String) {
        e(message, null, null)
    }

    @JvmStatic
    fun e(message: String, tag: String) {
        e(message, null, tag)
    }

    @JvmStatic
    fun e(message: String, tr: Throwable) {
        e(message, tr, null)
    }

    @JvmStatic
    fun e(message: String, tr: Throwable?, tag: String?) {
        println(tr, tag) {
            if (tr != null) {
                Log.e(TAG, message, tr)
            } else {
                Log.e(TAG, message)
            }
        }
    }

    /**
     * d
     */
    @JvmStatic
    fun d(message: String) {
        d(message, null, null)
    }

    @JvmStatic
    fun d(message: String, tag: String) {
        d(message, null, tag)
    }

    @JvmStatic
    fun d(message: String, tr: Throwable) {
        d(message, tr, null)
    }

    @JvmStatic
    fun d(message: String, tr: Throwable?, tag: String?) {
        println(tr, tag) {
            if (tr != null) {
                Log.d(TAG, message, tr)
            } else {
                Log.d(TAG, message)
            }
        }
    }

    private fun println(tr: Throwable?, tag: String?, log: () -> Unit) {
        if (!showLog) return
        Log.e(
            TAG, ""
        )
        Log.e(
            TAG, "======================================================================"
        )
        Log.e(
            TAG, getTag(tr, tag)
        )
        Log.e(
            TAG, "----------------${tag ?: "-"}----------------"
        )
        log()
        Log.e(
            TAG, "======================================================================"
        )
        Log.e(
            TAG, ""
        )
    }

    private fun getTag(tr: Throwable?, tag: String?): String {
        val caller: StackTraceElement =
            Thread.currentThread().stackTrace[if (tr != null && tag != null) 5 else 6]
        return "${caller.methodName}(${caller.fileName}:${caller.lineNumber})"
    }
}