/*
 * 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.lang.psi.impl.source.resolve;

import org.mustbe.consulo.csharp.lang.psi.impl.CSharpTypeUtil;
import org.mustbe.consulo.csharp.lang.psi.impl.source.CSharpExpressionWithParameters;
import org.mustbe.consulo.dotnet.psi.DotNetExpression;
import org.mustbe.consulo.dotnet.psi.DotNetParameter;
import org.mustbe.consulo.dotnet.psi.DotNetParameterListOwner;
import org.mustbe.consulo.dotnet.resolve.DotNetTypeRef;

/**
 * @author VISTALL
 * @since 07.01.14.
 */
public class MethodAcceptorImpl
{
	private static interface MethodAcceptor
	{
		boolean isAccepted(DotNetExpression[] expressions, DotNetParameter[] parameters);
	}

	private static class SimpleMethodAcceptor implements MethodAcceptor
	{
		@Override
		public boolean isAccepted(DotNetExpression[] expressions, DotNetParameter[] parameters)
		{
			if(expressions.length != parameters.length)
			{
				return false;
			}

			for(int i = 0; i < expressions.length; i++)
			{
				DotNetExpression expression = expressions[i];
				DotNetParameter parameter = parameters[i];

				DotNetTypeRef expressionType = expression.toTypeRef();
				DotNetTypeRef parameterType = parameter.toTypeRef();

				if(!CSharpTypeUtil.isInheritable(expressionType, parameterType, expression))
				{
					return false;
				}
			}

			return true;
		}
	}

	private static final MethodAcceptor[] ourAcceptors = new MethodAcceptor[] {new SimpleMethodAcceptor()};

	public static boolean isAccepted(CSharpExpressionWithParameters parameterExpressionsOwner, DotNetParameterListOwner methodDeclaration)
	{
		DotNetExpression[] parameterExpressions = parameterExpressionsOwner.getParameterExpressions();
		DotNetParameter[] parameters = methodDeclaration.getParameters();
		for(MethodAcceptor ourAcceptor : ourAcceptors)
		{
			if(ourAcceptor.isAccepted(parameterExpressions, parameters))
			{
				return true;
			}
		}
		return false;
	}
}

@@ -1905,24 +1905,957 @@
 ue;%0A%09%09%7D%0A%09%7D%0A%0A
+%09private static class MethodAcceptorWithDefaultValues implements MethodAcceptor%0A%09%7B%0A%09%09@Override%0A%09%09public boolean isAccepted(DotNetExpression%5B%5D expressions, DotNetParameter%5B%5D parameters)%0A%09%09%7B%0A%09%09%09if(expressions.length %3E= parameters.length)%0A%09%09%09%7B%0A%09%09%09%09return false;%0A%09%09%09%7D%0A%0A%09%09%09for(int i = 0; i %3C parameters.length; i++)%0A%09%09%09%7B%0A%09%09%09%09DotNetExpression expression = i %3C expressions.length ? expressions%5Bi%5D : null;%0A%09%09%09%09DotNetParameter parameter = parameters%5Bi%5D;%0A%0A%09%09%09%09// if expression no found - but parameter have default value - it value%0A%09%09%09%09if(expression == null && parameter.getInitializer() != null)%0A%09%09%09%09%7B%0A%09%09%09%09%09continue;%0A%09%09%09%09%7D%0A%0A%09%09%09%09if(expression == null)%0A%09%09%09%09%7B%0A%09%09%09%09%09return false;%0A%09%09%09%09%7D%0A%0A%09%09%09%09DotNetTypeRef expressionType = expression.toTypeRef();%0A%09%09%09%09DotNetTypeRef parameterType = parameter.toTypeRef();%0A%0A%09%09%09%09if(!CSharpTypeUtil.isInheritable(expressionType, parameterType, expression))%0A%09%09%09%09%7B%0A%09%09%09%09%09return false;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%0A%09%09%09return true;%0A%09%09%7D%0A%09%7D%0A%0A
 %09private sta
@@ -2944,16 +2944,55 @@
 ceptor()
+, new MethodAcceptorWithDefaultValues()
 %7D;%0A%0A%09pub

support parameters with default values whiile resolving