package com.xty.network.utils

import android.text.TextUtils
import com.tencent.mm.opensdk.utils.Log
import com.youth.banner.BuildConfig

object NetWorkLogUtils {

    private const val TAG = "ZJ365"

    private const val DEBUG = BuildConfig.DEBUG

    /**
     * 得到tag
     * @return 所在类->方法:行
     */
    private fun generateTag(): String {
        val stackTraceElement = Thread.currentThread().stackTrace[6]
//        val callerClazzName =
//            stackTraceElement.className.substring(stackTraceElement.className.lastIndexOf(".") + 1)
        val fileName = stackTraceElement.fileName
//        val methodName = stackTraceElement.methodName
        val lineNumber = stackTraceElement.lineNumber
        return "($fileName:$lineNumber)"
    }


    fun d(msg: String) {
        d(null, msg)
    }

    fun i(msg: String) {
        i(null, msg)
    }

    fun e(msg: String) {
        e(null, msg)
    }

    fun w(msg: String) {
        w(null, msg)
    }

    fun v(msg: String) {
        v(null, msg)
    }

    fun d(tag: String?, msg: String) {//测试环境输出日志
        if (DEBUG) {
            Log.d(TAG, getMsg(tag, msg))
        }
    }

    fun i(tag: String?, msg: String) {//测试环境输出日志
        if (DEBUG) {
            Log.i(TAG, getMsg(tag, msg))
        }
    }

    fun e(tag: String?, msg: String) {
        if (DEBUG) {
            Log.e(TAG, getMsg(tag, msg))
        }
    }

    fun w(tag: String?, msg: String) {//测试环境输出日志
        if (DEBUG) {
            Log.w(TAG, getMsg(tag, msg))
        }
    }

    fun v(tag: String?, msg: String) {//测试环境输出日志
        if (DEBUG) {
            Log.v(TAG, getMsg(tag, msg))
        }
    }

    private fun getMsg(tag: String?, msg: String): String{
        val tagStr = if (TextUtils.isEmpty(tag)) generateTag() else generateTag() + ":$tag"
        return "$msg:$tagStr"
    }
}