/*
 * Copyright 2000-2013 JetBrains s.r.o.
 * Copyright 2014-2014 AS3Boyan
 * Copyright 2014-2014 Elias Ku
 *
 * 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.plugins.haxe.ide.index;

import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.plugins.haxe.lang.psi.HaxeClass;
import com.intellij.plugins.haxe.util.HaxeResolveUtil;
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.Processor;
import com.intellij.util.indexing.*;
import com.intellij.util.io.EnumeratorStringDescriptor;
import com.intellij.util.io.KeyDescriptor;
import gnu.trove.THashMap;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author: Fedor.Korotkov
 */
public class HaxeComponentFileNameIndex extends ScalarIndexExtension<String> {
  public static final ID<String, Void> HAXE_COMPONENT_FILE_NAME_INDEX = ID.create("HaxeComponentFileNameIndex");
  private static final int INDEX_VERSION = HaxeIndexUtil.BASE_INDEX_VERSION + 4;
  private DataIndexer<String, Void, FileContent> myDataIndexer = new MyDataIndexer();

  @NotNull
  @Override
  public ID<String, Void> getName() {
    return HAXE_COMPONENT_FILE_NAME_INDEX;
  }

  @NotNull
  @Override
  public DataIndexer<String, Void, FileContent> getIndexer() {
    return myDataIndexer;
  }

  @Override
  public int getVersion() {
    return INDEX_VERSION;
  }

  @Override
  public boolean dependsOnFileContent() {
    return true;
  }

  @Override
  public KeyDescriptor<String> getKeyDescriptor() {
    return new EnumeratorStringDescriptor();
  }

  @Override
  public FileBasedIndex.InputFilter getInputFilter() {
    return HaxeSdkInputFilter.INSTANCE;
  }

  @NotNull
  public static List<VirtualFile> getFilesNameByQName(@NotNull String qName, @NotNull final GlobalSearchScope filter) {
    final List<VirtualFile> result = new ArrayList<VirtualFile>();
    getFileNames(qName, new Processor<VirtualFile>() {
      @Override
      public boolean process(VirtualFile file) {
        result.add(file);
        return true;
      }
    }, filter);
    return result;
  }

  public static boolean getFileNames(@NotNull String qName,
                                     @NotNull Processor<VirtualFile> processor,
                                     @NotNull final GlobalSearchScope filter) {
    Project project = filter.getProject();
    if (project != null) {
      if (DumbService.isDumb(project)) {
        return false;
      }
    }

    return FileBasedIndex.getInstance()
      .getFilesWithKey(HAXE_COMPONENT_FILE_NAME_INDEX, Collections.<String>singleton(qName), processor, filter);
  }

  private static class MyDataIndexer implements DataIndexer<String, Void, FileContent> {
    @Override
    @NotNull
    public Map<String, Void> map(final FileContent inputData) {
      final PsiFile psiFile = inputData.getPsiFile();
      final String moduleName = FileUtil.getNameWithoutExtension(inputData.getFileName());
      final List<HaxeClass> classes = HaxeResolveUtil.findComponentDeclarations(psiFile);
      if (classes.isEmpty()) {
        return Collections.emptyMap();
      }
      boolean moduleAdded = false;
      final Map<String, Void> result = new THashMap<String, Void>(classes.size());
      for (HaxeClass haxeClass : classes) {
        final String className = haxeClass.getName();
        if (className != null) {
          if(!moduleAdded && className.equals(moduleName)) {
            moduleAdded = true;
          }
          result.put(haxeClass.getQualifiedName(), null);
        }
      }
      if(!moduleAdded) {
        String mainModuleName = HaxeResolveUtil.joinQName(HaxeResolveUtil.getPackageName(psiFile), moduleName);
        result.put(mainModuleName, null);
      }
      return result;
    }
  }
}

@@ -100,16 +100,47 @@
 lias Ku%0A
+ * Copyright 2017 Eric Bishton%0A
  *%0A * Li
@@ -1852,16 +1852,165 @@
 xer();%0A%0A
+  /** The list of files searched in dumb mode.  (Always empty.) */%0A  public static List%3CVirtualFile%3E DUMB_MODE_LIST = new ArrayList%3CVirtualFile%3E();%0A%0A
   @NotNu
@@ -2727,24 +2727,182 @@
 e filter) %7B%0A
+%0A    Project project = filter.getProject();%0A    if (project != null) %7B%0A      if (DumbService.isDumb(project)) %7B%0A        return DUMB_MODE_LIST;%0A      %7D%0A    %7D%0A%0A
     final Li
@@ -3170,29 +3170,30 @@
 lt;%0A  %7D%0A%0A  p
-ublic
+rivate
  static bool

Return a special empty array in dumb mode, instead of silently failing.