package org.jetbrains.plugins.github;

import com.intellij.notification.NotificationType;
import com.intellij.openapi.components.ServiceManager;
import git4idea.commands.Git;
import org.jetbrains.plugins.github.api.GithubApiUtil;
import org.jetbrains.plugins.github.api.GithubConnection;
import org.jetbrains.plugins.github.api.GithubRepoDetailed;
import org.jetbrains.plugins.github.util.GithubAuthData;

import java.io.IOException;

import static com.intellij.openapi.vcs.Executor.cd;
import static git4idea.test.GitExecutor.git;

/**
 * @author Aleksey Pivovarov
 */
public class GithubShareProjectTest extends GithubShareProjectTestBase {

  public void testSimple() throws Throwable {
    registerDefaultShareDialogHandler();
    registerDefaultUntrackedFilesDialogHandler();

    createProjectFiles();

    GithubShareAction.shareProjectOnGithub(myProject, myProjectRoot);

    checkNotification(NotificationType.INFORMATION, "Successfully shared project on GitHub", null);
    initGitChecks();
    checkGitExists();
    checkGithubExists();
    checkRemoteConfigured();
    checkLastCommitPushed();
  }

  public void testGithubAlreadyExists() throws Throwable {
    registerDefaultShareDialogHandler();
    registerDefaultUntrackedFilesDialogHandler();

    createProjectFiles();
    GithubShareAction.shareProjectOnGithub(myProject, myProjectRoot);
    GithubShareAction.shareProjectOnGithub(myProject, myProjectRoot);

    checkNotification(NotificationType.INFORMATION, "Project is already on GitHub", null);
  }

  public void testExistingGit() throws Throwable {
    registerDefaultShareDialogHandler();
    registerDefaultUntrackedFilesDialogHandler();

    createProjectFiles();

    cd(myProjectRoot.getPath());
    git("init");
    setGitIdentity(myProjectRoot);
    git("add file.txt");
    git("commit -m init");

    GithubShareAction.shareProjectOnGithub(myProject, myProjectRoot);

    checkNotification(NotificationType.INFORMATION, "Successfully shared project on GitHub", null);
    initGitChecks();
    checkGitExists();
    checkGithubExists();
    checkRemoteConfigured();
    checkLastCommitPushed();
  }

  public void testExistingFreshGit() throws Throwable {
    registerDefaultShareDialogHandler();
    registerDefaultUntrackedFilesDialogHandler();

    createProjectFiles();

    Git git = ServiceManager.getService(Git.class);
    git.init(myProject, myProjectRoot);

    GithubShareAction.shareProjectOnGithub(myProject, myProjectRoot);

    checkNotification(NotificationType.INFORMATION, "Successfully shared project on GitHub", null);
    initGitChecks();
    checkGitExists();
    checkGithubExists();
    checkRemoteConfigured();
    checkLastCommitPushed();
  }

  public void testEmptyProject() throws Throwable {
    registerDefaultUntrackedFilesDialogHandler();
    registerDefaultShareDialogHandler();

    GithubShareAction.shareProjectOnGithub(myProject, myProjectRoot);

    checkNotification(NotificationType.INFORMATION, "Successfully created empty repository on GitHub", null);
    initGitChecks();
    checkGitExists();
    checkGithubExists();
    checkRemoteConfigured();
  }

  protected void checkGithubExists() throws IOException {
    GithubAuthData auth = myGitHubSettings.getAuthData();
    GithubRepoDetailed githubInfo = GithubApiUtil.getDetailedRepoInfo(new GithubConnection(auth), myLogin1, PROJECT_NAME);
    assertNotNull("GitHub repository does not exist", githubInfo);
  }
}

@@ -138,16 +138,100 @@
 anager;%0A
+import com.intellij.openapi.ui.Messages;%0Aimport com.intellij.openapi.ui.TestDialog;%0A
 import g
@@ -1239,32 +1239,384 @@
 ows Throwable %7B%0A
+    final boolean%5B%5D dialogShown = new boolean%5B1%5D;%0A    TestDialog oldTestDialog = Messages.setTestDialog(new TestDialog() %7B%0A      @Override%0A      public int show(String message) %7B%0A        dialogShown%5B0%5D = message.contains(%22Successfully connected to%22) && message.contains(%22Do you want to proceed anyway?%22);%0A        return 1;%0A      %7D%0A    %7D);%0A%0A    try %7B%0A  
     registerDefa
@@ -1636,32 +1636,34 @@
 gHandler();%0A    
+ 
+ 
 registerDefaultU
@@ -1689,32 +1689,34 @@
 Handler();%0A%0A    
+  
 createProjectFil
@@ -1717,24 +1717,26 @@
 ectFiles();%0A
+  
     GithubSh
@@ -1789,24 +1789,62 @@
 ojectRoot);%0A
+      assertFalse(dialogShown%5B0%5D);%0A%0A  
     GithubSh
@@ -1905,99 +1905,112 @@
 t);%0A
-%0A
+  
     
-checkNotification(NotificationType.INFORMATION, %22Project is already on GitHub%22, null);
+assertTrue(dialogShown%5B0%5D);%0A    %7D%0A    finally %7B%0A      Messages.setTestDialog(oldTestDialog);%0A    %7D
 %0A  %7D

fix test