/*
 * Copyright 2000-2015 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 com.intellij.codeInspection.htmlInspections;

import com.intellij.codeInsight.daemon.XmlErrorMessages;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiErrorElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.xml.XmlChildRole;
import com.intellij.psi.xml.XmlTag;
import com.intellij.psi.xml.XmlToken;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;

/**
 * @author spleaner
 */
public class RemoveExtraClosingTagIntentionAction implements LocalQuickFix, IntentionAction {
  @Override
  @NotNull
  public String getFamilyName() {
    return XmlErrorMessages.message("remove.extra.closing.tag.quickfix");
  }


  @Override
  @NotNull
  public String getText() {
    return getName();
  }

  @Override
  public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) {
    return true;
  }

  @Override
  public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
    final int offset = editor.getCaretModel().getOffset();
    final PsiElement psiElement = file.findElementAt(offset);
    if (psiElement == null || !psiElement.isValid() || !(psiElement instanceof XmlToken)) {
      return;
    }

    doFix(psiElement);
  }

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

  private static void doFix(@NotNull final PsiElement element) throws IncorrectOperationException {
    final XmlToken endNameToken = (XmlToken)element;
    final PsiElement tagElement = endNameToken.getParent();
    if (!(tagElement instanceof XmlTag) && !(tagElement instanceof PsiErrorElement)) return;

    if (tagElement instanceof PsiErrorElement) {
      tagElement.delete();
    }
    else {
      final ASTNode astNode = tagElement.getNode();
      if (astNode != null) {
        final ASTNode endTagStart = XmlChildRole.CLOSING_TAG_START_FINDER.findChild(astNode);
        if (endTagStart != null) {
          final Document document = PsiDocumentManager.getInstance(element.getProject()).getDocument(tagElement.getContainingFile());
          if (document != null) {
            document.deleteString(endTagStart.getStartOffset(), tagElement.getLastChild().getTextRange().getEndOffset());
          }
        }
      }
    }
  }

  @Override
  public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
    final PsiElement element = descriptor.getPsiElement();
    if (!(element instanceof XmlToken)) return;

    doFix(element);
  }
}

@@ -1403,16 +1403,43 @@
 tNull;%0A%0A
+import java.util.Objects;%0A%0A
 /**%0A * @
@@ -1891,423 +1891,490 @@
     
-return true;%0A  %7D%0A%0A  @Override%0A  public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException %7B%0A    final int offset = editor.getCaretModel().getOffset();%0A    final PsiElement psiElement = file.findElementAt(offset);%0A    if (psiElement == null %7C%7C !psiElement.isValid() %7C%7C !(psiElement instanceof XmlToken)) %7B%0A      return;%0A    %7D%0A%0A    doFix(psiElem
+PsiElement psiElement = file.findElementAt(editor.getCaretModel().getOffset());%0A    return psiElement instanceof XmlToken && %0A           (psiElement.getParent() instanceof XmlTag %7C%7C psiElement.getParent() instanceof PsiErrorElement);%0A  %7D%0A%0A  @Override%0A  public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException %7B%0A    doFix(Objects.requireNonNull(file.findElementAt(editor.getCaretModel().getOffset())).getPar
 ent
+()
 );%0A 
@@ -2488,22 +2488,16 @@
 NotNull 
-final 
 PsiEleme
@@ -2495,25 +2495,28 @@
  PsiElement 
-e
+tagE
 lement) thro
@@ -2552,215 +2552,8 @@
 n %7B%0A
-    final XmlToken endNameToken = (XmlToken)element;%0A    final PsiElement tagElement = endNameToken.getParent();%0A    if (!(tagElement instanceof XmlTag) && !(tagElement instanceof PsiErrorElement)) return;%0A%0A
     
@@ -2918,17 +2918,20 @@
 nstance(
-e
+tagE
 lement.g

disable "remove extra closing tag" intention when it would do nothing