package com.zhangmeng.online.exam.ui.service;


import com.zhangmeng.online.exam.ui.module.OperationLog;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class LogService {
    private static final List<LogEntry> logs = new ArrayList<>();
    private static final int MAX_LOGS = 10000; // 最多保存10000条日志

    /**
     * 记录日志
     */
    public static void log(String module, String operation, String message) {
        log(module, operation, message, null);
    }

    /**
     * 记录带异常的日志
     */
    public static void log(String module, String operation, String message, Exception e) {
        LogEntry entry = new LogEntry(
            module,
            operation,
            message,
            e != null ? e.getMessage() : null,
            LocalDateTime.now()
        );
        
        synchronized (logs) {
            logs.add(entry);
            if (logs.size() > MAX_LOGS) {
                logs.remove(0);
            }
        }
    }

    /**
     * 搜索日志
     */
    public static List<OperationLog> searchLogs(String keyword) {
        return logs.stream()
            .filter(log -> keyword == null || keyword.isEmpty() || log.getMessage().contains(keyword))
            .map(log -> new OperationLog(
                0, // Assuming ID is not used or set elsewhere
                log.getModule(),
                log.getOperation(),
                log.getMessage(),
                "127.0.0.1", // Placeholder for IP
                log.getTime(),
                "成功", // Placeholder for status
                log.getError() // Assuming error message is used as errorMsg
            ))
            .collect(Collectors.toList());
    }

    public static List<OperationLog> searchLogs(String keyword, String type, LocalDateTime startDate, LocalDateTime endDate) {

        return logs.stream()

                .filter(log -> {

                    // 检查日期范围

                    if (startDate != null && log.getTime().isBefore(startDate)) {

                        return false;

                    }

                    if (endDate != null && log.getTime().isAfter(endDate)) {

                        return false;

                    }



                    // 检查日志类型

                    if (type != null && !type.isEmpty() && !log.getModule().equals(type)) {

                        return false;

                    }



                    // 检查关键字

                    if (keyword != null && !keyword.isEmpty() && !log.getMessage().contains(keyword)) {

                        return false;

                    }



                    return true;

                })

                .map(log -> new OperationLog(

                        0, // Assuming ID is not used or set elsewhere

                        log.getModule(),

                        log.getOperation(),

                        log.getMessage(),

                        "127.0.0.1", // Placeholder for IP

                        log.getTime(),

                        "成功", // Placeholder for status

                        log.getError() // Assuming error message is used as errorMsg

                ))

                .collect(Collectors.toList());

    }

    /**
     * 清空日志
     */
    public static void clearLogs() {
        synchronized (logs) {
            logs.clear();
        }
    }

    /**
     * 日志条目类
     */
    private static class LogEntry {
        private final String module;
        private final String operation;
        private final String message;
        private final String error;
        private final LocalDateTime time;

        public LogEntry(String module, String operation, String message, 
                       String error, LocalDateTime time) {
            this.module = module;
            this.operation = operation;
            this.message = message;
            this.error = error;
            this.time = time;
        }

        public String getModule() {
            return module;
        }

        public String getOperation() {
            return operation;
        }

        public String getMessage() {
            return message;
        }

        public String getError() {
            return error;
        }

        public LocalDateTime getTime() {
            return time;
        }
    }
} 