import 'package:flutter/foundation.dart';
import 'package:loggy/loggy.dart';

/// 日志工具类
/// 🔧 Android 15 诊断模式：临时允许 Release 模式下输出关键日志用于问题诊断
class KRLogUtil {
  static final KRLogUtil _instance = KRLogUtil._internal();
  factory KRLogUtil() => _instance;
  KRLogUtil._internal();

  /// 🔧 临时启用 Release 日志用于 Android 15 问题诊断
  /// ⚠️ 生产环境修复后应该改回 false
  static const bool _forceEnableReleaseLogging = true;

  /// 初始化日志
  static void kr_init() {
    // Debug 模式或强制启用时初始化日志
    if (kDebugMode || _forceEnableReleaseLogging) {
      Loggy.initLoggy(
        logPrinter: PrettyPrinter(),
      );
    }
  }

  /// 调试日志
  /// 🔧 Debug 模式或强制启用时输出
  static void kr_d(String message, {String? tag}) {
    if (kDebugMode || _forceEnableReleaseLogging) {
      Loggy('${tag ?? 'KRLogUtil'}').debug(message);
    }
  }

  /// 信息日志
  /// 🔧 Debug 模式或强制启用时输出
  static void kr_i(String message, {String? tag}) {
    if (kDebugMode || _forceEnableReleaseLogging) {
      Loggy('${tag ?? 'KRLogUtil'}').info(message);
    }
  }

  /// 警告日志
  /// 🔧 Debug 模式或强制启用时输出
  static void kr_w(String message, {String? tag}) {
    if (kDebugMode || _forceEnableReleaseLogging) {
      Loggy('${tag ?? 'KRLogUtil'}').warning(message);
    }
  }

  /// 错误日志
  /// 🔧 Debug 模式或强制启用时输出
  static void kr_e(String message, {String? tag, Object? error, StackTrace? stackTrace}) {
    if (kDebugMode || _forceEnableReleaseLogging) {
      Loggy('${tag ?? 'KRLogUtil'}').error(message, error, stackTrace);
    }
  }

  /// 网络日志
  /// 🔧 Debug 模式或强制启用时输出
  static void kr_network(String message, {String? tag}) {
    if (kDebugMode || _forceEnableReleaseLogging) {
      Loggy('${tag ?? 'Network'}').info(message);
    }
  }

  /// 性能日志
  /// 🔧 Debug 模式或强制启用时输出
  static void kr_performance(String message, {String? tag}) {
    if (kDebugMode || _forceEnableReleaseLogging) {
      Loggy('${tag ?? 'Performance'}').info(message);
    }
  }
} 