package com.intellij.javaee;

import com.intellij.conversion.*;
import com.intellij.openapi.util.io.FileUtil;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.Collection;
import java.util.Collections;

/**
 * @author Eugene.Kudelevsky
 */
public class DefaultHtmlDoctypeConverter extends ConverterProvider {
  protected DefaultHtmlDoctypeConverter() {
    super("default-html-language-level");
  }

  @NotNull
  @Override
  public String getConversionDescription() {
    return "Default HTML language level setting will be updated";
  }

  @NotNull
  @Override
  public ProjectConverter createConverter(@NotNull ConversionContext context) {
    return new MyConverter(context);
  }

  private static class MyConverter extends ProjectConverter {
    private final ConversionContext myContext;

    private MyConverter(@NotNull ConversionContext context) {
      myContext = context;
    }

    @Override
    public boolean isConversionNeeded() {
      final Element root = getProjectResources();
      return root != null && root.getChild("default-html-doctype") != null;
    }

    @Override
    public void preProcessingFinished() throws CannotConvertException {
      final Element root = getProjectResources();
      if (root == null) {
        return;
      }

      final Element defaultHtmlDoctype = root.getChild("default-html-doctype");
      if (defaultHtmlDoctype == null) {
        return;
      }

      String value = defaultHtmlDoctype.getTextTrim();
      value = value != null ? myContext.expandPath(value) : null;
      if (value == null) {
        return;
      }

      if (FileUtil.toSystemIndependentName(value).endsWith("idea.jar!/resources/html5-schema/html5.rnc")) {
        value = ExternalResourceManagerImpl.HTML5_DOCTYPE_ELEMENT;
      }
      final Element newElement = new Element("default-html-language-level");
      newElement.setText(value);
      root.addContent(newElement);
      root.removeContent(defaultHtmlDoctype);
    }

    @Nullable
    private Element getProjectResources() {
      final ComponentManagerSettings settings = myContext.getProjectRootManagerSettings();
      return settings != null
             ? settings.getComponentElement("ProjectResources")
             : null;
    }

    @Override
    public Collection<File> getAdditionalAffectedFiles() {
      final ComponentManagerSettings settings = myContext.getProjectRootManagerSettings();
      final File file = settings != null ? settings.getFile() : null;
      return file != null
             ? Collections.singletonList(file)
             : Collections.<File>emptyList();
    }
  }
}

@@ -1048,126 +1048,238 @@
     
-final Element root = getProjectResources();%0A      return root != null && root.getChild(%22
+return getElementToUpdate() != null;%0A    %7D%0A%0A    @Override%0A    public void preProcessingFinished() throws CannotConvertException %7B%0A      final Element defaultHtmlDoctype = getElementToUpdate();%0A%0A      if (
 default
--html-d
+HtmlD
 octype
-%22)
  != null
 ;%0A  
@@ -1278,133 +1278,379 @@
 null
-;%0A    %7D%0A%0A    @Override%0A    public void preProcessingFinished() throws CannotConvertException %7B%0A      final Element root = get
+) %7B%0A        defaultHtmlDoctype.setText(ExternalResourceManagerImpl.HTML5_DOCTYPE_ELEMENT);%0A      %7D%0A    %7D%0A%0A    @Nullable%0A    private Element getElementToUpdate() %7B%0A      final ComponentManagerSettings settings = myContext.getProjectRootManagerSettings();%0A      if (settings == null) %7B%0A        return null;%0A      %7D%0A%0A      final Element root = settings.getComponentElement(%22
 Proj
@@ -1661,17 +1661,17 @@
 esources
-(
+%22
 );%0A     
@@ -1697,32 +1697,37 @@
 %7B%0A        return
+ null
 ;%0A      %7D%0A%0A     
@@ -1719,38 +1719,32 @@
 %0A      %7D%0A%0A      
-final 
 Element defaultH
@@ -1788,15 +1788,22 @@
 tml-
-doctype
+language-level
 %22);%0A
@@ -1848,32 +1848,37 @@
 %7B%0A        return
+ null
 ;%0A      %7D%0A%0A     
@@ -2034,16 +2034,21 @@
   return
+ null
 ;%0A      
@@ -2060,16 +2060,17 @@
     if (
+!
 FileUtil
@@ -2171,535 +2171,60 @@
     
-value = ExternalResourceManagerImpl.HTML5_DOCTYPE_ELEMENT;%0A      %7D%0A      final Element newElement = new Element(%22default-html-language-level%22);%0A      newElement.setText(value);%0A      root.addContent(newElement);%0A      root.removeContent(defaultHtmlDoctype);%0A    %7D%0A%0A    @Nullable%0A    private Element getProjectResources() %7B%0A      final ComponentManagerSettings settings = myContext.getProjectRootManagerSettings();%0A      return settings != null%0A             ? settings.getComponentElement(%22ProjectResources%22)%0A             : null
+return null;%0A      %7D%0A      return defaultHtmlDoctype
 ;%0A  

revert ' fix converter: translate "default-html-doctype" option into "default-html-language-level" '