/*
 * Copyright 2013-2014 must-be.org
 *
 * 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.mustbe.consulo.dotnet.lang.psi.impl;

import java.util.Collections;
import java.util.List;

import org.jetbrains.annotations.NotNull;
import org.mustbe.consulo.dotnet.resolve.DotNetNamespaceAsElement;
import com.intellij.lang.Language;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.ResolveState;
import com.intellij.psi.scope.PsiScopeProcessor;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.ArrayUtil;
import com.intellij.util.SmartList;
import com.intellij.util.containers.ContainerUtil;

/**
 * @author VISTALL
 * @since 23.09.14
 */
public class CompositeDotNetNamespaceAsElement extends BaseDotNetNamespaceAsElement
{
	private List<DotNetNamespaceAsElement> myList;

	public CompositeDotNetNamespaceAsElement(Project project, @NotNull String qName, List<DotNetNamespaceAsElement> list)
	{
		super(project, Language.ANY, qName);
		myList = list;
	}

	@Override
	public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, PsiElement lastParent, @NotNull PsiElement
			place)
	{
		for(DotNetNamespaceAsElement dotNetNamespaceAsElement : myList)
		{
			if(!dotNetNamespaceAsElement.processDeclarations(processor, state, lastParent, place))
			{
				return false;
			}
		}
		return true;
	}

	@NotNull
	@Override
	public PsiElement[] findChildren(@NotNull String name, @NotNull GlobalSearchScope globalSearchScope, @NotNull ChildrenFilter filter)
	{
		List<PsiElement> list = new SmartList<PsiElement>();
		for(DotNetNamespaceAsElement dotNetNamespaceAsElement : myList)
		{
			PsiElement[] children = dotNetNamespaceAsElement.findChildren(name, globalSearchScope, filter);
			Collections.addAll(list, children);
		}
		return list.isEmpty() ? PsiElement.EMPTY_ARRAY : ContainerUtil.toArray(list, PsiElement.ARRAY_FACTORY);
	}

	@NotNull
	@Override
	@SuppressWarnings("unchecked")
	public PsiElement[] getChildren(@NotNull GlobalSearchScope globalSearchScope, @NotNull ChildrenFilter filter)
	{
		PsiElement[] array = PsiElement.EMPTY_ARRAY;
		for(DotNetNamespaceAsElement dotNetNamespaceAsElement : myList)
		{
			PsiElement[] children = dotNetNamespaceAsElement.getChildren(globalSearchScope, filter);
			array = ArrayUtil.mergeArrays(array, children);
		}
		return array;
	}
}

@@ -1092,16 +1092,58 @@
 ayUtil;%0A
+import com.intellij.util.NotNullFunction;%0A
 import c
@@ -1170,16 +1170,16 @@
 rtList;%0A
-
 import c
@@ -1669,17 +1669,20 @@
 ocessor,
- 
+%0A%09%09%09
 @NotNull
@@ -1701,17 +1701,20 @@
 e state,
- 
+%0A%09%09%09
 PsiEleme
@@ -1727,17 +1727,20 @@
 tParent,
- 
+%0A%09%09%09
 @NotNull
@@ -1750,20 +1750,17 @@
 iElement
-%0A%09%09%09
+ 
 place)%0A%09
@@ -2048,17 +2048,20 @@
 ng name,
- 
+%0A%09%09%09
 @NotNull
@@ -2089,33 +2089,101 @@
 obalSearchScope,
- 
+%0A%09%09%09@NotNull NotNullFunction%3CPsiElement, PsiElement%3E transformer,%0A%09%09%09
 @NotNull Childre
@@ -2407,32 +2407,45 @@
 obalSearchScope,
+ transformer,
  filter);%0A%09%09%09Col
@@ -2722,17 +2722,85 @@
 chScope,
- 
+%0A%09%09%09@NotNull NotNullFunction%3CPsiElement, PsiElement%3E transformer,%0A%09%09%09
 @NotNull
@@ -2935,32 +2935,32 @@
 t : myList)%0A%09%09%7B%0A
-
 %09%09%09PsiElement%5B%5D 
@@ -3025,16 +3025,29 @@
 chScope,
+ transformer,
  filter)

fix composite namespace