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

import com.intellij.psi.PsiType
import com.intellij.psi.util.PsiUtil

/**
 * @author Maxim.Medvedev
 */

interface Constants {
  String STRING = "java.lang.String"
  String WILDCARD_PREFIX = "? extends "
}

private String extractIterableParameter(PsiType type) {
  def componentType = PsiUtil.extractIterableTypeParameter(type, true)
  return removeWildcard(componentType?.canonicalText ?: componentType?.presentableText);
}

private String removeWildcard(String componentType) {
  componentType = componentType?.replace("?extends", "? extends ")
  componentType = componentType?.replace("?super", "? super ")

  if (componentType?.startsWith(Constants.WILDCARD_PREFIX)) {
    return componentType?.substring(Constants.WILDCARD_PREFIX.size())
  }
  return componentType
}

private String constructElementTypeArg(PsiType psiType) {
  def arg = extractIterableParameter(psiType)
  if (!arg || arg == "null") return ""
  return "<$arg>"
}

/*
contributor(ctype: "java.util.Collection") {
  method name: "getAt", params: [arg: "int"], type: extractIterableParameter(psiType)
  method name: "getAt", params: [arg: "groovy.lang.Range"], type: "java.util.List${constructElementTypeArg(psiType)}"
  method name: "getAt", params: [arg: "java.util.Collection"], type: "java.util.List${constructElementTypeArg(psiType)}"
}
*/

contributor(ctype:"groovy.lang.GString") {
  delegatesTo(findClass("java.lang.String"))
}

contributor(ctype: 'groovy.lang.MetaClass') {
  property name: 'static'
}
