package com.quyunshuo.androidbaseframemvvm.common.util

import android.os.Environment
import android.text.TextUtils
import android.util.Log
import com.elvishew.xlog.LogConfiguration
import com.elvishew.xlog.LogLevel
import com.elvishew.xlog.XLog
import com.elvishew.xlog.flattener.ClassicFlattener
import com.elvishew.xlog.formatter.message.xml.DefaultXmlFormatter
import com.elvishew.xlog.formatter.stacktrace.DefaultStackTraceFormatter
import com.elvishew.xlog.printer.AndroidPrinter
import com.elvishew.xlog.printer.file.FilePrinter
import com.elvishew.xlog.printer.file.backup.FileSizeBackupStrategy
import com.elvishew.xlog.printer.file.clean.FileLastModifiedCleanStrategy
import com.elvishew.xlog.printer.file.naming.DateFileNameGenerator
import com.elvishew.xlog.printer.file.writer.SimpleWriter
import com.quyunshuo.androidbaseframemvvm.common.constant.SavaAddress

import com.quyunshuo.androidbaseframemvvm.common.util.FileUtil.makeFilePath

import java.io.BufferedReader
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.IOException
import java.io.InputStreamReader
import java.io.RandomAccessFile
import java.util.Random


/**
 * log 工具类：可以存储成文件。又可以输出，好东西。
 * tips：不要用于循环之中，不然文件大小会非常恐怖。
 */
object XLogUtil {

    lateinit var fileName: CustomFileNameGenerator;
    fun init() {
        val config = LogConfiguration.Builder()
            .logLevel(LogLevel.ALL)// 指定日志级别，低于该级别的日志将不会被打印，默认为 LogLevel.ALL
            .tag("Snow cone")// 指定 TAG
//            .enableThreadInfo()// 允许打印线程信息 ，如：thread:main这样。
            .xmlFormatter(DefaultXmlFormatter())// 指定 XML 格式化器，默认为 DefaultXmlFormatter
            .stackTraceFormatter(DefaultStackTraceFormatter())// 指定调用栈信息格式化器
            .build();

        val androidPrinter =
            AndroidPrinter(true)

        val logFolder = SavaAddress.log
        fileName = CustomFileNameGenerator()
        // createExternalFolder(logFolder)

        val filePrinter = FilePrinter.Builder(logFolder)
            .fileNameGenerator(fileName)//日志文件名格式
            .backupStrategy(FileSizeBackupStrategy((500 * 1024 * 1024).toLong()))//单个日志文件的大小默认:FileSizeBackupStrategy(1024 * 1024) 500MB
            .cleanStrategy(FileLastModifiedCleanStrategy(10L * 24L * 60L * 60L * 1000L))//日志文件存活时间，单位毫秒 10天
            .flattener(ClassicFlattener())//配置写入文件日志格式：年月日时分秒
            .writer(SimpleWriter())//配置了writer才支持日志写文件
            .build()

        XLog.init(config, androidPrinter, filePrinter)

    }

    fun getDeviceId(): String {
        return fileName.getDeviceId()
    }


    fun createExternalFolder(folderName: String?) {
        val folder = File(folderName)
        if (!folder.exists()) {
            val isCreated = folder.mkdir() // 或者使用mkdirs()
            if (isCreated) {
                Log.d("CreateFolder", "Folder created successfully")
            } else {
                Log.d("CreateFolder", "Folder creation failed")
            }
        } else {
            Log.d("CreateFolder", "Folder already exists")
        }
    }


    fun d(msg: String) {
        XLog.d(msg)
    }

    fun i(msg: String) {
        XLog.i(msg)
    }

    fun e(msg: String) {
        XLog.e(msg)
    }

    fun w(msg: String) {
        XLog.w(msg)
    }

    fun v(msg: String) {
        XLog.v(msg)
    }
}