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: Any?) {
        e(message, null, null)
    }

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

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

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

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

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

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

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

    private fun println(tag: String?, tr: Throwable?, log: () -> Unit) {
        if (!showLog) return
        Log.d(
            TAG, ""
        )
        Log.d(
            TAG, "======================================================================"
        )
        Log.d(
            TAG, getTag(tr, tag)
        )
        Log.d(
            TAG, "----------------${tag ?: "-"}----------------"
        )
        log()
        Log.d(
            TAG, "======================================================================"
        )
        Log.d(
            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})"
    }
}