package at.zeha.netbeans.cliglue;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectManager;
import org.netbeans.api.project.ui.OpenProjects;
import org.netbeans.spi.project.ProjectConfiguration;
import org.netbeans.spi.project.ProjectConfigurationProvider;
import org.netbeans.api.sendopts.CommandException;
import org.netbeans.spi.sendopts.Env;
import org.netbeans.spi.sendopts.OptionProcessor;
import org.netbeans.spi.sendopts.Option;
import org.openide.LifecycleManager;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.lookup.ServiceProvider;
import org.openide.windows.WindowManager;

@ServiceProvider(service = OptionProcessor.class)
public class AllConfigurationsOpenerCommandLineProcessor extends OptionProcessor {

    private static Option action = Option.requiredArgument(Option.NO_SHORT_NAME, "openallconfigs");
    private static final Logger logger = Logger.getLogger(AllConfigurationsOpenerCommandLineProcessor.class.getName());

    @Override
    public Set<org.netbeans.spi.sendopts.Option> getOptions() {
        return Collections.singleton(action);
    }

    @Override
    protected void process(Env env, Map<Option, String[]> values) throws CommandException {
        final String[] args = values.get(action);
        if (args.length > 0) {
            WindowManager.getDefault().invokeWhenUIReady(new Runnable() {
                @Override
                public void run() {
                    try {
                        ProjectManager manager = ProjectManager.getDefault();
                        manager.clearNonProjectCache();
                        String projectFilePath = args[0];
                        File projectFile = new File(projectFilePath);
                        FileObject projectToBeOpened = FileUtil.toFileObject(projectFile);
                        Project project = ProjectManager.getDefault().findProject(projectToBeOpened);
                        Project[] array = new Project[1];
                        array[0] = project;

                        OpenProjects OpenProjectsInstance = OpenProjects.getDefault();

                        Project[] currentOpenProjects = OpenProjectsInstance.openProjects().get();

                        logger.log(Level.ALL, "Closing existing projects...");
                        OpenProjectsInstance.close(currentOpenProjects);
                        // wait for close to complete
                        currentOpenProjects = OpenProjectsInstance.openProjects().get();

                        logger.log(Level.ALL, "Opening project...");
                        OpenProjectsInstance.open(array, false);
                        // wait for open to complete
                        currentOpenProjects = OpenProjectsInstance.openProjects().get();

                        project = currentOpenProjects[0];
                        ProjectConfigurationProvider configProvider = project.getLookup().lookup(ProjectConfigurationProvider.class);
                        Collection<ProjectConfiguration> configurations = configProvider.getConfigurations();
                        for (Object c : configurations.toArray()) {
                            logger.log(Level.ALL, "Activating configuration \"" + ((ProjectConfiguration) c).getDisplayName() + "\" ...");
                            configProvider.setActiveConfiguration((ProjectConfiguration) c);
                        }

                        logger.log(Level.ALL, "Closing opened projects...");
                        OpenProjectsInstance.close(currentOpenProjects);
                        // wait for close to complete
                        currentOpenProjects = OpenProjectsInstance.openProjects().get();
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                        logger.log(Level.ALL, ex.getStackTrace().toString());
                    } catch (ExecutionException ex) {
                        ex.printStackTrace();
                        logger.log(Level.ALL, ex.getStackTrace().toString());
                    } catch (IOException ex) {
                        ex.printStackTrace();
                        logger.log(Level.ALL, ex.getStackTrace().toString());
                    }

                    logger.log(Level.ALL, "Shutting down.");
                    LifecycleManager.getDefault().exit();
                }
            });
        }
    }
}

@@ -2448,80 +2448,33 @@
     
-Project%5B%5D currentOpenProjects = OpenProjectsInstance.openProjects().get(
+logger.setLevel(Level.ALL
 );%0A%0A
@@ -2506,35 +2506,36 @@
 ogger.log(Level.
-ALL
+INFO
 , %22Closing exist
@@ -2600,32 +2600,46 @@
 tance.close(
-currentO
+OpenProjectsInstance.o
 penProjects)
@@ -2629,32 +2629,40 @@
 nce.openProjects
+().get()
 );%0A             
@@ -2725,38 +2725,16 @@
         
- currentOpenProjects =
  OpenPro
@@ -2760,32 +2760,75 @@
 rojects().get();
+%0A                        Thread.sleep(500);
 %0A%0A              
@@ -2854,19 +2854,20 @@
 g(Level.
-ALL
+INFO
 , %22Openi
@@ -3024,38 +3024,16 @@
         
- currentOpenProjects =
  OpenPro
@@ -3063,24 +3063,67 @@
 cts().get();
+%0A                        Thread.sleep(500);
 %0A%0A          
@@ -3146,24 +3146,38 @@
 oject = 
-currentO
+OpenProjectsInstance.o
 penProje
@@ -3179,16 +3179,24 @@
 Projects
+().get()
 %5B0%5D;%0A   
@@ -3549,19 +3549,20 @@
 g(Level.
-ALL
+INFO
 , %22Activ
@@ -3737,16 +3737,63 @@
 on) c);%0A
+                            Thread.sleep(500);%0A
         
@@ -3852,19 +3852,20 @@
 g(Level.
-ALL
+INFO
 , %22Closi
@@ -3881,32 +3881,75 @@
  projects...%22);%0A
+                        Thread.sleep(500);%0A
                 
@@ -3983,24 +3983,38 @@
 e.close(
-currentO
+OpenProjectsInstance.o
 penProje
@@ -4016,16 +4016,24 @@
 Projects
+().get()
 );%0A     
@@ -4108,30 +4108,8 @@
     
- currentOpenProjects =
  Ope
@@ -4283,35 +4283,38 @@
 ogger.log(Level.
-ALL
+SEVERE
 , ex.getStackTra
@@ -4464,35 +4464,38 @@
 ogger.log(Level.
-ALL
+SEVERE
 , ex.getStackTra
@@ -4646,19 +4646,277 @@
 g(Level.
-ALL
+SEVERE, ex.getStackTrace().toString());%0A                    %7D catch (Exception ex) %7B%0A                        // catch all exceptions here so IDE shutdown works anyway.%0A                        ex.printStackTrace();%0A                        logger.log(Level.SEVERE
 , ex.get
@@ -5001,19 +5001,20 @@
 g(Level.
-ALL
+INFO
 , %22Shutt

fix logging, resilience