/*
 * Copyright 2013-2017 consulo.io
 *
 * 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 consulo.csharp.lang.psi.impl.source;

import com.intellij.psi.tree.IElementType;
import consulo.annotation.access.RequiredReadAction;
import consulo.csharp.lang.psi.CSharpElementVisitor;
import consulo.csharp.lang.psi.impl.CSharpTypeUtil;
import consulo.csharp.lang.psi.impl.source.resolve.type.CSharpNullTypeRef;
import consulo.dotnet.psi.DotNetExpression;
import consulo.dotnet.resolve.DotNetTypeRef;
import consulo.dotnet.util.ArrayUtil2;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * @author VISTALL
 * @since 04.01.14.
 */
public class CSharpConditionalExpressionImpl extends CSharpExpressionImpl implements DotNetExpression
{
	public CSharpConditionalExpressionImpl(@Nonnull IElementType elementType)
	{
		super(elementType);
	}

	@Override
	public void accept(@Nonnull CSharpElementVisitor visitor)
	{
		visitor.visitConditionalExpression(this);
	}

	private DotNetExpression[] getExpressions()
	{
		return findChildrenByClass(DotNetExpression.class);
	}

	@Nonnull
	public DotNetExpression getCondition()
	{
		return ArrayUtil2.safeGet(getExpressions(), 0);
	}

	@Nullable
	public DotNetExpression getTrueExpression()
	{
		return ArrayUtil2.safeGet(getExpressions(), 1);
	}

	@Nullable
	public DotNetExpression getFalseExpression()
	{
		return ArrayUtil2.safeGet(getExpressions(), 2);
	}

	@RequiredReadAction
	@Nonnull
	@Override
	public DotNetTypeRef toTypeRefImpl(boolean resolveFromParent)
	{
		DotNetExpression trueExpression = getTrueExpression();
		DotNetExpression falseExpression = getFalseExpression();
		if(falseExpression == null && trueExpression != null)
		{
			return trueExpression.toTypeRef(resolveFromParent);
		}
		if(trueExpression == null && falseExpression == null)
		{
			return DotNetTypeRef.ERROR_TYPE;
		}

		if(trueExpression == null)
		{
			return falseExpression.toTypeRef(resolveFromParent);
		}

		DotNetTypeRef trueType = trueExpression.toTypeRef(resolveFromParent);
		DotNetTypeRef falseType = falseExpression.toTypeRef(resolveFromParent);
		if(trueType instanceof CSharpNullTypeRef)
		{
			return falseType;
		}

		if(falseType instanceof CSharpNullTypeRef)
		{
			return trueType;
		}

		if(CSharpTypeUtil.isInheritableWithImplicit(falseType, trueType, getResolveScope()))
		{
			return trueType;
		}
		else
		{
			return falseType;
		}
	}
}

@@ -635,16 +635,139 @@
 ource;%0A%0A
+import com.intellij.psi.PsiElement;%0Aimport com.intellij.psi.ResolveState;%0Aimport com.intellij.psi.scope.PsiScopeProcessor;%0A
 import c
@@ -801,16 +801,16 @@
 ntType;%0A
-
 import c
@@ -1760,16 +1760,26 @@
 ondition
+Expression
 ()%0A%09%7B%0A%09%09
@@ -3040,13 +3040,433 @@
 pe;%0A
-
 %09%09%7D%0A%09%7D
+%0A%0A%09@Override%0A%09public boolean processDeclarations(@Nonnull PsiScopeProcessor processor, @Nonnull ResolveState state, PsiElement lastParent, @Nonnull PsiElement place)%0A%09%7B%0A%09%09DotNetExpression conditionExpression = getConditionExpression();%0A%09%09if(!conditionExpression.processDeclarations(processor, state, lastParent, place))%0A%09%09%7B%0A%09%09%09return false;%0A%09%09%7D%0A%09%09return super.processDeclarations(processor, state, lastParent, place);%0A%09%7D
 %0A%7D%0A

fix #564