/*
 * 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;

import org.jetbrains.annotations.Contract;
import javax.annotation.Nullable;
import com.intellij.psi.PsiElement;
import consulo.annotations.RequiredReadAction;
import consulo.csharp.lang.psi.impl.source.CSharpBinaryExpressionImpl;
import consulo.csharp.lang.psi.impl.source.CSharpConstantExpressionImpl;
import consulo.csharp.lang.psi.impl.source.CSharpDefaultExpressionImpl;
import consulo.csharp.lang.psi.impl.source.CSharpPrefixExpressionImpl;
import consulo.dotnet.psi.DotNetExpression;
import consulo.dotnet.psi.DotNetVariable;

/**
 * @author VISTALL
 * @since 02-Nov-17
 */
public class CSharpConstantUtil
{
	@RequiredReadAction
	@Contract("null -> false")
	public static boolean isConstant(@Nullable PsiElement expression)
	{
		if(expression instanceof CSharpConstantExpressionImpl)
		{
			return true;
		}

		if(expression instanceof CSharpPrefixExpressionImpl)
		{
			return ((CSharpPrefixExpressionImpl) expression).getOperatorElement().getOperatorElementType() == CSharpTokens.MINUS && isConstant(((CSharpPrefixExpressionImpl) expression).getExpression
					());
		}

		return false;
	}

	@RequiredReadAction
	@Contract("null -> false")
	public static boolean isCompileTimeConstant(@Nullable PsiElement element)
	{
		if(element == null)
		{
			return false;
		}

		if(isConstant(element))
		{
			return true;
		}

		if(element instanceof CSharpReferenceExpression)
		{
			PsiElement target = ((CSharpReferenceExpression) element).resolve();
			if(target instanceof DotNetVariable && ((DotNetVariable) target).isConstant())
			{
				return true;
			}
			if(target instanceof CSharpEnumConstantDeclaration)
			{
				return true;
			}
		}

		if(element instanceof CSharpBinaryExpressionImpl)
		{
			DotNetExpression leftExpression = ((CSharpBinaryExpressionImpl) element).getLeftExpression();
			DotNetExpression rightExpression = ((CSharpBinaryExpressionImpl) element).getRightExpression();
			if(leftExpression == null || rightExpression == null)
			{
				return false;
			}
			return isCompileTimeConstant(leftExpression) && isCompileTimeConstant(rightExpression);
		}

		if(element instanceof CSharpDefaultExpressionImpl)
		{
			return true;
		}

		return false;
	}
}

@@ -634,56 +634,56 @@
 ort 
-org.jetbrains.annotations.Contract;%0Aimport javax
+javax.annotation.Nullable;%0A%0Aimport org.jetbrains
 .ann
@@ -689,25 +689,26 @@
 notation
-.Nullable
+s.Contract
 ;%0Aimport
@@ -1046,29 +1046,178 @@
 urce.CSharpP
-refix
+arenthesesExpressionImpl;%0Aimport consulo.csharp.lang.psi.impl.source.CSharpPrefixExpressionImpl;%0Aimport consulo.csharp.lang.psi.impl.source.CSharpTypeCast
 ExpressionIm
@@ -2929,24 +2929,325 @@
  true;%0A%09%09%7D%0A%0A
+%09%09if(element instanceof CSharpTypeCastExpressionImpl)%0A%09%09%7B%0A%09%09%09return isConstant(((CSharpTypeCastExpressionImpl) element).getInnerExpression());%0A%09%09%7D%0A%0A%09%09if(element instanceof CSharpParenthesesExpressionImpl)%0A%09%09%7B%0A%09%09%09return isConstant(((CSharpParenthesesExpressionImpl) element).getInnerExpression());%0A%09%09%7D%0A
 %09%09return fal

fix compile time constant with type cast + parentheses exp