package com.novoda.downloadmanager;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.util.Log;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class LiteDownloadMigrationService extends Service {

    private static final String TAG = "MigrationService";
    private static ExecutorService executor;

    private Migrator v1ToV2Migrator;
    private IBinder binder;
    private Migrator.Callback migrationCallback;
    private String updateMessage;

    private static boolean isBound;

    public static boolean isBound() {
        return isBound;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        Log.d(TAG, "onStartCommand");

        executor.execute(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "Begin Migration");
                v1ToV2Migrator.migrate();
            }
        });

        return START_STICKY;
    }

    @Override
    public void onCreate() {
        Log.d(TAG, "onCreate");
        if (executor == null) {
            executor = Executors.newSingleThreadExecutor();
        }

        binder = new MigrationDownloadServiceBinder();
        v1ToV2Migrator = MigrationFactory.createVersionOneToVersionTwoMigrator(
                getApplicationContext(),
                getDatabasePath("downloads.db"),
                migrationCallback()
        );

        super.onCreate();
    }

    private Migrator.Callback migrationCallback() {
        return new Migrator.Callback() {
            @Override
            public void onUpdate(String message) {
                updateMessage = message;
                if (migrationCallback != null) {
                    migrationCallback.onUpdate(message);
                }
            }
        };
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        isBound = true;
        return binder;
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        Log.d(TAG, "onTaskRemoved");
        rescheduleMigration();
        Log.d(TAG, "Rescheduling");
        super.onTaskRemoved(rootIntent);
    }

    private void rescheduleMigration() {
        Intent intent = new Intent(getApplicationContext(), LiteDownloadMigrationService.class);
        PendingIntent pendingIntent = PendingIntent.getService(this, 1, intent, PendingIntent.FLAG_ONE_SHOT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        if (alarmManager == null) {
            Log.w(TAG, "Could not retrieve AlarmManager for rescheduling.");
            return;
        }
        alarmManager.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() + 5000, pendingIntent);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(TAG, "onUnbind");
        migrationCallback = null;
        Log.d(TAG, "Stopping service");
        isBound = false;
        return super.onUnbind(intent);
    }

    class MigrationDownloadServiceBinder extends Binder {

        MigrationDownloadServiceBinder withCallback(Migrator.Callback migrationCallback) {
            LiteDownloadMigrationService.this.migrationCallback = migrationCallback;
            return this;
        }

        void bind() {
            LiteDownloadMigrationService.this.migrationCallback.onUpdate(LiteDownloadMigrationService.this.updateMessage);
        }

    }

}

@@ -596,45 +596,8 @@
 r;%0A%0A
-    private Migrator v1ToV2Migrator;%0A
     
@@ -1080,24 +1080,320 @@
 oid run() %7B%0A
+                Migrator migrator = MigrationFactory.createVersionOneToVersionTwoMigrator(%0A                        getApplicationContext(),%0A                        getDatabasePath(%22downloads.db%22),%0A                        migrationCallback()%0A                );%0A                migrator.migrate();%0A
             
@@ -1423,17 +1423,41 @@
 igration
-%22
+: %22 + migrator.getClass()
 );%0A     
@@ -1463,31 +1463,25 @@
             
-v1ToV2M
+m
 igrator.migr
@@ -1786,225 +1786,8 @@
 r();
-%0A        v1ToV2Migrator = MigrationFactory.createVersionOneToVersionTwoMigrator(%0A                getApplicationContext(),%0A                getDatabasePath(%22downloads.db%22),%0A                migrationCallback()%0A        );
 %0A%0A  

Create Migrator just before executing the task.