/**
 * @file SystemLogger.cpp
 * @brief 系统日志管理器单例类实现
 * @author Qt UI Developer Expert
 * @date 2024-12-21
 * @version 1.0
 */

#include "utils/SystemLogger.h"
#include <QDateTime>
#include <QDebug>
#include <QMutexLocker>
#include <QCoreApplication>
#include <iostream>

// 静态成员初始化
SystemLogger* SystemLogger::s_instance = nullptr;
QMutex SystemLogger::s_mutex;

SystemLogger::SystemLogger(QObject *parent)
    : QObject(parent)
    , m_consoleOutputEnabled(true)
{
    qDebug() << "SystemLogger instance created";
}

SystemLogger::~SystemLogger()
{
    qDebug() << "SystemLogger instance destroyed";
}

SystemLogger* SystemLogger::getInstance()
{
    // 双重检查锁定模式确保线程安全
    if (s_instance == nullptr) {
        QMutexLocker locker(&s_mutex);
        if (s_instance == nullptr) {
            s_instance = new SystemLogger();
        }
    }
    return s_instance;
}

void SystemLogger::destroyInstance()
{
    QMutexLocker locker(&s_mutex);
    if (s_instance != nullptr) {
        delete s_instance;
        s_instance = nullptr;
    }
}

void SystemLogger::logDebug(const QString &message)
{
    log(Debug, message);
}

void SystemLogger::logInfo(const QString &message)
{
    log(Info, message);
}

void SystemLogger::logWarning(const QString &message)
{
    log(Warning, message);
}

void SystemLogger::logError(const QString &message)
{
    log(Error, message);
}

void SystemLogger::logSuccess(const QString &message)
{
    log(Success, message);
}

void SystemLogger::log(LogLevel level, const QString &message)
{
    logInternal(level, message);
}

void SystemLogger::setConsoleOutputEnabled(bool enabled)
{
    QMutexLocker locker(&m_logMutex);
    m_consoleOutputEnabled = enabled;
    
    QString statusMsg = enabled ? "Console output enabled" : "Console output disabled";
    qDebug() << "SystemLogger:" << statusMsg;
}

QString SystemLogger::getLevelString(LogLevel level)
{
    switch (level) {
        case Debug:   return "DEBUG";
        case Info:    return "INFO";
        case Warning: return "WARNING";
        case Error:   return "ERROR";
        case Success: return "SUCCESS";
        default:      return "UNKNOWN";
    }
}

void SystemLogger::logInternal(LogLevel level, const QString &message)
{
    // 线程安全保护
    QMutexLocker locker(&m_logMutex);
    
    // 输出到控制台（如果启用）
    if (m_consoleOutputEnabled) {
        outputToConsole(level, message);
    }
    
    // 发出信号通知UI组件
    emit logAdded(level, message);
}

void SystemLogger::outputToConsole(LogLevel level, const QString &message)
{
    QString timestamp = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
    QString levelStr = getLevelString(level);
    QString logLine = QString("[%1] [%2] %3").arg(timestamp, levelStr, message);
    
    // 根据日志级别选择输出流
    switch (level) {
        case Error:
            // 错误输出到标准错误流
            std::cerr << logLine.toStdString() << std::endl;
            break;
        case Warning:
            // 警告也输出到标准错误流
            std::cerr << logLine.toStdString() << std::endl;
            break;
        default:
            // 其他级别输出到标准输出流
            std::cout << logLine.toStdString() << std::endl;
            break;
    }
    
    // 同时也输出到Qt的调试系统
    switch (level) {
        case Debug:
            qDebug().noquote() << logLine;
            break;
        case Info:
            qInfo().noquote() << logLine;
            break;
        case Warning:
            qWarning().noquote() << logLine;
            break;
        case Error:
            qCritical().noquote() << logLine;
            break;
        case Success:
            qInfo().noquote() << logLine;  // 成功信息使用qInfo输出
            break;
    }
}