package info.nightscout.androidaps.receivers;

/**
 * Created by mike on 07.07.2016.
 */

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.PowerManager;
import android.preference.PreferenceManager;

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

import java.util.Date;

import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin;
import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin;
import info.nightscout.client.data.NSProfile;

public class KeepAliveReceiver extends BroadcastReceiver {
    private static Logger log = LoggerFactory.getLogger(KeepAliveReceiver.class);

    @Override
    public void onReceive(Context context, Intent rIntent) {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
        wl.acquire();


        final PumpInterface pump = MainApp.getConfigBuilder();
        final NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
        if (pump != null && profile != null) {
            boolean isBasalOutdated = false;
            boolean isStatusOutdated = false;

            Date lastConnection = pump.lastStatusTime();
            if (lastConnection.getTime() + 30 * 60 * 1000L < new Date().getTime())
                isStatusOutdated = true;
            if (Math.abs(profile.getBasal(NSProfile.secondsFromMidnight()) - pump.getBaseBasalRate()) > pump.getPumpDescription().basalStep)
                isBasalOutdated = true;

            SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
            if (SP.getBoolean("syncprofiletopump", false)) {
                Thread t = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        pump.setNewBasalProfile(profile);
                    }
                });
                t.start();
            } else if (isBasalOutdated) {
                Thread t = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        pump.updateStatus("KeepAlive. Basal outdated.");
                    }
                });
                t.start();
            } else if (isStatusOutdated) {
                Thread t = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        pump.updateStatus("KeepAlive. Status outdated.");
                    }
                });
                t.start();
            }
        }

        log.debug("KeepAlive received");
        final DanaRPlugin danaRPlugin = (DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class);
        if (danaRPlugin != null && Config.DANAR && danaRPlugin.isEnabled(PluginBase.PUMP)) {
            if (danaRPlugin.getDanaRPump().lastConnection.getTime() + 30 * 60 * 1000L < new Date().getTime() && !danaRPlugin.isConnected() && !danaRPlugin.isConnecting()) {
                Thread t = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        danaRPlugin.doConnect("KeepAlive. Basal outdated: ");
                    }
                });
                t.start();
            }
        }
        final DanaRKoreanPlugin danaRKoreanPlugin = (DanaRKoreanPlugin) MainApp.getSpecificPlugin(DanaRKoreanPlugin.class);
        if (danaRKoreanPlugin != null && Config.DANAR && danaRKoreanPlugin.isEnabled(PluginBase.PUMP)) {
            if (danaRKoreanPlugin.getDanaRPump().lastConnection.getTime() + 30 * 60 * 1000L < new Date().getTime() && !danaRKoreanPlugin.isConnected() && !danaRKoreanPlugin.isConnecting()) {
                Thread t = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        danaRKoreanPlugin.doConnect("KeepAlive. Basal outdated: ");
                    }
                });
                t.start();
            }
        }
        wl.release();
    }

    public void setAlarm(Context context) {
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, KeepAliveReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
        try {
            pi.send();
        } catch (PendingIntent.CanceledException e) {
        }
        am.cancel(pi);
        am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), Constants.keepAliveMsecs, pi);
    }

    public void cancelAlarm(Context context) {
        Intent intent = new Intent(context, KeepAliveReceiver.class);
        PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(sender);
    }
}
@@ -3091,1392 +3091,8 @@
 %22);%0A
-        final DanaRPlugin danaRPlugin = (DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class);%0A        if (danaRPlugin != null && Config.DANAR && danaRPlugin.isEnabled(PluginBase.PUMP)) %7B%0A            if (danaRPlugin.getDanaRPump().lastConnection.getTime() + 30 * 60 * 1000L %3C new Date().getTime() && !danaRPlugin.isConnected() && !danaRPlugin.isConnecting()) %7B%0A                Thread t = new Thread(new Runnable() %7B%0A                    @Override%0A                    public void run() %7B%0A                        danaRPlugin.doConnect(%22KeepAlive. Basal outdated: %22);%0A                    %7D%0A                %7D);%0A                t.start();%0A            %7D%0A        %7D%0A        final DanaRKoreanPlugin danaRKoreanPlugin = (DanaRKoreanPlugin) MainApp.getSpecificPlugin(DanaRKoreanPlugin.class);%0A        if (danaRKoreanPlugin != null && Config.DANAR && danaRKoreanPlugin.isEnabled(PluginBase.PUMP)) %7B%0A            if (danaRKoreanPlugin.getDanaRPump().lastConnection.getTime() + 30 * 60 * 1000L %3C new Date().getTime() && !danaRKoreanPlugin.isConnected() && !danaRKoreanPlugin.isConnecting()) %7B%0A                Thread t = new Thread(new Runnable() %7B%0A                    @Override%0A                    public void run() %7B%0A                        danaRKoreanPlugin.doConnect(%22KeepAlive. Basal outdated: %22);%0A                    %7D%0A                %7D);%0A                t.start();%0A            %7D%0A        %7D%0A
     

remove forgotten code