import log from 'electron-log/main';
import { app } from 'electron';
import path from 'path';

log.initialize();

// 配置日志立即写入
log.transports.file.level = 'info';
log.transports.console.level = 'info';

// 确保日志立即写入文件
log.transports.file.sync = true;

// 获取日志文件路径
const logFilePath = log.transports.file.getFile().path;
log.info(`日志文件路径: ${logFilePath}`);

// 日志级别配置
const LOG_LEVELS = {
  ERROR: 0,
  WARN: 1,
  INFO: 2,
  DEBUG: 3
};

let currentLogLevel = LOG_LEVELS.INFO; // 默认日志级别

// 设置日志级别
export function setLogLevel(level) {
  if (typeof level === 'string') {
    currentLogLevel = LOG_LEVELS[level.toUpperCase()] || LOG_LEVELS.INFO;
  } else {
    currentLogLevel = level;
  }
}

// 检查是否应该打印日志
function shouldLog(level) {
  return level <= currentLogLevel;
}

export const logger = {
  log: (message, ...args) => {
    if (shouldLog(LOG_LEVELS.INFO)) {
      log.info(message, ...args);
    }
  },
  info: (message, ...args) => {
    if (shouldLog(LOG_LEVELS.INFO)) {
      log.info(message, ...args);
    }
  },
  error: (message, ...args) => {
    if (shouldLog(LOG_LEVELS.ERROR)) {
      log.error(message, ...args);
    }
  },
  warn: (message, ...args) => {
    if (shouldLog(LOG_LEVELS.WARN)) {
      log.warn(message, ...args);
    }
  },
  debug: (message, ...args) => {
    if (shouldLog(LOG_LEVELS.DEBUG)) {
      log.debug(message, ...args);
    }
  },
  getLogPath: () => logFilePath,
  setLogLevel: setLogLevel
}

export default logger
