/*
 * Copyright 2000-2015 JetBrains s.r.o.
 *
 * 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.intellij.testFramework;

import com.intellij.ide.highlighter.ModuleFileType;
import com.intellij.openapi.application.AccessToken;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.components.ComponentsPackage;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.module.ModuleType;
import com.intellij.openapi.module.StdModuleTypes;
import com.intellij.openapi.module.impl.ModuleImpl;
import com.intellij.openapi.project.ex.ProjectEx;
import com.intellij.openapi.project.impl.ProjectImpl;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileVisitor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

public abstract class ModuleTestCase extends IdeaTestCase {
  protected final Collection<Module> myModulesToDispose = new ArrayList<Module>();

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    myModulesToDispose.clear();
  }

  @Override
  protected void tearDown() throws Exception {
    try {
      final ModuleManager moduleManager = ModuleManager.getInstance(myProject);
      ApplicationManager.getApplication().runWriteAction(new Runnable() {
        @Override
        public void run() {
          for (Module module : myModulesToDispose) {
            String moduleName = module.getName();
            if (moduleManager.findModuleByName(moduleName) != null) {
              moduleManager.disposeModule(module);
            }
          }
        }
      });
    }
    finally {
      myModulesToDispose.clear();
      super.tearDown();
    }
  }

  protected Module createModule(final File moduleFile) {
    return createModule(moduleFile, StdModuleTypes.JAVA);
  }

  protected Module createModule(final File moduleFile, final ModuleType moduleType) {
    final String path = moduleFile.getAbsolutePath();
    return createModule(path, moduleType);
  }

  protected Module createModule(final String path, final ModuleType moduleType) {
    Module module = ApplicationManager.getApplication().runWriteAction(
      new Computable<Module>() {
        @Override
        public Module compute() {
          return ModuleManager.getInstance(myProject).newModule(path, moduleType.getId());
        }
      }
    );

    myModulesToDispose.add(module);
    return module;
  }

  protected Module loadModule(final File moduleFile) {
    return loadModule(moduleFile, false);
  }

  protected Module loadModule(final File moduleFile, final boolean loadComponentStates) {
    Module module = ApplicationManager.getApplication().runWriteAction(
      new Computable<Module>() {
        @Override
        public Module compute() {
          ProjectImpl project = (ProjectImpl)myProject;
          boolean oldOptimiseTestLoadSpeed = project.isOptimiseTestLoadSpeed();
          if (loadComponentStates) {
            project.setOptimiseTestLoadSpeed(false);
          }
          try {
            LocalFileSystem.getInstance().refreshIoFiles(Collections.singletonList(moduleFile));
            return ModuleManager.getInstance(myProject).loadModule(moduleFile.getAbsolutePath());
          }
          catch (Exception e) {
            LOG.error(e);
            return null;
          }
          finally {
            project.setOptimiseTestLoadSpeed(oldOptimiseTestLoadSpeed);
          }
        }
      }
    );

    myModulesToDispose.add(module);
    return module;
  }

  protected Module loadModule(final String modulePath) {
    return loadModule(new File(modulePath));
  }

  @Nullable
  protected Module loadAllModulesUnder(@NotNull VirtualFile rootDir) throws Exception {
    final Ref<Module> result = Ref.create();

    VfsUtilCore.visitChildrenRecursively(rootDir, new VirtualFileVisitor() {
      @Override
      public boolean visitFile(@NotNull VirtualFile file) {
        if (!file.isDirectory() && file.getName().endsWith(ModuleFileType.DOT_DEFAULT_EXTENSION)) {
          ModuleImpl module = (ModuleImpl)loadModule(new File(file.getPath()));
          readJdomExternalizables(module);
          result.setIfNull(module);
        }
        return true;
      }
    });

    return result.get();
  }

  protected void readJdomExternalizables(@NotNull Module module) {
    loadModuleComponentState(module, ModuleRootManager.getInstance(module));
  }

  protected final void loadModuleComponentState(@NotNull Module module, @NotNull Object component) {
    AccessToken token = WriteAction.start();
    try {
      ProjectEx project = (ProjectEx)myProject;
      project.setOptimiseTestLoadSpeed(false);
      ComponentsPackage.getStateStore(module).initComponent(component, false);
      project.setOptimiseTestLoadSpeed(true);
    }
    finally {
      token.finish();
    }
  }

  protected Module createModuleFromTestData(final String dirInTestData, final String newModuleFileName, final ModuleType moduleType,
                                            final boolean addSourceRoot)
    throws IOException {
    final File dirInTestDataFile = new File(dirInTestData);
    assertTrue(dirInTestDataFile.isDirectory());
    final File moduleDir = createTempDirectory();
    FileUtil.copyDir(dirInTestDataFile, moduleDir);
    final Module module = createModule(moduleDir + "/" + newModuleFileName, moduleType);
    final VirtualFile root = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(moduleDir);
    assertNotNull(root);
    new WriteCommandAction.Simple(module.getProject()) {
      @Override
      protected void run() throws Throwable {
        root.refresh(false, true);
      }
    }.execute().throwException();
    if (addSourceRoot) {
      PsiTestUtil.addSourceContentToRoots(module, root);
    }
    else {
      PsiTestUtil.addContentRoot(module, root);
    }
    return module;
  }
}

@@ -728,131 +728,25 @@
 on.A
-ccessToken;%0Aimport com.intellij.openapi.application.ApplicationManager;%0Aimport com.intellij.openapi.application.WriteAction
+pplicationManager
 ;%0Aim
@@ -5500,65 +5500,8 @@
 ) %7B%0A
-    AccessToken token = WriteAction.start();%0A    try %7B%0A  
     
@@ -5542,26 +5542,24 @@
 roject;%0A    
- 
- 
 project.setO
@@ -5583,24 +5583,34 @@
 eed(false);%0A
+    try %7B%0A
       Compon
@@ -5672,24 +5672,44 @@
 nt, false);%0A
+    %7D%0A    finally %7B%0A
       projec
@@ -5752,50 +5752,8 @@
   %7D%0A
-    finally %7B%0A      token.finish();%0A    %7D%0A
   %7D%0A

write action is not needed here, ensure that we setOptimiseTestLoadSpeed to true again