package info.nightscout.androidaps.plugins.Treatments;

import android.content.Intent;
import android.support.annotation.Nullable;

import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.DetailedBolusInfo;
import info.nightscout.androidaps.db.CareportalEvent;
import info.nightscout.androidaps.db.Source;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.Overview.Dialogs.ErrorHelperActivity;
import info.nightscout.androidaps.queue.Callback;
import info.nightscout.utils.DateUtil;

import static info.nightscout.utils.DateUtil.now;

public class CarbsGenerator {
    public static void generateCarbs(int amount, long startTime, int duration, @Nullable String notes) {
        long remainingCarbs = amount;
        int ticks = (duration * 4); //duration guaranteed to be integer greater zero
        for (int i = 0; i < ticks; i++){
            long carbTime = startTime + i * 15 * 60 * 1000;
            int smallCarbAmount = (int) Math.round((1d * remainingCarbs) / (ticks-i));  //on last iteration (ticks-i) is 1 -> smallCarbAmount == remainingCarbs
            remainingCarbs -= smallCarbAmount;
            if (smallCarbAmount > 0)
                createCarb(smallCarbAmount, carbTime, CareportalEvent.MEALBOLUS, notes);
        }
    }

    public static void createCarb(int carbs, long time, String eventType, @Nullable String notes) {
        long now = DateUtil.now();
        int carbTime = (int) ((time - now) / 1000 / 60);
        DetailedBolusInfo carbInfo = new DetailedBolusInfo();
        carbInfo.date = now;
        carbInfo.carbTime = carbTime;
        carbInfo.eventType = eventType;
        carbInfo.carbs = carbs;
        carbInfo.context = MainApp.instance();
        carbInfo.source = Source.USER;
        carbInfo.notes = notes;
        if (ConfigBuilderPlugin.getPlugin().getActivePump().getPumpDescription().storesCarbInfo && carbInfo.date <= now()) {
            ConfigBuilderPlugin.getPlugin().getCommandQueue().bolus(carbInfo, new Callback() {
                @Override
                public void run() {
                    if (!result.success) {
                        Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class);
                        i.putExtra("soundid", R.raw.boluserror);
                        i.putExtra("status", result.comment);
                        i.putExtra("title", MainApp.gs(R.string.treatmentdeliveryerror));
                        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        MainApp.instance().startActivity(i);
                    }
                }
            });
        } else {
            TreatmentsPlugin.getPlugin().addToHistoryTreatment(carbInfo, false);
        }
    }
}

@@ -572,47 +572,8 @@
 ack;
-%0Aimport info.nightscout.utils.DateUtil;
 %0A%0Aim
@@ -1434,100 +1434,8 @@
 ) %7B%0A
-        long now = DateUtil.now();%0A        int carbTime = (int) ((time - now) / 1000 / 60);%0A
     
@@ -1520,46 +1520,9 @@
 e = 
-now;%0A        carbInfo.carbTime = carbT
+t
 ime;

Revert "fix carb generator"