/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * 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.jetbrains.plugins.groovy.gpp;

import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiType;
import com.intellij.psi.util.PsiUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.findUsages.LiteralConstructorReference;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap;
import org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature;
import org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType;
import org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType;
import org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil;
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.TypesUtil;
import org.jetbrains.plugins.groovy.lang.psi.typeEnhancers.GrTypeConverter;

/**
 * @author peter
 */
public class GppTypeConverter extends GrTypeConverter {

  @Override
  public boolean isAllowedInMethodCall() {
    return true;
  }

  @Override
  public Boolean isConvertible(@NotNull PsiType lType, @NotNull PsiType rType, @NotNull GroovyPsiElement context) {
    if (context instanceof GrListOrMap &&
        context.getReference() instanceof LiteralConstructorReference &&
        ((LiteralConstructorReference)context.getReference()).getConstructedClassType() != null) return null;

    if (rType instanceof GrTupleType) {
      final GrTupleType tupleType = (GrTupleType)rType;

      final PsiType expectedComponent = PsiUtil.extractIterableTypeParameter(lType, false);
      if (expectedComponent != null && isMethodCallConversion(context)) {
        PsiType[] parameters = tupleType.getParameters();
        if (parameters.length == 1) {
          PsiType tupleComponent = parameters[0];
          if (tupleComponent != null &&
              TypesUtil.isAssignable(expectedComponent, tupleComponent, context) && hasDefaultConstructor(lType)) {
            return true;
          }
        }
      }
    }

    return null;
  }

  public static boolean isClosureOverride(PsiType[] methodParameters, GrClosureType closureType, GroovyPsiElement context) {
    final GrSignature signature = closureType.getSignature();
    if (methodParameters != null && GrClosureSignatureUtil.isSignatureApplicable(signature, methodParameters, context)) {
      return true;
    }
    return false;
  }

  public static boolean hasDefaultConstructor(PsiType type) {
    final PsiClass psiClass = PsiUtil.resolveClassInType(type);
    return psiClass != null && PsiUtil.hasDefaultConstructor(psiClass, true, false);

  }

}

@@ -21,9 +21,9 @@
 -201
-4
+7
  Jet
@@ -784,16 +784,59 @@
 otNull;%0A
+import org.jetbrains.annotations.Nullable;%0A
 import o
@@ -1397,25 +1397,32 @@
 essions.
-TypesUtil
+ConversionResult
 ;%0Aimport
@@ -1492,16 +1492,118 @@
 erter;%0A%0A
+import static org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.TypesUtil.canAssign;%0A%0A
 /**%0A * @
@@ -1712,52 +1712,118 @@
  isA
-llowedInMethodCall() %7B%0A    return true;%0A  %7D%0A
+pplicableTo(@NotNull ApplicableTo position) %7B%0A    return position == ApplicableTo.ASSIGNMENT;%0A  %7D%0A%0A  @Nullable
 %0A  @
@@ -1844,15 +1844,24 @@
 lic 
-Boolean
+ConversionResult
  isC
@@ -1870,16 +1870,18 @@
 vertible
+Ex
 (@NotNul
@@ -1894,72 +1894,247 @@
 ype 
-lType, @NotNull PsiType rType, @NotNull GroovyPsiElement context
+targetType,%0A                                          @NotNull PsiType actualType,%0A                                          @NotNull GroovyPsiElement context,%0A                                          @NotNull ApplicableTo currentPosition
 ) %7B%0A
@@ -2367,17 +2367,22 @@
     if (
-r
+actual
 Type ins
@@ -2453,17 +2453,22 @@
 pleType)
-r
+actual
 Type;%0A%0A 
@@ -2543,17 +2543,22 @@
 rameter(
-l
+target
 Type, fa
@@ -2841,30 +2841,17 @@
     
-TypesUtil.is
+can
 Assign
-able
 (exp
@@ -2893,12 +2893,74 @@
 text
-) &&
+, ApplicableTo.ASSIGNMENT) == ConversionResult.OK &&%0A             
  has
@@ -2978,17 +2978,22 @@
 tructor(
-l
+target
 Type)) %7B
@@ -3008,28 +3008,43 @@
      return 
-true
+ConversionResult.OK
 ;%0A          

Fix absence of cast fix on unapplicable method calls