package com.njcn.gather.systemmonitor.disk.util;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 生成文件名处理工具。
 */
public final class GeneratedFileNameUtil {

    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");

    private GeneratedFileNameUtil() {
    }

    public static String appendToday(String fileName) {
        return appendDate(fileName, LocalDate.now());
    }

    public static String appendDate(String fileName, LocalDate date) {
        if (fileName == null || date == null) {
            return fileName;
        }
        String dateText = DATE_FORMATTER.format(date);
        int separatorIndex = Math.max(fileName.lastIndexOf('/'), fileName.lastIndexOf('\\'));
        int dotIndex = fileName.lastIndexOf('.');
        if (dotIndex > separatorIndex) {
            return fileName.substring(0, dotIndex) + "_" + dateText + fileName.substring(dotIndex);
        }
        return fileName + "_" + dateText;
    }
}
