package org.swellrt.beta.client.js.editor.annotation;

import java.util.Map;

import org.waveprotocol.wave.client.editor.EditorContext;
import org.waveprotocol.wave.client.editor.content.paragraph.Paragraph;
import org.waveprotocol.wave.model.document.util.Range;

/**
 * Paragraph annotation type. See internals in {@See Paragraph}.
 *  
 * @author pablojan@gmail.com (Pablo Ojanguren)
 *
 */
public class ParagraphValueAnnotation implements Annotation {
  
  private final String name;
  private final Map<String, Paragraph.LineStyle> styles;
  private final AttributeGenerator attributeGen;
  
  public ParagraphValueAnnotation(String name, Map<String, Paragraph.LineStyle> styles, AttributeGenerator attributeGen) {
    this.name = name;
    this.styles = styles;
    this.attributeGen = attributeGen;    
  }
  
  public String getName() {
    return name;
  }
  
  @Override
  public void set(EditorContext editor, Range range, String styleKey) {
    if (range != null && editor != null) {

      final boolean isOn = (styleKey != null && !styleKey.isEmpty());
      if (isOn)
        if (!styles.containsKey(styleKey))
          return;
        else
          styleKey = "default";

      Paragraph.LineStyle style = styles.get(styleKey);
      if (attributeGen != null)
      style.setAttributes(attributeGen.generate(editor, range, styleKey));

      editor.undoableSequence(new Runnable() {
        @Override
        public void run() {
          Paragraph.apply(editor.getDocument(), range.getStart(), range.getEnd(), style, isOn);
          style.setAttributes(null);
        }
      });

    }
  }

  @Override
  public void reset(EditorContext editor, Range range) {
    set(editor, range, null);
  }
  
  public String apply(EditorContext editor, Range range) {
    
    for (String styleValue: styles.keySet()) {
      if (Paragraph.appliesEntirely(editor.getDocument(), range.getStart(), range.getEnd(), styles.get(styleValue))) {
        return styleValue;
      }
    }
    
    return null;
  }
}

@@ -1000,28 +1000,10 @@
     
+i
 f
-inal boolean isOn =
  (st
@@ -1043,39 +1043,12 @@
 ty()
-);%0A      if (isOn)%0A        if (
+ && 
 !sty
@@ -1078,26 +1078,24 @@
 y))%0A        
-  
 return;%0A    
@@ -1100,17 +1100,96 @@
     
+%0A
   
-else%0A  
+    final boolean isOn = (styleKey != null && !styleKey.isEmpty());%0A      if (!isOn)%0A
     
@@ -1214,16 +1214,22 @@
 fault%22;%0A
+      
 %0A      P

Fix annotation paragraph apply method