package nerd.tuxmobil.fahrplan.congress.system;

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.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.preference.PreferenceManager;
import android.text.format.Time;

import org.ligi.tracedroid.logging.Log;

import nerd.tuxmobil.fahrplan.congress.MyApp;
import nerd.tuxmobil.fahrplan.congress.alarms.AlarmReceiver;
import nerd.tuxmobil.fahrplan.congress.autoupdate.UpdateService;
import nerd.tuxmobil.fahrplan.congress.net.ConnectivityStateReceiver;
import nerd.tuxmobil.fahrplan.congress.persistence.AlarmsDBOpenHelper;
import nerd.tuxmobil.fahrplan.congress.persistence.FahrplanContract.AlarmsTable;
import nerd.tuxmobil.fahrplan.congress.utils.FahrplanMisc;

public final class OnBootReceiver extends BroadcastReceiver {

    final String LOG_TAG = "onBoot";

    @Override
    public void onReceive(Context context, Intent intent) {
        MyApp.LogDebug(LOG_TAG, "onReceive (reboot)");
        AlarmsDBOpenHelper lecturesDB = new AlarmsDBOpenHelper(context);

        SQLiteDatabase db = lecturesDB.getWritableDatabase();
        Cursor cursor;

        try {
            cursor = db.query(AlarmsTable.NAME, AlarmsDBOpenHelper.allcolumns,
                    null, null, null, null, null);
        } catch (SQLiteException e) {
            e.printStackTrace();
            db.close();
            return;
        }

        Time now = new Time();
        Time storedAlarmTime = new Time();
        now.setToNow();
        now.second += 15;
        now.normalize(true);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            long alarmTime = cursor.getLong(cursor.getColumnIndex(AlarmsTable.Columns.TIME));
            storedAlarmTime.set(alarmTime);

            if (now.before(storedAlarmTime)) {
                // set alarm

                String lecture_id = cursor.getString(
                        cursor.getColumnIndex(AlarmsTable.Columns.EVENT_ID));
                int day = cursor.getInt(
                        cursor.getColumnIndex(AlarmsTable.Columns.DAY));
                String title = cursor.getString(
                        cursor.getColumnIndex(AlarmsTable.Columns.EVENT_TITLE));
                long startTime = cursor.getLong(
                        cursor.getColumnIndex(AlarmsTable.Columns.TIME));
                Log.d(getClass().getName(), "Set alarm for lecture: " + lecture_id);

                Intent addAlarmIntent = new AlarmReceiver.AlarmIntentBuilder()
                        .setContext(context)
                        .setLectureId(lecture_id)
                        .setDay(day)
                        .setTitle(title)
                        .setStartTime(startTime)
                        .setIsAddAlarm()
                        .build();

                PendingIntent pendingintent = PendingIntent.getBroadcast(
                        context, Integer.parseInt(lecture_id), addAlarmIntent, 0);
                MyApp.LogDebug(LOG_TAG, "add alarm for " + title);
                // Set new alarm
                alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingintent);
            } else {
                // remove from DB

                MyApp.LogDebug(LOG_TAG, "remove alarm from DB ");

                String id = cursor.getString(0);
                db.delete(AlarmsTable.NAME, AlarmsTable.Columns.ID + " = ?", new String[]{id});
            }

            cursor.moveToNext();
        }
        cursor.close();
        db.close();

        if (ConnectivityStateReceiver.isEnabled(context)) {
            ConnectivityStateReceiver.disableReceiver(context);
        }

        // start auto updates
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        boolean do_auto_updates = prefs.getBoolean("auto_update", false);
        if (do_auto_updates) {
            long last_fetch = prefs.getLong("last_fetch", 0);
            long now_millis;
            now.setToNow();
            now_millis = now.toMillis(true);

            long interval = FahrplanMisc.setUpdateAlarm(context, true);

            MyApp.LogDebug(LOG_TAG, "now: " + now_millis + ", last_fetch: " + last_fetch);
            if ((interval > 0) && (now_millis - last_fetch >= interval)) {
                UpdateService.start(context);
            }
        }
    }
}

@@ -4105,23 +4105,21 @@
 olean do
-_auto_u
+AutoU
 pdates =
@@ -4177,15 +4177,13 @@
  (do
-_auto_u
+AutoU
 pdat
@@ -4205,26 +4205,25 @@
    long last
-_f
+F
 etch = prefs
@@ -4265,26 +4265,25 @@
     long now
-_m
+M
 illis;%0A     
@@ -4316,26 +4316,25 @@
          now
-_m
+M
 illis = now.
@@ -4472,18 +4472,17 @@
  %22 + now
-_m
+M
 illis + 
@@ -4500,26 +4500,25 @@
 ch: %22 + last
-_f
+F
 etch);%0A     
@@ -4554,10 +4554,9 @@
 (now
-_m
+M
 illi
@@ -4559,26 +4559,25 @@
 illis - last
-_f
+F
 etch %3E= inte

Use camel-case.