package name.abuchen.portfolio.ui.handlers;

import java.io.File;
import java.util.Optional;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Named;

import org.eclipse.e4.core.di.annotations.CanExecute;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.services.IServiceConstants;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;

import name.abuchen.portfolio.model.Account;
import name.abuchen.portfolio.model.Client;
import name.abuchen.portfolio.model.Portfolio;
import name.abuchen.portfolio.ui.Messages;
import name.abuchen.portfolio.ui.editor.PortfolioPart;
import name.abuchen.portfolio.ui.wizards.datatransfer.CSVImportWizard;

public class ImportCSVHandler
{
    @CanExecute
    boolean isVisible(@Named(IServiceConstants.ACTIVE_PART) MPart part)
    {
        return MenuHelper.isClientPartActive(part);
    }

    @Execute
    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart part,
                    @Named(IServiceConstants.ACTIVE_SHELL) Shell shell)
    {
        MenuHelper.getActiveClient(part).ifPresent(client -> runImport((PortfolioPart) part.getObject(), shell, client, "", null, null)); //$NON-NLS-1$
    }
    
    public static void runImport(PortfolioPart part, Shell shell, Client client, String filterPath, Account account, Portfolio portfolio)
    {
        FileDialog fileDialog = new FileDialog(shell, SWT.OPEN);
        fileDialog.setFilterPath(filterPath);
        fileDialog.setFilterNames(new String[] { Messages.CSVImportLabelFileCSV, Messages.PDFImportFilterName, Messages.CSVImportLabelFileAll });
        fileDialog.setFilterExtensions(new String[] { "*.csv", "*.pdf", "*.*" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        String fileName = fileDialog.open();

        if (fileName == null)
            return;

        IPreferenceStore preferences = part.getPreferenceStore();
        CSVImportWizard wizard = new CSVImportWizard(client, preferences, new File(fileName));
        if (account != null)
            wizard.setTarget(account);
        if (portfolio != null)
            wizard.setTarget(portfolio);

        Dialog wizwardDialog = new WizardDialog(shell, wizard);
        wizwardDialog.open();
    }
}

@@ -1659,16 +1659,28 @@
 SWT.OPEN
+ %7C SWT.MULTI
 );%0A     
@@ -1990,16 +1990,44 @@
 -NLS-3$%0A
+        fileDialog.open();%0A%0A
         
@@ -2036,17 +2036,20 @@
 ring
+%5B%5D
  file
-N
+n
 ame
+s
  = f
@@ -2050,36 +2050,44 @@
 es = fileDialog.
-open
+getFileNames
 ();%0A%0A        if 
@@ -2095,40 +2095,205 @@
 file
-Name == null)%0A            return
+names.length == 0)%0A            return;%0A%0A        List%3CFile%3E files = new ArrayList%3C%3E();%0A        for (String filename : filenames)%0A            files.add(new File(fileDialog.getFilterPath(), filename))
 ;%0A%0A 
@@ -2353,24 +2353,75 @@
 nceStore();%0A
+%0A        for (File fileName : files)%0A        %7B%0A    
         CSVI
@@ -2482,25 +2482,16 @@
 rences, 
-new File(
 fileName
@@ -2491,19 +2491,22 @@
 ileName)
-)
 ;%0A
+    
         
@@ -2530,32 +2530,36 @@
 ll)%0A            
+    
 wizard.setTarget
@@ -2573,24 +2573,28 @@
 t);%0A        
+    
 if (portfoli
@@ -2608,32 +2608,36 @@
 ll)%0A            
+    
 wizard.setTarget
@@ -2646,24 +2646,28 @@
 ortfolio);%0A%0A
+    
         Dial
@@ -2726,16 +2726,20 @@
         
+    
 wizwardD
@@ -2748,20 +2748,30 @@
 log.open();%0A
+        %7D%0A
     %7D%0A%7D%0A

Introduce import of multiple CSV/PDF files