/*
 * 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 com.intellij.lang.properties.parsing;

import com.intellij.lang.*;
import com.intellij.lang.impl.PsiBuilderImpl;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Ref;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.ThreeState;
import com.intellij.util.TripleFunction;
import com.intellij.util.diff.FlyweightCapableTreeStructure;
import org.jetbrains.annotations.NotNull;

/**
 * @author max
 */
public class PropertiesParser implements PsiParser {
  private static final TripleFunction<ASTNode,LighterASTNode,FlyweightCapableTreeStructure<LighterASTNode>,ThreeState>
          MATCH_BY_KEY = new TripleFunction<ASTNode, LighterASTNode, FlyweightCapableTreeStructure<LighterASTNode>, ThreeState>() {
    @Override
    public ThreeState fun(ASTNode oldNode,
                          LighterASTNode newNode,
                          FlyweightCapableTreeStructure<LighterASTNode> structure) {
      if (oldNode.getElementType() == PropertiesElementTypes.PROPERTY) {
        ASTNode oldName = oldNode.findChildByType(PropertiesTokenTypes.KEY_CHARACTERS);
        if (oldName != null) {
          CharSequence oldNameStr = oldName.getChars();
          CharSequence newNameStr = findKeyCharacters(newNode, structure);

          if (oldNameStr != null && !Comparing.equal(oldNameStr, newNameStr)) {
            return ThreeState.NO;
          }
        }
      }

      return ThreeState.UNSURE;
    }
  };

  private static CharSequence findKeyCharacters(LighterASTNode newNode, FlyweightCapableTreeStructure<LighterASTNode> structure) {
    Ref<LighterASTNode[]> childrenRef = Ref.create(null);
    structure.getChildren(newNode, childrenRef);
    LighterASTNode[] children = childrenRef.get();
    for (int i = 0; i < children.length; ++i) {
      if (children[i].getTokenType() == PropertiesTokenTypes.KEY_CHARACTERS)
        return ((LighterASTTokenNode) children[i]).getText();
    }
    return null;
  }


  @NotNull
  public ASTNode parse(IElementType root, PsiBuilder builder) {
    doParse(root, builder);
    return builder.getTreeBuilt();
  }

  @NotNull
  public FlyweightCapableTreeStructure<LighterASTNode> parseLight(IElementType root, PsiBuilder builder) {
    doParse(root, builder);
    return builder.getLightTree();
  }

  public void doParse(IElementType root, PsiBuilder builder) {
    builder.putUserDataUnprotected(PsiBuilderImpl.CUSTOM_COMPARATOR, MATCH_BY_KEY);
    final PsiBuilder.Marker rootMarker = builder.mark();
    final PsiBuilder.Marker propertiesList = builder.mark();
    while (!builder.eof()) {
      Parsing.parseProperty(builder);
    }
    propertiesList.done(PropertiesElementTypes.PROPERTIES_LIST);
    rootMarker.done(root);
  }
}

@@ -2246,16 +2246,36 @@
 ll);%0A   
+ int childrenCount =
  structu
@@ -2363,20 +2363,33 @@
 .get();%0A
+%0A    try %7B%0A 
     
+ 
 for (int
@@ -2420,24 +2420,26 @@
 gth; ++i) %7B%0A
+  
       if (ch
@@ -2507,24 +2507,26 @@
 RS)%0A        
+ 
+ 
 return ((Lig
@@ -2571,22 +2571,26 @@
 ();%0A    
+  
 %7D%0A    
+ 
+ 
 return n
@@ -2594,16 +2594,100 @@
 n null;%0A
+    %7D%0A    finally %7B%0A      structure.disposeChildren(children, childrenCount);%0A    %7D%0A
   %7D%0A%0A%0A  

add disposeChildren()