/**
 * Copyright 2005-2015 Red Hat, Inc.
 * <p>
 * Red Hat licenses this file to you under the Apache License, version
 * 2.0 (the "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */
package io.fabric8.forge.devops;

import io.fabric8.forge.addon.utils.ProfilesProjectHelper;
import io.fabric8.forge.addon.utils.StopWatch;
import io.fabric8.forge.devops.setup.Fabric8SetupStep;
import io.fabric8.forge.devops.setup.SetupProjectHelper;
import org.jboss.forge.addon.projects.Project;
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UIExecutionContext;
import org.jboss.forge.addon.ui.context.UINavigationContext;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.result.NavigationResult;
import org.jboss.forge.addon.ui.result.Result;
import org.jboss.forge.addon.ui.result.Results;
import org.jboss.forge.addon.ui.result.navigation.NavigationResultBuilder;
import org.jboss.forge.addon.ui.util.Categories;
import org.jboss.forge.addon.ui.util.Metadata;
import org.jboss.forge.addon.ui.wizard.UIWizard;

import static io.fabric8.forge.devops.setup.Fabric8SetupStep.setupSitePlugin;

public class DevOpsEditCommand extends AbstractDevOpsCommand implements UIWizard {

    private volatile boolean needFabric8Setup;

    @Override
    public UICommandMetadata getMetadata(UIContext context) {
        return Metadata.forCommand(getClass())
                .category(Categories.create(AbstractDevOpsCommand.CATEGORY))
                .name(AbstractDevOpsCommand.CATEGORY + ": Edit")
                .description("Edit the DevOps configuration for this project");
    }

    @Override
    public void initializeUI(UIBuilder builder) throws Exception {
        StopWatch watch = new StopWatch();
        try {
            Project project = getSelectedProject(builder.getUIContext());
            log.info("initializeUI#getSelectedProject taken " + watch.taken());
            if (project != null) {
                setupSitePlugin(project);
                log.info("initializeUI#setupSitePlugin taken " + watch.taken());

                if (ProfilesProjectHelper.isProfilesProject(project)) {
                    // TODO: in the future we might want to verify the setup of a profiles project here.
                } else if (!SetupProjectHelper.fabric8ProjectSetupCorrectly(project)) {
                    needFabric8Setup = true;
                }
                log.info("initializeUI#fabric8ProjectSetupCorrectly taken " + watch.taken());
            }
        } catch (IllegalStateException e) {
            // ignore lack of project
        }
        log.info("initializeUI took " + watch.taken());
    }

    @Override
    public NavigationResult next(UINavigationContext context) throws Exception {
        NavigationResultBuilder builder = NavigationResultBuilder.create();
        if (needFabric8Setup) {
            builder.add(Fabric8SetupStep.class);
        }
        builder.add(DevOpsEditStep.class);
        builder.add(SaveDevOpsStep.class);
        return builder.build();
    }

    @Override
    public Result execute(UIExecutionContext context) throws Exception {
        return Results.success();
    }
}

@@ -676,60 +676,55 @@
 ort 
-io.fabric8.forge.addon.utils.ProfilesProjectHelper
+java.io.File;%0Aimport java.io.FileInputStream
 ;%0A
+%0A
 impo
@@ -857,78 +857,26 @@
 ps.s
-etup.SetupProjectHelper;%0Aimport org.jboss.forge.addon.projects.Project
+pringboot.IOHelper
 ;%0Aim
@@ -1533,87 +1533,8 @@
 d;%0A%0A
-import static io.fabric8.forge.devops.setup.Fabric8SetupStep.setupSitePlugin;%0A%0A
 publ
@@ -1658,16 +1658,23 @@
 ic8Setup
+ = true
 ;%0A%0A    @
@@ -2143,16 +2143,1285 @@
 Watch();
+%0A%0A        // load maven pom.xml%0A        File rootFolder = getSelectionFolder(builder.getUIContext());%0A        if (rootFolder != null) %7B%0A            File pom = new File(rootFolder, %22pom.xml%22);%0A            if (pom.exists() && pom.isFile()) %7B%0A                String text = IOHelper.loadText(new FileInputStream(pom));%0A                if (text.contains(%22fabric8-profiles%22)) %7B%0A                    // no need for fabric8 for fuse projects%0A                    needFabric8Setup = false;%0A                %7D else if (text.contains(%22io.fabric8.funktion%22)) %7B%0A                    // no need for fabric8 for funktion projects%0A                    needFabric8Setup = false;%0A                %7D else if (text.contains(%22fabric8-maven-plugin%22)) %7B%0A                    // no need for fabric8 if we have f-m-p plugin%0A                    needFabric8Setup = false;%0A                %7D%0A            %7D%0A        %7D%0A%0A        log.info(%22Need fabric8 setup? %22 + needFabric8Setup);%0A%0A        log.info(%22initializeUI took %22 + watch.taken());%0A    %7D%0A%0A    // the following old code is slow so we optimize in a different way%0A    // https://github.com/fabric8io/fabric8-forge/issues/704%0A    /*%0A    @Override%0A    public void initializeUI(UIBuilder builder) throws Exception %7B%0A        StopWatch watch = new StopWatch();
 %0A       
@@ -4320,29 +4320,31 @@
 h.taken());%0A
-
     %7D
+*/
 %0A%0A    @Overr

Optimize devops-edit command to be faster