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) {

      if (styleKey != null && !styleKey.isEmpty() && !styles.containsKey(styleKey))
        return;
      
      final boolean isOn = (styleKey != null && !styleKey.isEmpty());
      if (!isOn)
        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;
  }
}

@@ -201,16 +201,86 @@
 agraph;%0A
+import org.waveprotocol.wave.client.editor.util.EditorAnnotationUtil;%0A
 import o
@@ -327,16 +327,16 @@
 .Range;%0A
-
 %0A/**%0A * 
@@ -1234,16 +1234,47 @@
 sEmpty()
+ && !styleKey.equals(%22default%22)
 );%0A     
@@ -1732,17 +1732,23 @@
     %7D);%0A
+      
 %0A
-
     %7D%0A  
@@ -2082,24 +2082,24 @@
 eValue))) %7B%0A
-
         retu
@@ -2100,16 +2100,54 @@
   return
+ styleValue.equals(%22default%22) ? null :
  styleVa

Fix default value for paragraph annotations