package info.nightscout.androidaps.plugins.Loop;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.Html;
import android.text.Spanned;

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

import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.utils.DecimalFormatter;

/**
 * Created by mike on 09.06.2016.
 */
public class APSResult implements Parcelable {
    public String reason;
    public double rate;
    public int duration;
    public boolean changeRequested = false;

    @Override
    public String toString() {
        if (changeRequested) {
            if (rate == 0 && duration == 0)
                return MainApp.sResources.getString(R.string.canceltemp);
            else
                return MainApp.sResources.getString(R.string.rate) + ": " + DecimalFormatter.to2Decimal(rate) + " U/h\n" +
                        MainApp.sResources.getString(R.string.duration) + ": " + DecimalFormatter.to0Decimal(duration) + " min\n" +
                        MainApp.sResources.getString(R.string.reason) + ": " + reason;
        } else
            return MainApp.sResources.getString(R.string.nochangerequested);
    }

    public Spanned toSpanned() {
        if (changeRequested) {
            String ret = "";
            if (rate == 0 && duration == 0) ret = MainApp.sResources.getString(R.string.canceltemp);
            else
                ret = "<b>" + MainApp.sResources.getString(R.string.rate) + "</b>: " + DecimalFormatter.to2Decimal(rate) + " U/h<br>" +
                        "<b>" + MainApp.sResources.getString(R.string.duration) + "</b>: " + DecimalFormatter.to2Decimal(duration) + " min<br>" +
                        "<b>" + MainApp.sResources.getString(R.string.reason) + "</b>: " + reason.replace("<", "&lt;").replace(">", "&gt;");
            return Html.fromHtml(ret);
        } else
            return Html.fromHtml(MainApp.sResources.getString(R.string.nochangerequested));
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(reason);
        dest.writeDouble(rate);
        dest.writeInt(duration);
        dest.writeInt(changeRequested ? 1 : 0);
    }

    public final Parcelable.Creator<APSResult> CREATOR = new Parcelable.Creator<APSResult>() {
        public APSResult createFromParcel(Parcel in) {
            return new APSResult(in);
        }

        public APSResult[] newArray(int size) {
            return new APSResult[size];
        }
    };

    protected APSResult(Parcel in) {
        reason = in.readString();
        rate = in.readDouble();
        duration = in.readInt();
        changeRequested = in.readInt() == 1;
    }

    public APSResult() {
    }

    public APSResult clone() {
        APSResult newResult = new APSResult();
        newResult.reason = new String(reason);
        newResult.rate = rate;
        newResult.duration = duration;
        newResult.changeRequested = changeRequested;
        return newResult;
    }

    public JSONObject json() {
        JSONObject json = new JSONObject();
        try {
            if (changeRequested) {
                json.put("rate", rate);
                json.put("duration", duration);
                json.put("reason", reason);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return json;
    }
}

@@ -295,16 +295,89 @@
 daps.R;%0A
+import info.nightscout.androidaps.plugins.VirtualPump.VirtualPumpPlugin;%0A
 import i
@@ -415,16 +415,16 @@
 matter;%0A
-
 %0A/**%0A * 
@@ -624,17 +624,118 @@
  false;%0A
+    private static VirtualPumpPlugin virtualPumpPlugin = new VirtualPumpPlugin(); //ECOR %25 basal rate
 %0A
-
     @Ove
@@ -1054,16 +1054,132 @@
  + %22 U/h
+ %22%0A                        + %22(%22 + DecimalFormatter.to2Decimal(rate/virtualPumpPlugin.getBaseBasalRate() *100) + %22%25)
 %5Cn%22 +%0A  
@@ -1697,32 +1697,75 @@
            else%0A
+                //ECOR Try to %25 basal rate%0A
                 
@@ -1876,16 +1876,133 @@
  + %22 U/h
+ %22 +%0A                        %22(%22 + DecimalFormatter.to2Decimal(rate/virtualPumpPlugin.getBaseBasalRate() *100) + %22%25) 
 %3Cbr%3E%22 +%0A

Add percent to result to allow manual adjust