/*
 * Copyright 2000-2013 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 org.jetbrains.plugins.javaFX.fxml.refs;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlAttributeValue;
import com.intellij.util.Processor;
import com.intellij.util.QueryExecutor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.javaFX.JavaFxControllerClassIndex;
import org.jetbrains.plugins.javaFX.fxml.FxmlConstants;

import java.util.List;

/**
 * User: anna
 * Date: 3/29/13
 */
public class JavaFxControllerFieldSearcher implements QueryExecutor<PsiReference, ReferencesSearch.SearchParameters>{
  @Override
  public boolean execute(@NotNull final ReferencesSearch.SearchParameters queryParameters, @NotNull final Processor<PsiReference> consumer) {
    final PsiElement elementToSearch = queryParameters.getElementToSearch();
    if (elementToSearch instanceof PsiField) {
      final PsiField field = (PsiField)elementToSearch;
      final PsiClass containingClass = field.getContainingClass();
      if (containingClass != null) {
        final String qualifiedName = ApplicationManager.getApplication().runReadAction(new Computable<String>() {
          @Override
          public String compute() {
            return containingClass.getQualifiedName(); 
          }
        });
        if (qualifiedName != null) {
          final List<PsiFile> fxmlWithController = 
            JavaFxControllerClassIndex.findFxmlWithController(containingClass.getProject(), qualifiedName);
          final String fieldName = field.getName();
          for (final PsiFile file : fxmlWithController) {
            final VirtualFile virtualFile = file.getViewProvider().getVirtualFile();
            final SearchScope searchScope = ApplicationManager.getApplication().runReadAction(new Computable<SearchScope>() {
              @Override
              public SearchScope compute() {
                return queryParameters.getEffectiveSearchScope();
              }
            });
            if (searchScope instanceof LocalSearchScope) {
              if (!((LocalSearchScope)searchScope).isInScope(virtualFile)) continue;
            } else if (searchScope instanceof GlobalSearchScope) {
              if (!((GlobalSearchScope)searchScope).contains(virtualFile)) continue;
            }
            final Runnable runnable = new Runnable() {
              public void run() {
                file.accept(new XmlRecursiveElementVisitor() {
                  @Override
                  public void visitXmlAttributeValue(final XmlAttributeValue value) {
                    final PsiReference reference = value.getReference();
                    if (reference != null) {
                      final PsiElement resolve = reference.resolve();
                      if (resolve instanceof XmlAttributeValue) {
                        final PsiElement parent = resolve.getParent();
                        if (parent instanceof XmlAttribute) {
                          final XmlAttribute attribute = (XmlAttribute)parent;
                          if (FxmlConstants.FX_ID.equals(attribute.getName()) && fieldName.equals(attribute.getValue())) {
                            consumer.process(reference);
                          }
                        }
                      }
                    }
                  }
                });
              }
            };
            ApplicationManager.getApplication().runReadAction(runnable);
          }
        }
      }
    }
    return true;
  }
}

@@ -2419,60 +2419,8 @@
 e);%0A
-          final String fieldName = field.getName();%0A
     
@@ -2488,125 +2488,8 @@
     
- final VirtualFile virtualFile = file.getViewProvider().getVirtualFile();%0A            final SearchScope searchScope =
  App
@@ -2543,31 +2543,16 @@
 new 
-Computable%3CSearchScope%3E
+Runnable
 () %7B
@@ -2601,31 +2601,78 @@
 lic 
-SearchScope compute() %7B
+void run() %7B%0A                final String fieldName = field.getName();
 %0A   
@@ -2684,22 +2684,136 @@
         
-return
+final VirtualFile virtualFile = file.getViewProvider().getVirtualFile();%0A                final SearchScope searchScope =
  queryPa
@@ -2866,42 +2866,29 @@
     
-%7D%0A            %7D);%0A            if (
+  boolean contains = 
 sear
@@ -2922,39 +2922,19 @@
 rchScope
-) %7B%0A              if (!
+ ? 
 ((LocalS
@@ -2975,35 +2975,26 @@
 virtualFile)
-) continue;
+ :
 %0A           
@@ -2998,63 +2998,14 @@
     
-%7D else if (searchScope instanceof GlobalSearchScope) %7B%0A
+      
     
@@ -3018,13 +3018,11 @@
     
-if (!
+   
 ((Gl
@@ -3075,18 +3075,8 @@
 ile)
-) continue
 ;%0A  
@@ -3088,100 +3088,31 @@
     
- %7D%0A
      
-       final Runnable runnable = new Runnable() %7B%0A              public void run(
+if (contains
 ) %7B%0A
+  
     
@@ -3180,32 +3180,34 @@
                 
+  
 @Override%0A      
@@ -3192,32 +3192,34 @@
       @Override%0A
+  
                 
@@ -3280,32 +3280,34 @@
 eValue value) %7B%0A
+  
                 
@@ -3379,24 +3379,26 @@
             
+  
 if (referenc
@@ -3402,32 +3402,34 @@
 ence != null) %7B%0A
+  
                 
@@ -3500,24 +3500,26 @@
             
+  
 if (resolve 
@@ -3542,32 +3542,34 @@
 tributeValue) %7B%0A
+  
                 
@@ -3643,24 +3643,26 @@
             
+  
 if (parent i
@@ -3709,24 +3709,26 @@
             
+  
 final XmlAtt
@@ -3768,16 +3768,18 @@
 parent;%0A
+  
         
@@ -3889,24 +3889,26 @@
 Value())) %7B%0A
+  
             
@@ -3948,24 +3948,54 @@
 reference);%0A
+                            %7D%0A
             
@@ -4093,32 +4093,34 @@
                %7D
+);
 %0A               
@@ -4113,34 +4113,32 @@
                %7D
-);
 %0A              %7D
@@ -4155,80 +4155,8 @@
    %7D
-;%0A            ApplicationManager.getApplication().runReadAction(runnable
 );%0A 

read action