package org.jabref.logic.importer.fetcher;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.regex.Pattern;

import org.jabref.logic.importer.FetcherException;
import org.jabref.logic.importer.FulltextFetcher;
import org.jabref.logic.importer.IdBasedFetcher;
import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.logic.importer.ParseException;
import org.jabref.logic.importer.fileformat.BibtexParser;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.net.URLDownload;
import org.jabref.logic.util.URLUtil;
import org.jabref.logic.util.strings.StringUtil;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;

import org.jspecify.annotations.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class IacrEprintFetcher implements FulltextFetcher, IdBasedFetcher {
    public static final String NAME = "IACR eprints";

    private static final Logger LOGGER = LoggerFactory.getLogger(IacrEprintFetcher.class);

    private static final Pattern WITHOUT_LETTERS_SPACE = Pattern.compile("[^0-9/]");

    private static final Predicate<String> IDENTIFIER_PREDICATE = Pattern.compile("\\d{4}/\\d{3,5}").asPredicate();
    private static final String CITATION_URL_PREFIX = "https://eprint.iacr.org/";
    private static final String DESCRIPTION_URL_PREFIX = "https://eprint.iacr.org/";
    private static final String FULLTEXT_URL_PREFIX = "https://eprint.iacr.org/";
    private static final String VERSION_URL_PREFIX = "https://eprint.iacr.org/archive/versions/";

    private final ImportFormatPreferences prefs;

    public IacrEprintFetcher(ImportFormatPreferences prefs) {
        this.prefs = prefs;
    }

    @Override
    public Optional<BibEntry> performSearchById(String identifier) throws FetcherException {
        String identifierWithoutLettersAndSpaces = WITHOUT_LETTERS_SPACE.matcher(identifier).replaceAll(" ").trim();

        if (!IDENTIFIER_PREDICATE.test(identifierWithoutLettersAndSpaces)) {
            throw new FetcherException(Localization.lang("Invalid identifier: '%0'.", identifier));
        }

        Optional<BibEntry> entry = createEntryFromIacrCitation(identifierWithoutLettersAndSpaces);

        if (entry.isPresent()) {
            setAdditionalFields(entry.get(), identifierWithoutLettersAndSpaces);
        }

        return entry;
    }

    private Optional<BibEntry> createEntryFromIacrCitation(String validIdentifier) throws FetcherException {
        URL url;
        try {
            url = URLUtil.create(CITATION_URL_PREFIX + validIdentifier);
        } catch (MalformedURLException e) {
            throw new FetcherException("Invalid URL", e);
        }
        String bibtexCitationHtml = getHtml(url);
        if (bibtexCitationHtml.contains("No such report found")) {
            throw new FetcherException(Localization.lang("No results found."));
        }
        String actualEntry = getRequiredValueBetween("<pre id=\"bibtex\">", "</pre>", bibtexCitationHtml);

        try {
            return BibtexParser.singleFromString(actualEntry, prefs);
        } catch (ParseException e) {
            throw new FetcherException(Localization.lang("Entry from %0 could not be parsed.", "IACR"), e);
        }
    }

    private void setAdditionalFields(BibEntry entry, String identifier) throws FetcherException {
        URL entryUrl;
        try {
            entryUrl = URLUtil.create(DESCRIPTION_URL_PREFIX + identifier);
        } catch (MalformedURLException e) {
            throw new FetcherException("Invalid URL", e);
        }

        String descriptiveHtml = getHtml(entryUrl);

        entry.setField(StandardField.ABSTRACT, getAbstract(descriptiveHtml));
        entry.setField(StandardField.DATE, getDate(descriptiveHtml));

        // Version information for entries after year 2000
        if (isFromOrAfterYear2000(entry)) {
            try {
                entryUrl = URLUtil.create(VERSION_URL_PREFIX + identifier);
            } catch (MalformedURLException e) {
                throw new FetcherException("Invalid URL", e);
            }
            String versionHtml = getHtml(entryUrl);
            String version = getVersion(identifier, versionHtml);
            entry.setField(StandardField.VERSION, version);
            entry.setField(StandardField.URL, entryUrl + "/" + version);
        }
    }

    private String getVersion(String identifier, String versionHtml) throws FetcherException {
        String startOfVersionString = "<li><a href=\"/archive/" + identifier + "/";
        String version = getRequiredValueBetween(startOfVersionString, "\">", versionHtml);
        return version;
    }

    private String getAbstract(String descriptiveHtml) throws FetcherException {
        String startOfAbstractString = "<h5 class=\"mt-3\">Abstract</h5>\n    <p style=\"white-space: pre-wrap;\">";
        String abstractText = getRequiredValueBetween(startOfAbstractString, "</p>", descriptiveHtml);
        return abstractText;
    }

    private String getDate(String descriptiveHtml) throws FetcherException {
        String startOfHistoryString = "<dt>History</dt>\n      \n      \n      <dd>";
        String dateStringAsInHtml = getRequiredValueBetween(startOfHistoryString, ":", descriptiveHtml);
        return dateStringAsInHtml;
    }

    private String getHtml(URL url) throws FetcherException {
        URLDownload download = new URLDownload(url);
        return download.asString();
    }

    private String getRequiredValueBetween(String from, String to, String haystack) throws FetcherException {
        String value = StringUtil.substringBetween(haystack, from, to);
        if (value == null) {
            throw new FetcherException(Localization.lang("Entry from %0 could not be parsed.", "IACR"));
        } else {
            return value;
        }
    }

    private boolean isFromOrAfterYear2000(BibEntry entry) throws FetcherException {
        Optional<String> yearField = entry.getField(StandardField.YEAR);
        if (yearField.isPresent()) {
            return Integer.parseInt(yearField.get()) > 2000;
        }
        throw new FetcherException(Localization.lang("Entry from %0 could not be parsed.", "IACR"));
    }

    @Override
    public String getName() {
        return NAME;
    }

    @Override
    public Optional<URL> findFullText(@NonNull BibEntry entry) throws IOException, FetcherException {
        Optional<String> urlField = entry.getField(StandardField.URL);
        if (urlField.isPresent()) {
            URL url;
            try {
                url = URLUtil.create(urlField.get());
            } catch (MalformedURLException e) {
                LOGGER.warn("Invalid URL {}", urlField.get(), e);
                return Optional.empty();
            }

            String descriptiveHtml = getHtml(url);
            String startOfFulltextLink = "<a class=\"btn btn-sm btn-outline-dark\"";
            String fulltextLinkAsInHtml = getRequiredValueBetween(startOfFulltextLink, ".pdf", descriptiveHtml);
            // There is an additional "\n           href=\"/archive/" we have to remove - and for some reason,
            // getRequiredValueBetween refuses to match across the line break.
            fulltextLinkAsInHtml = fulltextLinkAsInHtml.replaceFirst(".*href=\"/", "").trim();
            String fulltextLink = FULLTEXT_URL_PREFIX + fulltextLinkAsInHtml + ".pdf";
            return Optional.of(URLUtil.create(fulltextLink));
        }
        return Optional.empty();
    }

    @Override
    public TrustLevel getTrustLevel() {
        return TrustLevel.PREPRINT;
    }
}
