package de.renew.navigator.io;

import java.io.File;
import java.util.Arrays;
import java.util.Collection;

import org.apache.log4j.Logger;

import CH.ifa.draw.IOHelper;
import CH.ifa.draw.io.CombinationFileFilter;
import CH.ifa.draw.io.ImportHolder;
import CH.ifa.draw.io.SimpleFileFilter;
import CH.ifa.draw.io.importFormats.ImportFormat;
import CH.ifa.draw.io.importFormats.ImportFormatMultiAbstract;
import de.renew.ioontology.ExtensionFileFilter;

/**
 * @author Konstantin Simon Maria Moellers
 * @version 2015-10-07
 * @deprecated This class is not to be used externally anymore.
 * For usages within the Navigator plugin, use the type {@link de.renew.navigator.io.internal.FileFilterHandler} instead.
 */
@Deprecated(since = "5.0", forRemoval = true)
@SuppressWarnings("removal")
public class FileFilterBuilderImpl implements FileFilterBuilder {
    private static final Logger LOGGER = Logger.getLogger(FileFilterBuilderImpl.class);

    private final IOHelper _ioHelper;
    private final ImportHolder _importHolder;

    /**
     * Constructor.
     *
     * @param helperIO Helper to handle IO operations.
     * @param holderImport Holder of import formats.
     */
    public FileFilterBuilderImpl(IOHelper helperIO, ImportHolder holderImport) {
        this._ioHelper = helperIO;
        this._importHolder = holderImport;
    }

    @Override
    public CombinationFileFilter buildFileFilter() {
        final CombinationFileFilter fileFilter = _ioHelper.getFileFilter();

        for (ImportFormat format : _importHolder.allImportFormats()) {
            boolean added = addImportFormat(fileFilter, format);
            if (!added) {
                throw new RuntimeException("Unsupported import format: " + format.getClass());
            }
        }


        // Add file filters of external file types.
        fileFilter.addAll(getExternalFileTypes());
        fileFilter.allowDirectory(true);
        fileFilter.allowHidden(false);
        return fileFilter;
    }

    @Override
    public boolean isExternallyOpenedFile(File file) {
        CombinationFileFilter filter = new CombinationFileFilter("temp");
        filter.addAll(getExternalFileTypes());
        return filter.accept(file);
    }


    /**
     * Adds an import format to the accepted file types of the navigator.
     *
     * @param format import format to add
     * @param target target to add the import format to
     * @return true if the format was added/handled, false if unsupported
     */
    private static boolean addImportFormat(CombinationFileFilter target, ImportFormat format) {
        // Load each format of a multi format.
        if (format instanceof ImportFormatMultiAbstract multiFormat) {
            ImportFormat[] allImportFormats = multiFormat.allImportFormats();
            for (ImportFormat importFormat2 : allImportFormats) {
                if (!addImportFormat(target, importFormat2)) {
                    return false;
                }
            }
            return true;
        }

        // Load a simple file filter coming from our own ExtensionFileFilter.
        if (format.fileFilter() instanceof ExtensionFileFilter) {
            target.add((ExtensionFileFilter) format.fileFilter());
            return true;
        }

        // Accept CH.ifa.draw.io.SimpleFileFilter as well (third-party import formats).
        if (format.fileFilter() instanceof SimpleFileFilter) {
            target.add((SimpleFileFilter) format.fileFilter());
            return true;
        }

        LOGGER.warn("Skipping unsupported import format: " + format.getClass());
        return false;
    }

    /**
     * This method returns a collection of all file filters that are opened externally.
     *
     * @return collection of all file filters which are opened externally
     */
    private Collection<SimpleFileFilter> getExternalFileTypes() {
        return Arrays.asList(
            new SimpleFileFilter("kb", "Knowledge Base"),
            new SimpleFileFilter("java", "Java Sourcecode"),
            new SimpleFileFilter("jsp", "Java Servlet Pages"),
            new SimpleFileFilter("xml", "Extensible Markup Language"),
            new SimpleFileFilter("xmi", "XML Metadata Interchange"),
            new SimpleFileFilter("md", "Markdown"), new SimpleFileFilter("tex", "LaTeX file"),
            new SimpleFileFilter("html", "Hypertext"), new SimpleFileFilter("cfg", "Configuration"),
            new SimpleFileFilter("gif", "Graphics Interchange Format"),
            new SimpleFileFilter("png", "Portable Network Graphics"),
            new SimpleFileFilter("jpg", "Joint Photographic experts Group"),
            new SimpleFileFilter("jpeg", "Joint Photographic Experts Group"));
    }
}