package org.buffagon.intellij.catberry;

import com.intellij.json.psi.JsonFile;
import com.intellij.json.psi.JsonProperty;
import com.intellij.json.psi.JsonStringLiteral;
import com.intellij.lang.javascript.psi.JSFile;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.search.FileTypeIndex;
import com.intellij.psi.search.FilenameIndex;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.containers.HashMap;
import com.intellij.util.indexing.FileBasedIndex;
import org.jetbrains.annotations.NotNull;

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

/**
 * @author Prokofiev Alex
 */
public class CatberryComponentUtils {
  public static Map<String, PsiFile> findComponents(Project project) {
    Map<String, PsiFile> result = new HashMap<String, PsiFile>();
    Collection<VirtualFile> virtualFiles =
        FileBasedIndex.getInstance().getContainingFiles(FilenameIndex.NAME, CatberryConstants.CAT_COMPONENT_JSON,
            GlobalSearchScope.allScope(project));
    for (VirtualFile virtualFile : virtualFiles) {
      JsonFile psiFile = (JsonFile) PsiManager.getInstance(project).findFile(virtualFile);
      if (psiFile != null) {
        JsonProperty[] properties = PsiTreeUtil.getChildrenOfType(psiFile.getTopLevelValue(), JsonProperty.class);
        if (properties != null) {
          for (JsonProperty property : properties) {
            if (!property.getName().equals("name"))
              continue;
            if (property.getValue() != null && property.getValue() instanceof JsonStringLiteral)
              result.put(((JsonStringLiteral) property.getValue()).getValue(), psiFile);
            break;
          }
        }
      }
    }
    return result;
  }

  public static PsiFile findComponent(@NotNull final Project project, @NotNull final String name) {
    Collection<VirtualFile> virtualFiles =
        FileBasedIndex.getInstance().getContainingFiles(FilenameIndex.NAME, CatberryConstants.CAT_COMPONENT_JSON,
            GlobalSearchScope.allScope(project));
    for (VirtualFile virtualFile : virtualFiles) {
      JsonFile psiFile = (JsonFile) PsiManager.getInstance(project).findFile(virtualFile);
      if (psiFile != null) {
        JsonProperty[] properties = PsiTreeUtil.getChildrenOfType(psiFile.getTopLevelValue(), JsonProperty.class);
        if (properties != null) {
          boolean found = false;
          for (JsonProperty property : properties) {
            if (!property.getName().equals("name"))
              continue;
            if (property.getValue() != null && property.getValue() instanceof JsonStringLiteral)
              found = name.equals(((JsonStringLiteral) property.getValue()).getValue());
            break;
          }
          if(!found)
            continue;
          for (JsonProperty property : properties) {
            if (!property.getName().equals("logic"))
              continue;
            if (property.getValue() != null && property.getValue() instanceof JsonStringLiteral) {
              final String path = ((JsonStringLiteral) property.getValue()).getValue();
              VirtualFile f = psiFile.getVirtualFile().getParent();
              f = f.findFileByRelativePath(path);
              if (f != null)
                return PsiManager.getInstance(project).findFile(f);
            }
            return null;
          }
        }
      }
    }
    return null;
  }
}

@@ -3019,32 +3019,71 @@
       continue;%0A
+          String logic = %22./index.js%22;%0A
           for (J
@@ -3292,18 +3292,16 @@
 Literal)
- %7B
 %0A       
@@ -3311,25 +3311,13 @@
     
-final String path
+logic
  = (
@@ -3365,32 +3365,59 @@
 getValue();%0A    
+        break;%0A          %7D%0A
           Virtua
@@ -3474,20 +3474,16 @@
         
-    
 f = f.fi
@@ -3507,18 +3507,16 @@
 ath(
-path);%0A   
+logic);%0A
     
@@ -3517,25 +3517,24 @@
 ;%0A          
- 
 if (f != nul
@@ -3536,20 +3536,16 @@
 = null)%0A
-    
         
@@ -3614,48 +3614,20 @@
     
-  %7D%0A            return null;%0A          %7D
+return null;
 %0A   

fix findComponent with default name.