package org.jabref.logic.integrity;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;

import org.jabref.logic.FilePreferences;
import org.jabref.logic.importer.util.FileFieldParser;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.strings.StringUtil;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.LinkedFile;

import org.jspecify.annotations.Nullable;

public class FileChecker implements ValueChecker {

    private final BibDatabaseContext context;
    private final FilePreferences filePreferences;

    public FileChecker(BibDatabaseContext context, FilePreferences filePreferences) {
        this.context = context;
        this.filePreferences = filePreferences;
    }

    @Override
    public Optional<String> checkValue(@Nullable String value) {
        if (StringUtil.isBlank(value)) {
            return Optional.empty();
        }

        List<LinkedFile> linkedFiles = FileFieldParser
                .parse(value).stream()
                .filter(file -> !file.isOnlineLink())
                .toList();

        for (LinkedFile file : linkedFiles) {
            Optional<Path> linkedFile = file.findIn(context, filePreferences);
            if ((linkedFile.isEmpty()) || !Files.exists(linkedFile.get())) {
                return Optional.of(Localization.lang("link should refer to a correct file path"));
            }
        }

        return Optional.empty();
    }
}
