/*
 * 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.csharp.ide.resolve;

import org.jetbrains.annotations.NotNull;
import org.mustbe.consulo.csharp.lang.psi.CSharpCallArgumentList;
import org.mustbe.consulo.csharp.lang.psi.impl.source.CSharpArrayAccessExpressionImpl;
import org.mustbe.consulo.csharp.lang.psi.impl.source.CSharpLikeMethodDeclarationImplUtil;
import org.mustbe.consulo.dotnet.psi.DotNetVirtualImplementOwner;
import com.intellij.openapi.util.TextRange;
import com.intellij.patterns.StandardPatterns;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiNameIdentifierOwner;
import com.intellij.psi.PsiNamedElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.PsiReferenceBase;
import com.intellij.psi.PsiReferenceContributor;
import com.intellij.psi.PsiReferenceProvider;
import com.intellij.psi.PsiReferenceRegistrar;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.ProcessingContext;
import lombok.val;

/**
 * @author VISTALL
 * @since 04.09.14
 */
public class CSharpPsiReferenceContributor extends PsiReferenceContributor
{
	@Override
	public void registerReferenceProviders(PsiReferenceRegistrar psiReferenceRegistrar)
	{
		psiReferenceRegistrar.registerReferenceProvider(StandardPatterns.psiElement(CSharpCallArgumentList.class).withParent
				(CSharpArrayAccessExpressionImpl.class), new PsiReferenceProvider()
		{
			@NotNull
			@Override
			public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext processingContext)
			{
				CSharpArrayAccessExpressionImpl parent = (CSharpArrayAccessExpressionImpl) element.getParent();
				PsiElement callable = parent.resolveToCallable();
				if(callable == null)
				{
					return PsiReference.EMPTY_ARRAY;
				}
				return new PsiReference[]{
						new PsiReferenceBase.Immediate<PsiElement>(element, new TextRange(0, element.getTextLength()), callable)
				};
			}
		});

		psiReferenceRegistrar.registerReferenceProvider(StandardPatterns.psiElement(DotNetVirtualImplementOwner.class), new PsiReferenceProvider()
		{
			@NotNull
			@Override
			public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context)
			{
				PsiElement nameIdentifier = ((PsiNameIdentifierOwner) element).getNameIdentifier();
				if(nameIdentifier == null)
				{
					return PsiReference.EMPTY_ARRAY;
				}

				val resultPair = CSharpLikeMethodDeclarationImplUtil.resolveVirtualImplementation((DotNetVirtualImplementOwner) element, element);
				switch(resultPair.getFirst())
				{
					case FOUND:
						int textOffset = nameIdentifier.getStartOffsetInParent();
						PsiReferenceBase.Immediate<PsiElement> psiElementImmediate = new PsiReferenceBase.Immediate<PsiElement>(element,
								new TextRange(textOffset, textOffset + nameIdentifier.getTextLength()), resultPair.getSecond())
						{
							@Override
							public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException
							{
								PsiElement element1 = getElement();
								if(element1 instanceof PsiNamedElement)
								{
									return ((PsiNamedElement) element1).setName(newElementName);
								}
								return element1;
							}
						};

						return new PsiReference[]{psiElementImmediate};
				}

				return PsiReference.EMPTY_ARRAY;
			}
		});
	}
}

@@ -837,165 +837,8 @@
 pl;%0A
-import org.mustbe.consulo.csharp.lang.psi.impl.source.CSharpLikeMethodDeclarationImplUtil;%0Aimport org.mustbe.consulo.dotnet.psi.DotNetVirtualImplementOwner;%0A
 impo
@@ -964,97 +964,8 @@
 nt;%0A
-import com.intellij.psi.PsiNameIdentifierOwner;%0Aimport com.intellij.psi.PsiNamedElement;%0A
 impo
@@ -1186,62 +1186,8 @@
 ar;%0A
-import com.intellij.util.IncorrectOperationException;%0A
 impo
@@ -1229,27 +1229,8 @@
 ext;
-%0Aimport lombok.val;
 %0A%0A/*
@@ -2189,1418 +2189,8 @@
 %09%7D);
-%0A%0A%09%09psiReferenceRegistrar.registerReferenceProvider(StandardPatterns.psiElement(DotNetVirtualImplementOwner.class), new PsiReferenceProvider()%0A%09%09%7B%0A%09%09%09@NotNull%0A%09%09%09@Override%0A%09%09%09public PsiReference%5B%5D getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context)%0A%09%09%09%7B%0A%09%09%09%09PsiElement nameIdentifier = ((PsiNameIdentifierOwner) element).getNameIdentifier();%0A%09%09%09%09if(nameIdentifier == null)%0A%09%09%09%09%7B%0A%09%09%09%09%09return PsiReference.EMPTY_ARRAY;%0A%09%09%09%09%7D%0A%0A%09%09%09%09val resultPair = CSharpLikeMethodDeclarationImplUtil.resolveVirtualImplementation((DotNetVirtualImplementOwner) element, element);%0A%09%09%09%09switch(resultPair.getFirst())%0A%09%09%09%09%7B%0A%09%09%09%09%09case FOUND:%0A%09%09%09%09%09%09int textOffset = nameIdentifier.getStartOffsetInParent();%0A%09%09%09%09%09%09PsiReferenceBase.Immediate%3CPsiElement%3E psiElementImmediate = new PsiReferenceBase.Immediate%3CPsiElement%3E(element,%0A%09%09%09%09%09%09%09%09new TextRange(textOffset, textOffset + nameIdentifier.getTextLength()), resultPair.getSecond())%0A%09%09%09%09%09%09%7B%0A%09%09%09%09%09%09%09@Override%0A%09%09%09%09%09%09%09public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException%0A%09%09%09%09%09%09%09%7B%0A%09%09%09%09%09%09%09%09PsiElement element1 = getElement();%0A%09%09%09%09%09%09%09%09if(element1 instanceof PsiNamedElement)%0A%09%09%09%09%09%09%09%09%7B%0A%09%09%09%09%09%09%09%09%09return ((PsiNamedElement) element1).setName(newElementName);%0A%09%09%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09%09%09return element1;%0A%09%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09%7D;%0A%0A%09%09%09%09%09%09return new PsiReference%5B%5D%7BpsiElementImmediate%7D;%0A%09%09%09%09%7D%0A%0A%09%09%09%09return PsiReference.EMPTY_ARRAY;%0A%09%09%09%7D%0A%09%09%7D);
 %0A%09%7D%0A

remove not needed code