package org.emoncms.myapps.myelectric;

import android.os.Parcel;
import android.os.Parcelable;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Holds the details of a MyElectric instance
 */
public class MyElectricSettings implements Parcelable {

    private int id;
    private String name;
    private int powerFeedId;
    private int useFeedId;
    private String unitCost;
    private String costSymbol;
    private String powerScale;
    private float powerScaleFloat;
    private float unitCostFloat;
    private boolean deleted = false;

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public MyElectricSettings createFromParcel(Parcel in) {
            return new MyElectricSettings(in);
        }
        public MyElectricSettings[] newArray(int size) {
            return new MyElectricSettings[size];
        }
    };

    public static MyElectricSettings fromJson(int id, JSONObject jsonObject) throws JSONException {
        return new MyElectricSettings(id, jsonObject.getString("name"),
                jsonObject.getInt("powerFeedId"),
                jsonObject.getInt("useFeedId"),
                jsonObject.getString("powerScale"),
                jsonObject.getString("unitCost"),
                jsonObject.getString("costSymbol"));
    }

    public MyElectricSettings(int id, String name, int powerFeedId, int useFeedId, String powerScale, String unitCost, String costSymbol) {
        this.id = id;
        this.name = name;
        this.powerFeedId = powerFeedId;
        this.useFeedId = useFeedId;
        this.unitCost = unitCost;
        this.costSymbol = costSymbol;
        this.powerScale = powerScale;
        this.powerScaleFloat = Float.parseFloat(powerScale);
        this.unitCostFloat = Float.parseFloat(unitCost);
    }

    public MyElectricSettings(Parcel in) {
        this.id = in.readInt();
        this.name = in.readString();
        this.powerFeedId = in.readInt();
        this.useFeedId = in.readInt();
        this.powerScale = in.readString();
        this.unitCost = in.readString();
        this.costSymbol = in.readString();
        this.powerScaleFloat = Float.parseFloat(powerScale);
        this.unitCostFloat = Float.parseFloat(unitCost);
    }

    public void setDeleted() {
        deleted = true;
    }

    public boolean isDeleted() {
        return deleted;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public int getPowerFeedId() {
        return powerFeedId;
    }

    public int getUseFeedId() {
        return useFeedId;
    }

    public String getPowerScale() {
        return powerScale;
    }

    public float getPowerScaleAsFloat() {
        return powerScaleFloat;
    }

    public void setPowerScale(String powerScale) {
        this.powerScale = powerScale;
        this.powerScaleFloat = Float.parseFloat(powerScale);
    }

    public String getUnitCost() {
        return unitCost;
    }

    public String getCostSymbol() {
        return costSymbol;
    }

    public void setUseFeedId(int useFeedId) {
        this.useFeedId = useFeedId;
    }

    public void setPowerFeedId(int powerFeedId) {
        this.powerFeedId = powerFeedId;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setCostSymbol(String costSymbol) {
        this.costSymbol = costSymbol;
    }

    public void setUnitCost(String unitCost) {
        this.unitCost = unitCost;
        this.unitCostFloat = Float.parseFloat(unitCost);
    }

    public float getUnitCostFloat() {
        return unitCostFloat;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(id);
        parcel.writeString(name);
        parcel.writeInt(powerFeedId);
        parcel.writeInt(useFeedId);
        parcel.writeString(powerScale);
        parcel.writeString(unitCost);
        parcel.writeString(costSymbol);
    }

    public String toJson() throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name",name);
        jsonObject.put("powerFeedId",powerFeedId);
        jsonObject.put("useFeedId",useFeedId);
        jsonObject.put("powerScale",powerScale);
        jsonObject.put("unitCost",unitCost);
        jsonObject.put("costSymbol",costSymbol);
        return jsonObject.toString();
    }

    public String toString() {
        return name + ", power: " + powerFeedId + ", use: " + useFeedId;
    }
}

@@ -1717,35 +1717,32 @@
 caleFloat = 
-Float.parse
+stringTo
 Float(powerS
@@ -1773,35 +1773,32 @@
 CostFloat = 
-Float.parse
+stringTo
 Float(unitCo
@@ -2155,35 +2155,32 @@
 caleFloat = 
-Float.parse
+stringTo
 Float(powerS
@@ -2211,35 +2211,32 @@
 CostFloat = 
-Float.parse
+stringTo
 Float(unitCo
@@ -2944,35 +2944,32 @@
 caleFloat = 
-Float.parse
+stringTo
 Float(powerS
@@ -3515,24 +3515,25 @@
 unitCost) %7B%0A
+%0A
         this
@@ -3587,19 +3587,16 @@
 t = 
-Float.parse
+stringTo
 Floa
@@ -3600,32 +3600,33 @@
 loat(unitCost);%0A
+%0A
     %7D%0A%0A    publi
@@ -4542,24 +4542,24 @@
 oString() %7B%0A
-
         retu
@@ -4624,11 +4624,197 @@
 d;%0A    %7D
+%0A%0A    private float stringToFloat(String val) %7B%0A        try %7B%0A            return Float.parseFloat(val);%0A        %7D catch (NumberFormatException ex) %7B%0A            return 0;%0A        %7D%0A    %7D
 %0A%7D%0A

handle blank cost value