/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed 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
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 com.google.cloud.tools.eclipse.integration.appengine;

import com.google.cloud.tools.eclipse.appengine.deploy.AppEngineDeployInfo;
import com.google.cloud.tools.eclipse.swtbot.SwtBotTestingUtilities;
import com.google.cloud.tools.eclipse.swtbot.SwtBotTimeoutManager;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;

import java.io.IOException;
import java.io.InputStream;

/**
 * Useful App Engine-related actions for Google Cloud Tools for Eclipse
 */
public class SwtBotAppEngineActions {

  /**
   * Create a native App Engine project using the
   * {@code com.google.cloud.tools.eclipse.appengine.newproject.AppEngineStandard} new wizard.
   * 
   * @param bot the bot
   * @param projectName the name of the project
   * @param location can be {@code null}
   * @param javaPackage can be {@code null} or empty
   * @param projectId can be {@code null}
   * @return the project
   */
  public static IProject createNativeWebAppProject(SWTWorkbenchBot bot, String projectName,
      String location, String javaPackage, String projectId) {
    bot.menu("File").menu("New").menu("Project...").click();

    SWTBotShell shell = bot.shell("New Project");
    shell.activate();

    bot.tree().expandNode("Google Cloud Platform")
        .select("Google App Engine Standard Java Project");
    bot.button("Next >").click();

    bot.textWithLabel("Project name:").setText(projectName);
    if (location == null) {
      bot.checkBox("Use default location").select();
    } else {
      bot.checkBox("Use default location").deselect();
      bot.textWithLabel("Location:").setText(location);
    }
    if (javaPackage != null) {
      bot.textWithLabel("Java package:").setText(javaPackage);
    }
    if (projectId != null) {
      bot.textWithLabel("App Engine Project ID: (optional)").setText(projectId);
    }
    SwtBotTestingUtilities.clickButtonAndWaitForWindowChange(bot, bot.button("Finish"));
    return getWorkspaceRoot().getProject(projectName);
  }

  /** Create a new project with the Maven-based Google App Engine Standard Java Project wizard */
  public static IProject createMavenWebAppProject(SWTWorkbenchBot bot, String location,
      String groupId, String artifactId, String javaPackage, String projectId,
      String archetypeDescription) {
    bot.menu("File").menu("New").menu("Project...").click();

    SWTBotShell shell = bot.shell("New Project");
    shell.activate();

    bot.tree().expandNode("Google Cloud Platform")
        .select("Maven-based Google App Engine Standard Java Project");
    bot.button("Next >").click();

    if (location == null) {
      bot.checkBox("Create project in workspace").select();
    } else {
      bot.checkBox("Create project in workspace").deselect();
      bot.textWithLabel("Location:").setText(location);
    }
    bot.textWithLabel("Group Id:").setText(groupId);
    bot.textWithLabel("Artifact Id:").setText(artifactId);
    if (javaPackage != null) {
      bot.textWithLabel("Java package:").setText(javaPackage);
    }
    if (projectId != null) {
      bot.textWithLabel("App Engine Project ID: (optional)").setText(projectId);
    }
    bot.button("Next >").click();
    // select an archetype; use the default
    if (archetypeDescription != null) {
      bot.list().select(archetypeDescription);
    }

    int mavenCompletionTimeout = 45000/* ms */; // can take a loooong time to fetch archetypes
    SwtBotTimeoutManager.setTimeout(mavenCompletionTimeout);
    SwtBotTestingUtilities.clickButtonAndWaitForWindowChange(bot, bot.button("Finish"));
    SwtBotTimeoutManager.resetTimeout();
    // this isn't right for location != null
    return getWorkspaceRoot().getProject(artifactId);
  }

  private static IWorkspaceRoot getWorkspaceRoot() {
    return ResourcesPlugin.getWorkspace().getRoot();
  }

  private SwtBotAppEngineActions() {}

  /**
   * Extracts the project ID from the given file.
   * 
   * @return the project ID or {@code null} if none found
   */
  public static String getAppEngineProjectId(IFile appEngineXml) throws IOException, CoreException {
    try (InputStream contents = appEngineXml.getContents()) {
      AppEngineDeployInfo info = new AppEngineDeployInfo();
      info.parse(contents);
      String projectId = info.getProjectId();
      if (projectId == null || projectId.trim().isEmpty()) {
        return null;
      }
      return projectId;
    }
  }
}

@@ -1172,24 +1172,130 @@
 rkbenchBot;%0A
+import org.eclipse.swtbot.swt.finder.SWTBot;%0Aimport org.eclipse.swtbot.swt.finder.waits.DefaultCondition;%0A
 import org.e
@@ -1340,16 +1340,16 @@
 tShell;%0A
-
 %0Aimport 
@@ -2928,32 +2928,60 @@
 h%22));%0A    return
+ waitUntilProjectExists(bot,
  getWorkspaceRoo
@@ -3003,24 +3003,25 @@
 projectName)
+)
 ;%0A  %7D%0A%0A  /**
@@ -4629,101 +4629,568 @@
     
-// this isn't right for location != null%0A
+return waitUntilProjectExists(bot, getWorkspaceRoot().getProject(artifactId));%0A  %7D%0A%0A%0A%0A  /**%0A   * Spin until the given project actually exists.%0A   * %0A   * @return the project%0A   */%0A  private static IProject waitUntilProjectExists(SWTBot bot, final IProject project) %7B%0A    bot.waitUntil(new DefaultCondition() %7B%0A      @Override%0A      public String getFailureMessage() %7B%0A   
     
+ 
 return 
-getWorkspaceRoot().getProject(artifactId)
+%22Project does not exist! %22 + project;%0A      %7D%0A%0A      @Override%0A      public boolean test() throws Exception %7B%0A        return project.exists();%0A      %7D%0A    %7D);%0A    return project
 ;%0A  

Change SwtBotAppEngineActions to wait for a new project to exist (#587)