package org.paules.geckoboard.api.widget;

import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.ObjectNode;
import org.paules.geckoboard.api.Push;
import org.paules.geckoboard.api.error.ValidationException;
import org.paules.geckoboard.api.json.GraphType;

public class RAGColumnsAndNumbers extends Push {
    private static final class Item {
        private final String text;

        private final int    value;

        private final String prefix;

        public Item( String text, int value ) {
            super();
            this.text = text;
            this.value = value;
            prefix = null;
        }

        public Item( String text, int value, String prefix ) {
            super();
            this.text = text;
            this.value = value;
            this.prefix = prefix;
        }

        public String getPrefix() {
            return prefix;
        }

        public String getText() {
            return text;
        }

        public int getValue() {
            return value;
        }
    }

    private Item      red;

    private Item      amber;

    private Item      green;

    private GraphType graphType;

    public RAGColumnsAndNumbers( String widgetKey, GraphType graphType ) {
        super( widgetKey );
        this.graphType = graphType;
    }
    
    @Override
    protected void validate() throws ValidationException {
    }

    @Override
    protected void getData( ObjectNode data ) {
        data.put( "type", graphType.toString().toLowerCase() );
        ArrayNode items = data.arrayNode();
        data.put( "item", items );
        ObjectNode red = data.objectNode();
        red.put( "value", this.red.getValue() );
        red.put( "text", this.red.getText() );
        if ( this.red.getPrefix() != null ) {
            red.put( "prefix", this.red.getPrefix() );
        }
        items.add( red );

        ObjectNode amber = data.objectNode();
        amber.put( "value", this.amber.getValue() );
        amber.put( "text", this.amber.getText() );
        if ( this.amber.getPrefix() != null ) {
            amber.put( "prefix", this.amber.getPrefix() );
        }
        items.add( amber );

        ObjectNode green = data.objectNode();
        green.put( "value", this.green.getValue() );
        green.put( "text", this.green.getText() );
        if ( this.green.getPrefix() != null ) {
            green.put( "prefix", this.green.getPrefix() );
        }
        items.add( green );
    }

    public void setAmber( String label, int value ) {
        amber = new Item( label, value );
    }

    public void setAmber( String label, int value, String prefix ) {
        amber = new Item( label, value, prefix );
    }

    public void setGreen( String label, int value ) {
        green = new Item( label, value );
    }

    public void setGreen( String label, int value, String prefix ) {
        green = new Item( label, value, prefix );
    }

    public void setRed( String label, int value ) {
        red = new Item( label, value );
    }

    public void setRed( String label, int value, String prefix ) {
        red = new Item( label, value, prefix );
    }
}

@@ -1318,20 +1318,16 @@
 ;%0A    %7D%0A
-    
 %0A    @Ov
@@ -1600,24 +1600,25 @@
 %22, items );%0A
+%0A
         Obje
@@ -1624,19 +1624,21 @@
 ectNode 
+g
 re
-d
+en
  = data.
@@ -1655,27 +1655,29 @@
 ();%0A        
+g
 re
-d
+en
 .put( %22value
@@ -1680,27 +1680,29 @@
 alue%22, this.
+g
 re
-d
+en
 .getValue() 
@@ -1708,27 +1708,29 @@
  );%0A        
+g
 re
-d
+en
 .put( %22text%22
@@ -1732,27 +1732,29 @@
 text%22, this.
+g
 re
-d
+en
 .getText() )
@@ -1769,27 +1769,29 @@
   if ( this.
+g
 re
-d
+en
 .getPrefix()
@@ -1815,19 +1815,21 @@
         
+g
 re
-d
+en
 .put( %22p
@@ -1841,19 +1841,21 @@
 %22, this.
+g
 re
-d
+en
 .getPref
@@ -1891,19 +1891,21 @@
 ms.add( 
+g
 re
-d
+en
  );%0A%0A   
@@ -2208,37 +2208,35 @@
      ObjectNode 
-g
 re
-en
+d
  = data.objectNo
@@ -2241,37 +2241,35 @@
 Node();%0A        
-g
 re
-en
+d
 .put( %22value%22, t
@@ -2264,37 +2264,35 @@
 ( %22value%22, this.
-g
 re
-en
+d
 .getValue() );%0A 
@@ -2290,37 +2290,35 @@
 ue() );%0A        
-g
 re
-en
+d
 .put( %22text%22, th
@@ -2312,37 +2312,35 @@
 t( %22text%22, this.
-g
 re
-en
+d
 .getText() );%0A  
@@ -2347,37 +2347,35 @@
       if ( this.
-g
 re
-en
+d
 .getPrefix() != 
@@ -2387,37 +2387,35 @@
 ) %7B%0A            
-g
 re
-en
+d
 .put( %22prefix%22, 
@@ -2411,37 +2411,35 @@
  %22prefix%22, this.
-g
 re
-en
+d
 .getPrefix() );%0A
@@ -2463,29 +2463,27 @@
  items.add( 
-g
 re
-en
+d
  );%0A    %7D%0A%0A 

Fix in order of RAG items