package org.azavea.otm;

import java.text.NumberFormat;

import org.azavea.otm.data.Model;
import org.json.JSONException;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

public class EcoField extends Field {

	private static final String VALUE_KEY = ".value";
	private static final String UNIT_KEY = ".unit";
	private static final String AMOUNT_KEY = ".dollars";
	
	protected EcoField(String key, String label, int minimumToEdit, 
			String keyboard, String format, String type)	{
		super(key, label, minimumToEdit, keyboard, format, type);
	}
	
	@Override
	public View renderForDisplay(LayoutInflater layout, Model model) 
			throws JSONException {
		View container = layout.inflate(R.layout.plot_ecofield_row, null);
        ((TextView)container.findViewById(R.id.field_label)).setText(this.label);
        
        // Extract the value of this type of eco benefit
        String value = getValueForKey(this.key + VALUE_KEY, model.getData()).toString();
        String units = getValueForKey(this.key + UNIT_KEY, model.getData()).toString();
        ((TextView)container.findViewById(R.id.field_value))
        	.setText(value + " " + units);
        
        // The dollar amount of the benefit
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        Double amount = (Double)getValueForKey(this.key + AMOUNT_KEY, model.getData());
        ((TextView)container.findViewById(R.id.field_money))
        	.setText(currency.format(amount));
    	
        return container;
	}
}

@@ -916,22 +916,22 @@
         
-String
+Object
  value =
@@ -980,35 +980,24 @@
 l.getData())
-.toString()
 ;%0A        St
@@ -994,22 +994,22 @@
         
-String
+Object
  units =
@@ -1065,21 +1065,40 @@
 a())
-.toString();%0A
+;%0A        if (value != null) %7B%0A%09
     
@@ -1154,16 +1154,17 @@
 value))%0A
+%09
         
@@ -1195,16 +1195,17 @@
 units);%0A
+%09
         
@@ -1197,32 +1197,33 @@
 its);%0A%09        %0A
+%09
         // The d
@@ -1242,32 +1242,33 @@
  of the benefit%0A
+%09
         NumberFo
@@ -1319,16 +1319,17 @@
 ance();%0A
+%09
         
@@ -1400,32 +1400,33 @@
 del.getData());%0A
+%09
         ((TextVi
@@ -1470,16 +1470,17 @@
 money))%0A
+%09
         
@@ -1519,17 +1519,21 @@
 ));%0A    
-%09
+    %7D
 %0A       

Handle null and missing eco fields