package com.narutohuo.xindazhou.core.log.impl

import com.narutohuo.xindazhou.core.log.ILogger
import com.narutohuo.xindazhou.core.log.LogLevel
import timber.log.Timber

/**
 * Timber 日志实现（默认实现，单例）
 * 
 * 封装 Timber 日志库，实现 ILog 接口
 * 以后如果不用 Timber，只需修改此实现类，或提供新的实现类
 * 
 * 使用方式：
 * ```kotlin
 * val log: ILog = TimberLog.getInstance()
 * ```
 */
class TimberLog private constructor() : ILogger {
    
    companion object {
        @Volatile
        private var INSTANCE: TimberLog? = null
        
        /**
         * 获取单例实例
         */
        @JvmStatic
        fun getInstance(): ILogger {
            return INSTANCE ?: synchronized(this) {
                INSTANCE ?: TimberLog().also { INSTANCE = it }
            }
        }
    }
    
    override fun d(tag: String, message: String) {
        Timber.tag(tag).d(message)
    }
    
    override fun i(tag: String, message: String) {
        Timber.tag(tag).i(message)
    }
    
    override fun w(tag: String, message: String) {
        Timber.tag(tag).w(message)
    }
    
    override fun e(tag: String, message: String, throwable: Throwable?) {
        if (throwable != null) {
            Timber.tag(tag).e(throwable, message)
        } else {
            Timber.tag(tag).e(message)
        }
    }
    
    override fun log(level: LogLevel, tag: String, message: String, throwable: Throwable?) {
        when (level) {
            LogLevel.DEBUG -> d(tag, message)
            LogLevel.INFO -> i(tag, message)
            LogLevel.WARN -> w(tag, message)
            LogLevel.ERROR -> e(tag, message, throwable)
        }
    }
    
    override fun uploadLogs() {
        // Timber 默认不提供日志上传功能，可以在这里扩展
        // 或者由 app 模块提供自定义实现
    }
    
    override fun collectCrash(throwable: Throwable) {
        // Timber 默认不提供崩溃收集功能，可以在这里扩展
        // 或者由 app 模块提供自定义实现
        Timber.e(throwable, "崩溃信息")
    }
}

