package org.helioviewer.jhv.gui;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.swing.TransferHandler;

import org.helioviewer.jhv.io.FileUtils;
import org.helioviewer.jhv.io.Load;
import org.helioviewer.jhv.log.Log;

import org.apache.commons.validator.routines.UrlValidator;

@SuppressWarnings("serial")
class DropHandler extends TransferHandler {

    @Override
    public boolean canImport(TransferHandler.TransferSupport support) {
        Transferable transferable = support.getTransferable();
        return support.isDrop() && (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor) ||
                transferable.isDataFlavorSupported(DataFlavor.stringFlavor));
    }

    @Override
    public boolean importData(TransferHandler.TransferSupport support) {
        if (!canImport(support))
            return false;

        try {
            Transferable transferable = support.getTransferable();
            if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                List<?> objects = (List<?>) transferable.getTransferData(DataFlavor.javaFileListFlavor);

                List<URI> imageUris = new ArrayList<>(objects.size());
                List<URI> jsonUris = new ArrayList<>(objects.size());
                List<URI> cdfUris = new ArrayList<>(objects.size());
                for (Object o : objects) {
                    if (o instanceof File f) {
                        if (f.isFile() && f.canRead()) {
                            URI uri = f.toURI();
                            String loc = uri.toString().toLowerCase(Locale.ENGLISH);
                            if (loc.endsWith(".json"))
                                jsonUris.add(uri);
                            else if (loc.endsWith(".cdf"))
                                cdfUris.add(uri);
                            else
                                imageUris.add(uri);
                        } else if (f.isDirectory()) {
                            try {
                                for (URI uri : FileUtils.listDir(f.toPath())) {
                                    String loc = uri.toString().toLowerCase(Locale.ENGLISH);
                                    if (loc.endsWith(".json"))
                                        jsonUris.add(uri);
                                    else if (loc.endsWith(".cdf"))
                                        cdfUris.add(uri);
                                    else
                                        imageUris.add(uri);
                                }
                            } catch (Exception e) {
                                Log.error("Error reading directory " + f, e);
                            }
                        }
                    }
                }

                // jsonUris.forEach(Load.request::get);
                jsonUris.forEach(Load.sunJSON::get);
                Load.CDF.getAll(cdfUris);
                Load.Image.getAll(imageUris);

                return true;
            } else if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                String loc = (String) transferable.getTransferData(DataFlavor.stringFlavor);

                if (UrlValidator.getInstance().isValid(loc)) {
                    URI uri = new URI(loc);
                    if (loc.endsWith(".json"))
                        Load.sunJSON.get(uri);
                    else if (loc.endsWith(".cdf"))
                        Load.cdf.get(uri);
                    else if (loc.endsWith(".jhv"))
                        Load.state.get(uri);
                    else
                        Load.image.get(uri);

                    return true;
                }
            }
        } catch (Exception e) {
            Log.error("Import error", e);
        }

        return false;
    }

}

@@ -849,24 +849,380 @@
 r));%0A    %7D%0A%0A
+    private static void classify(URI uri, List%3CURI%3E imageUris, List%3CURI%3E jsonUris, List%3CURI%3E cdfUris) %7B%0A        String loc = uri.toString().toLowerCase(Locale.ENGLISH);%0A        if (loc.endsWith(%22.json%22))%0A            jsonUris.add(uri);%0A        else if (loc.endsWith(%22.cdf%22))%0A            cdfUris.add(uri);%0A        else%0A            imageUris.add(uri);%0A    %7D%0A%0A
     @Overrid
@@ -2017,411 +2017,56 @@
     
-URI uri = f.toURI();%0A                            String loc = uri.toString().toLowerCase(Locale.ENGLISH);%0A                            if (loc.endsWith(%22.json%22))%0A                                jsonUris.add(uri);%0A                            else if (loc.endsWith(%22.cdf%22))%0A                                cdfUris.add(uri);%0A                            else%0A                                imageUris.add(uri
+classify(f.toURI(), imageUris, jsonUris, cdfUris
 );%0A 
@@ -2187,23 +2187,8 @@
     
- for (URI uri :
  Fil
@@ -2217,486 +2217,69 @@
 h())
-) %7B%0A                                    String loc = uri.toString().toLowerCase(Locale.ENGLISH);%0A                                    if (loc.endsWith(%22.json%22))%0A                                        jsonUris.add(uri);%0A                                    else if (loc.endsWith(%22.cdf%22))%0A                                        cdfUris.add(uri);%0A                                    else%0A                                        imageUris.add(uri);%0A                                %7D
+.forEach(uri -%3E classify(uri, imageUris, jsonUris, cdfUris));
 %0A   

Reduce repetition