package de.plushnikov.intellij.plugin.extension;

import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElementFinder;
import com.intellij.psi.impl.file.impl.JavaFileManager;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.SmartList;
import com.intellij.util.containers.ContainerUtil;
import de.plushnikov.intellij.plugin.util.PsiAnnotationUtil;
import lombok.Builder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class LombokElementFinder extends PsiElementFinder {
  @Nullable
  @Override
  public PsiClass findClass(@NotNull String qualifiedName, @NotNull GlobalSearchScope scope) {
    final Project project = scope.getProject();
    if (null == project) {
      return null;
    }

    final int lastDot = qualifiedName.lastIndexOf('.');
    if (lastDot < 0) {
      return null;
    }
    final String shortName = qualifiedName.substring(lastDot + 1);
    final String parentName = qualifiedName.substring(0, lastDot);

    if (shortName.length() == 0 || parentName.length() == 0) {
      return null;
    }

    List<PsiClass> result = new SmartList<PsiClass>();

    final JavaFileManager javaFileManager = ServiceManager.getService(project, JavaFileManager.class);
    PsiClass parentClass = javaFileManager.findClass(parentName, scope);
    if (null != parentClass) {
      if (PsiAnnotationUtil.isAnnotatedWith(parentClass, Builder.class, lombok.experimental.Builder.class)) {
        ContainerUtil.addIfNotNull(result, parentClass.findInnerClassByName(shortName, false));
      }
    }
    //PsiShortNamesCache.getInstance(project).getClassesByName(name, scope)
    return result.isEmpty() ? null : result.get(0);
//    return result.isEmpty() ? PsiClass.EMPTY_ARRAY : result.toArray(new PsiClass[result.size()]);
  }

  @NotNull
  @Override
  public PsiClass[] findClasses(@NotNull String qualifiedName, @NotNull GlobalSearchScope scope) {
    return PsiClass.EMPTY_ARRAY;
  }
}

@@ -329,95 +329,8 @@
 pe;%0A
-import com.intellij.util.SmartList;%0Aimport com.intellij.util.containers.ContainerUtil;%0A
 impo
@@ -499,32 +499,8 @@
 e;%0A%0A
-import java.util.List;%0A%0A
 publ
@@ -571,32 +571,67 @@
 ble%0A  @Override%0A
+  @SuppressWarnings(%22deprecation%22)%0A
   public PsiClas
@@ -1067,29 +1067,25 @@
 ortName.
-length() == 0
+isEmpty()
  %7C%7C pare
@@ -1095,21 +1095,17 @@
 ame.
-length() == 0
+isEmpty()
 ) %7B%0A
@@ -1134,64 +1134,8 @@
  %7D%0A%0A
-    List%3CPsiClass%3E result = new SmartList%3CPsiClass%3E();%0A%0A
     
@@ -1237,16 +1237,59 @@
 s);%0A    
+if (null != javaFileManager) %7B%0A      final 
 PsiClass
@@ -1349,24 +1349,26 @@
 scope);%0A    
+  
 if (null != 
@@ -1378,24 +1378,26 @@
 entClass) %7B%0A
+  
       if (Ps
@@ -1506,42 +1506,16 @@
     
-ContainerUtil.addIfNotNull(result,
+  return
  par
@@ -1561,19 +1561,20 @@
 , false)
-)
 ;%0A
+  
       %7D%0A
@@ -1581,236 +1581,33 @@
     
-%7D%0A    //PsiShortNamesCache.getInstance(project).getClassesByName(name, scope)%0A    return result.isEmpty() ? null : result.get(0);%0A//    return result.isEmpty() ? PsiClass.EMPTY_ARRAY : result.toArray(new PsiClass%5Bresult.size()%5D)
+  %7D%0A    %7D%0A    return null
 ;%0A  

clean code