package jetbrains.mps.ide.findusages.model.scopes;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import java.util.Map;
import org.jetbrains.mps.openapi.module.SModuleReference;
import org.jetbrains.mps.openapi.module.SModule;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.Collection;
import jetbrains.mps.project.dependency.GlobalModuleDependenciesManager;
import java.util.HashMap;
import org.jdom.Element;
import jetbrains.mps.project.Project;
import jetbrains.mps.ide.findusages.CantLoadSomethingException;
import org.jetbrains.mps.openapi.module.SRepository;
import java.util.List;
import java.util.ArrayList;
import jetbrains.mps.logging.Logger;
import org.jetbrains.mps.openapi.persistence.PersistenceFacade;
import jetbrains.mps.ide.findusages.CantSaveSomethingException;

@GeneratedClass(node = "r:3189948c-a57a-415e-beba-405572f2d13c(jetbrains.mps.ide.findusages.model.scopes)/8568892084424435948", model = "r:3189948c-a57a-415e-beba-405572f2d13c(jetbrains.mps.ide.findusages.model.scopes)")
public class ModulesScope extends FindUsagesScope {
  private static final String MODULE_ID = "ref";
  private static final String MODULE_TAG = "module";
  private boolean myRespectVisible = false;
  private Map<SModuleReference, SModule> myResolveScope;
  public ModulesScope(@NotNull Iterable<? extends SModule> modules) {
    for (SModule module : modules) {
      if (module != null) {
        addModule(module);
      }
    }
  }
  public ModulesScope(@NotNull SModule... modules) {
    this(Arrays.asList(modules));
  }
  /**
   * Tells whether to use only specified modules as reference resolution scope for {@link #resolve(org.jetbrains.mps.openapi.module.SModuleReference)},
   * or all visible/accessible modules shall be considered
   */
  public void resolveRespectsAllVisible(boolean respectVisible) {
    myRespectVisible = respectVisible;
  }
  @Override
  public SModule resolve(@NotNull SModuleReference reference) {
    if (myResolveScope == null) {
      final Collection<SModule> allVisible;
      if (myRespectVisible) {
        allVisible = new GlobalModuleDependenciesManager(myModules).getModules(GlobalModuleDependenciesManager.Deptype.VISIBLE);
      } else {
        allVisible = myModules;
      }
      myResolveScope = new HashMap<SModuleReference, SModule>(allVisible.size() * 4 / 3);
      for (SModule m : allVisible) {
        myResolveScope.put(m.getModuleReference(), m);
      }
    }
    return myResolveScope.get(reference);
  }
  @Override
  protected void scopeChanged() {
  }
  public ModulesScope(Element element, Project project) throws CantLoadSomethingException {
    this(resolveModules(element, project.getRepository()));
  }
  private static Iterable<SModule> resolveModules(Element element, SRepository repo) throws CantLoadSomethingException {
    List<SModule> result = new ArrayList<SModule>();
    final Logger log = Logger.getLogger(ModulesScope.class);
    for (Element moduleXml : element.getChildren(MODULE_TAG)) {
      try {
        final String moduleRef = moduleXml.getAttributeValue(MODULE_ID);
        if (moduleRef == null) {
          continue;
        }
        SModuleReference mr = PersistenceFacade.getInstance().createModuleReference(moduleRef);
        final SModule module = mr.resolve(repo);
        if (module != null) {
          result.add(module);
        } else {
          log.warning("module not found " + moduleRef);
        }
      } catch (IllegalArgumentException e) {
        throw new CantLoadSomethingException(e);
      }
    }
    return result;
  }
  @Override
  public void write(Element element, Project project) throws CantSaveSomethingException {
    for (SModule module : myModules) {
      Element moduleXml = new Element(MODULE_TAG);
      moduleXml.setAttribute(MODULE_ID, PersistenceFacade.getInstance().asString(module.getModuleReference()));
      element.addContent(moduleXml);
    }
  }
  public String toString() {
    return "module scope";
  }
}
