package com.jetbrains.python.buildout.config.ref;

import com.google.common.collect.Lists;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.django.lang.template.psi.impl.DjangoTemplateFileImpl;
import com.jetbrains.django.model.DjangoTemplateManager;
import com.jetbrains.django.ref.BaseReference;
import com.jetbrains.python.PythonStringUtil;
import com.jetbrains.python.buildout.config.psi.impl.BuildoutCfgFile;
import com.jetbrains.python.buildout.config.psi.impl.BuildoutCfgSection;
import com.jetbrains.python.psi.PyElementGenerator;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @author traff
 */
public class BuildoutPartReference extends BaseReference {
  private final String myPartName;
  private final int myOffsetInElement;

  public BuildoutPartReference(PsiElement element, String partName, int offsetInElement) {
    super(element);
    myPartName = partName;
    myOffsetInElement = offsetInElement;
  }

  @Override
  public TextRange getRangeInElement() {
    return TextRange.from(myOffsetInElement, myPartName.length());
  }

  public PsiElement resolve() {
    BuildoutCfgFile file = PsiTreeUtil.getParentOfType(myElement, BuildoutCfgFile.class);
    if (file != null) {
      return file.findSectionByName(myPartName);
    }
    return null;
  }

  @NotNull
  public Object[] getVariants() {
    List<String> res = Lists.newArrayList();
    BuildoutCfgFile file = PsiTreeUtil.getParentOfType(myElement, BuildoutCfgFile.class);
    if (file != null) {
      for (BuildoutCfgSection sec : file.getSections()) {
        String name = sec.getHeaderName();
        if (name != null) {
          res.add(name);
        }
      }
      return res.toArray();
    }
    return EMPTY_ARRAY;
  }

  @Override
  public PsiElement handleElementRename(@NotNull String newElementName) {
    String fullName = PythonStringUtil.replaceLastSuffix(getElement().getText(), "/", newElementName);
    return myElement.replace(PyElementGenerator.getInstance(myElement.getProject()).createStringLiteralAlreadyEscaped(fullName));
  }

  @Override
  public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
    Module module = ModuleUtil.findModuleForPsiElement(myElement);
    if (module != null) {
      String name = DjangoTemplateManager.getRelativeName(module, (DjangoTemplateFileImpl)element);
      if (name != null) {
        return myElement.replace(PyElementGenerator.getInstance(myElement.getProject()).createStringLiteralFromString(name));
      }
    }
    return myElement;
  }
}

@@ -88,98 +88,8 @@
 ts;%0A
-import com.intellij.openapi.module.Module;%0Aimport com.intellij.openapi.module.ModuleUtil;%0A
 impo
@@ -210,194 +210,8 @@
 il;%0A
-import com.intellij.util.IncorrectOperationException;%0Aimport com.jetbrains.django.lang.template.psi.impl.DjangoTemplateFileImpl;%0Aimport com.jetbrains.django.model.DjangoTemplateManager;%0A
 impo
@@ -2017,505 +2017,7 @@
 %0A  %7D
-%0A%0A  @Override%0A  public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException %7B%0A    Module module = ModuleUtil.findModuleForPsiElement(myElement);%0A    if (module != null) %7B%0A      String name = DjangoTemplateManager.getRelativeName(module, (DjangoTemplateFileImpl)element);%0A      if (name != null) %7B%0A        return myElement.replace(PyElementGenerator.getInstance(myElement.getProject()).createStringLiteralFromString(name));%0A      %7D%0A    %7D%0A    return myElement;%0A  %7D
 %0A%7D%0A

remove nonsense code from BuildoutPartReference