/*  Copyright (C) 2017-2019 Carsten Pfeiffer, Daniele Gobbetti

    This file is part of Gadgetbridge.

    Gadgetbridge is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Gadgetbridge is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.model;

import android.content.Context;
import android.widget.Toast;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Calendar;
import java.util.List;

import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.activities.charts.ActivityAnalysis;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
import nodomain.freeyourgadget.gadgetbridge.util.GB;



public class DailyTotals {
    Logger LOG = LoggerFactory.getLogger(DailyTotals.class);


    public float[] getDailyTotalsForAllDevices(Calendar day) {
        Context context = GBApplication.getContext();
        //get today's steps for all devices in GB
        float all_steps = 0;
        float all_sleep = 0;


        if (context instanceof GBApplication) {
            GBApplication gbApp = (GBApplication) context;
            List<? extends GBDevice> devices = gbApp.getDeviceManager().getDevices();
            for (GBDevice device : devices) {
                float[] all_daily = getDailyTotalsForDevice(device, day);
                all_steps += all_daily[0];
                all_sleep += all_daily[1] + all_daily[2];
            }
        }
        LOG.debug("gbwidget daily totals, all steps:" + all_steps);
        LOG.debug("gbwidget  daily totals, all sleep:" + all_sleep);
        return new float[]{all_steps, all_sleep};
    }


    public float[] getDailyTotalsForDevice(GBDevice device, Calendar day
    ) {

        try (DBHandler handler = GBApplication.acquireDB()) {
            ActivityAnalysis analysis = new ActivityAnalysis();
            ActivityAmounts amountsSteps = null;
            ActivityAmounts amountsSleep = null;

            amountsSteps = analysis.calculateActivityAmounts(getSamplesOfDay(handler, day, 0, device));
            amountsSleep = analysis.calculateActivityAmounts(getSamplesOfDay(handler, day, -12, device));

            float[] Sleep = getTotalsSleepForActivityAmounts(amountsSleep);
            float Steps = getTotalsStepsForActivityAmounts(amountsSteps);

            return new float[]{Steps, Sleep[0], Sleep[1]};

        } catch (Exception e) {

            GB.toast("Error loading activity summaries.", Toast.LENGTH_SHORT, GB.ERROR, e);
            return new float[]{0, 0, 0};
        }
    }

    private float[] getTotalsSleepForActivityAmounts(ActivityAmounts activityAmounts) {
        long totalSecondsDeepSleep = 0;
        long totalSecondsLightSleep = 0;
        for (ActivityAmount amount : activityAmounts.getAmounts()) {
            if (amount.getActivityKind() == ActivityKind.TYPE_DEEP_SLEEP) {
                totalSecondsDeepSleep += amount.getTotalSeconds();
            } else if (amount.getActivityKind() == ActivityKind.TYPE_LIGHT_SLEEP) {
                totalSecondsLightSleep += amount.getTotalSeconds();
            }
        }
        int totalMinutesDeepSleep = (int) (totalSecondsDeepSleep / 60);
        int totalMinutesLightSleep = (int) (totalSecondsLightSleep / 60);
        return new float[]{totalMinutesDeepSleep, totalMinutesLightSleep};
    }


    private float getTotalsStepsForActivityAmounts(ActivityAmounts activityAmounts) {
        long totalSteps = 0;
        float totalValue = 0;

        for (ActivityAmount amount : activityAmounts.getAmounts()) {
            totalSteps += amount.getTotalSteps();
        }

        float[] totalValues = new float[]{totalSteps};

        for (int i = 0; i < totalValues.length; i++) {
            float value = totalValues[i];
            totalValue += value;
        }
        return totalValue;
    }


    private List<? extends ActivitySample> getSamplesOfDay(DBHandler db, Calendar day, int offsetHours, GBDevice device) {
        int startTs;
        int endTs;

        day = (Calendar) day.clone(); // do not modify the caller's argument
        day.set(Calendar.HOUR_OF_DAY, 0);
        day.set(Calendar.MINUTE, 0);
        day.set(Calendar.SECOND, 0);
        day.add(Calendar.HOUR, offsetHours);

        startTs = (int) (day.getTimeInMillis() / 1000);
        endTs = startTs + 24 * 60 * 60 - 1;

        return getSamples(db, device, startTs, endTs);
    }


    protected List<? extends ActivitySample> getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) {
        return getAllSamples(db, device, tsFrom, tsTo);
    }


    protected SampleProvider<? extends AbstractActivitySample> getProvider(DBHandler db, GBDevice device) {
        DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(device);
        return coordinator.getSampleProvider(device, db.getDaoSession());
    }


    protected List<? extends ActivitySample> getAllSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) {
        SampleProvider<? extends ActivitySample> provider = getProvider(db, device);
        return provider.getAllActivitySamples(tsFrom, tsTo);
    }
}

@@ -1593,17 +1593,16 @@
 l.GB;%0A%0A%0A
-%0A
 public c
@@ -1624,16 +1624,37 @@
 s %7B%0A    
+private static final 
 Logger L
@@ -2166,24 +2166,238 @@
  devices) %7B%0A
+                DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(device);%0A                if (!coordinator.supportsActivityDataFetching()) %7B%0A                    continue;%0A                %7D%0A
             
@@ -2850,21 +2850,16 @@
 ndar day
-%0A    
 ) %7B%0A%0A   
@@ -3021,23 +3021,16 @@
 ntsSteps
- = null
 ;%0A      
@@ -3067,15 +3067,8 @@
 leep
- = null
 ;%0A%0A 

Fix null pointer exception when a device which has no activity database is used in Gadgetbridge