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

import com.intellij.openapi.util.io.FileUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileFactory;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.python.buildout.BuildoutFacet;
import com.jetbrains.python.buildout.config.BuildoutCfgLanguage;
import com.jetbrains.python.buildout.config.psi.impl.BuildoutCfgFile;
import com.jetbrains.python.buildout.config.psi.impl.BuildoutCfgOption;
import com.jetbrains.python.buildout.config.psi.impl.BuildoutCfgSection;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.List;

/**
 * @author traff
 */
public class BuildoutPsiUtil {
  private static final String RECIPE = "recipe";
  private static final String DJANGO_RECIPE = "djangorecipe";

  @Nullable
  public static BuildoutCfgSection getDjangoSection(@NotNull BuildoutCfgFile configFile) {
    List<String> parts = configFile.getParts();
    for (String part : parts) {
      final BuildoutCfgSection section = configFile.findSectionByName(part);
      if (section != null && DJANGO_RECIPE.equals(section.getOptionValue(RECIPE))) {
        return section;
      }
    }
    return null;
  }

  @Nullable
  public static BuildoutCfgFile readBuildOutCfgFile(BuildoutFacet buildoutFacet) {
    File cfg = buildoutFacet.getConfigFile();
    if (cfg != null && cfg.exists()) {
      try {
        char[] scriptText = FileUtil.loadFileText(cfg);
        final PsiFile configFile = PsiFileFactory
          .getInstance(buildoutFacet.getModule().getProject()).createFileFromText("buildout.cfg",
                                                                                  BuildoutCfgLanguage.INSTANCE, new String(scriptText));
        if (configFile != null && configFile instanceof BuildoutCfgFile) {
          return (BuildoutCfgFile)configFile;
        }
      }
      catch (Exception e) {
      }
    }
    return null;
  }

  public static boolean isInBuildoutSection(@NotNull PsiElement element) {
    BuildoutCfgSection section = PsiTreeUtil.getParentOfType(element, BuildoutCfgSection.class);
    return section != null && "buildout".equals(section.getHeaderName());
  }

  public static boolean isAssignedTo(@NotNull PsiElement element, @NotNull String name) {
    BuildoutCfgOption option = PsiTreeUtil.getParentOfType(element, BuildoutCfgOption.class);
    return (option != null && name.equals(option.getKey()));
  }


}


@@ -68,300 +68,64 @@
 lij.
-openapi.util.io.FileUtil;%0Aimport com.intellij.psi.PsiElement;%0Aimport com.intellij.psi.PsiFile;%0Aimport com.intellij.psi.PsiFileFactory;%0Aimport com.intellij.psi.util.PsiTreeUtil;%0Aimport com.jetbrains.python.buildout.BuildoutFacet;%0Aimport com.jetbrains.python.buildout.config.BuildoutCfgLanguage
+psi.PsiElement;%0Aimport com.intellij.psi.util.PsiTreeUtil
 ;%0Aim
@@ -427,29 +427,8 @@
 e;%0A%0A
-import java.io.File;%0A
 impo
@@ -1024,744 +1024,8 @@
  %7D%0A%0A
-  @Nullable%0A  public static BuildoutCfgFile readBuildOutCfgFile(BuildoutFacet buildoutFacet) %7B%0A    File cfg = buildoutFacet.getConfigFile();%0A    if (cfg != null && cfg.exists()) %7B%0A      try %7B%0A        char%5B%5D scriptText = FileUtil.loadFileText(cfg);%0A        final PsiFile configFile = PsiFileFactory%0A          .getInstance(buildoutFacet.getModule().getProject()).createFileFromText(%22buildout.cfg%22,%0A                                                                                  BuildoutCfgLanguage.INSTANCE, new String(scriptText));%0A        if (configFile != null && configFile instanceof BuildoutCfgFile) %7B%0A          return (BuildoutCfgFile)configFile;%0A        %7D%0A      %7D%0A      catch (Exception e) %7B%0A      %7D%0A    %7D%0A    return null;%0A  %7D%0A%0A
   pu

delete method which has been moved away