package org.jabref.logic.util;

import java.util.Arrays;
import java.util.List;

import org.jabref.model.util.OptionalUtil;

/// @implNote Enter the extensions in lowercase without a dot! The dot is added implicitly.
public enum StandardFileType implements FileType {

    ANY_FILE("Any", "*"),

    AUX("Aux file", "aux"),
    BIBTEX_DB("Bibtex library", "bib"),
    BST("BibTeX-Style file", "bst"),
    BLG("BibTeX log file", "blg"),

    CER("SSL Certificate", "cer"),
    CFF("CFF", "cff"),
    CITATION_STYLE("Citation Style", "csl"),
    CITAVI("Citavi", "ctv6bak", "ctv5bak"),
    CLASS("Class file", "class"),
    CSS("CSS Styleshet", "css"),
    CSV("CSV", "csv"),
    DOC("Microsoft Word", "doc", "docx"),
    ENDNOTE("Endnote", "ref", "enw"),
    EPS("Encapsulated PostScript", "eps"),
    EXCEL("Microsoft Excel", "xls", "xlsx"),
    HTML("HTML", "html", "htm"),
    IMAGE("Image", "png", "jpg", "jpeg", "gif", "bmp", "tiff", "webp"),
    ISI("Isi", "isi", "txt"),
    JAR("JAR", "jar"),
    JAVA_KEYSTORE("Java Keystore", "jks"),
    JSON("JSON", "json"),
    JSTYLE("JStyle", "jstyle"),
    LAYOUT("Custom Exporter format", "layout"),
    MARKDOWN("Markdown", "md"),
    MEDLINE("Medline", "nbib", "xml"),
    MEDLINE_PLAIN("Medline Plain", "nbib", "txt"),
    ODT("OpenDocument Text", "odt"),
    ODS("OpenOffice Calc", "ods"),
    PDF("PDF", "pdf"),
    POWERPOINT("Microsoft PowerPoint", "ppt", "pptx"),
    PS("PostScript", "ps"),
    PUBMED("Pubmed", "fcgi"),
    RDF("RDF", "rdf"),
    RIS("RIS", "ris"),
    RTF("RTF", "rtf"),
    SXC("Open Office Calc 1.x", "sxc"),
    TERMS("Protected terms", "terms"),
    TEX("LaTeX", "tex"),
    TXT("Plain Text", "txt"),
    XML("XML", "xml"),
    XMP("XMP", "xmp"),
    YAML("YAML Markup", "yml", "yaml"),
    ZIP("Zip Archive", "zip");

    private final List<String> extensions;
    private final String name;

    StandardFileType(String name, String... extensions) {
        this.extensions = Arrays.asList(extensions);
        this.name = name;
    }

    @Override
    public List<String> getExtensions() {
        return extensions;
    }

    @Override
    public String getName() {
        return this.name;
    }

    public static FileType fromExtensions(String... extensions) {
        List<String> exts = Arrays.asList(extensions);

        return OptionalUtil.orElse(Arrays.stream(StandardFileType.values())
                                         .filter(field -> field.getExtensions().stream().anyMatch(exts::contains))
                                         .findAny(),
                new UnknownFileType(extensions));
    }
}
