package jetbrains.mps.vcs.integration;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import com.intellij.openapi.vcs.checkout.CheckoutListener;
import org.jetbrains.annotations.NotNull;
import com.intellij.openapi.project.Project;
import java.nio.file.Path;
import java.nio.file.Files;
import jetbrains.mps.workbench.actions.OpenMPSProjectTrustProjectHelper;
import com.intellij.openapi.project.ex.ProjectManagerEx;
import com.intellij.ide.impl.OpenProjectTask;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;

@GeneratedClass(node = "r:eec25685-8f1e-47c9-a9de-4a7ef6b504ec(jetbrains.mps.vcs.integration)/5748651123750026353", model = "r:eec25685-8f1e-47c9-a9de-4a7ef6b504ec(jetbrains.mps.vcs.integration)")
public class ProjectDirMPSCheckoutListener implements CheckoutListener {
  @Override
  public boolean processCheckedOutDirectory(@NotNull final Project project, @NotNull final Path directory) {
    Path dotIdea = directory.resolve(Project.DIRECTORY_STORE_FOLDER);
    if (!(Files.exists(dotIdea))) {
      return false;
    }
    final Runnable runnable = new Runnable() {
      @Override
      public void run() {
        if (OpenMPSProjectTrustProjectHelper.checkTrust(directory)) {
          ProjectManagerEx.getInstanceEx().openProject(directory, OpenProjectTask.build().withProjectToClose(project));
        }
      }
    };
    final Application application = ApplicationManager.getApplication();
    if (application.isDispatchThread()) {
      runnable.run();
    } else {
      application.invokeAndWait(runnable);
    }
    return true;

  }
}
