package de.danoeh.antennapod.receiver;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import org.apache.commons.lang3.StringUtils;

import de.danoeh.antennapod.core.BuildConfig;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.service.PlayerWidgetService;

public class PlayerWidget extends AppWidgetProvider {
    private static final String TAG = "PlayerWidget";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "onReceive");
        super.onReceive(context, intent);
        if (StringUtils.equals(intent.getAction(), PlaybackService.FORCE_WIDGET_UPDATE)) {
            startUpdate(context);
        } else if (StringUtils.equals(intent.getAction(), PlaybackService.STOP_WIDGET_UPDATE)) {
            stopUpdate(context);
        }

    }

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        Log.d(TAG, "Widget enabled");
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                         int[] appWidgetIds) {
        Log.d(TAG, "onUpdate() called with: " + "context = [" + context + "], appWidgetManager = [" + appWidgetManager + "], appWidgetIds = [" + appWidgetIds + "]");
        startUpdate(context);
    }

    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);
        stopUpdate(context);
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
        stopUpdate(context);
    }

    private void startUpdate(Context context) {
        Log.d(TAG, "startUpdate() called with: " + "context = [" + context + "]");
        context.startService(new Intent(context, PlayerWidgetService.class));
    }

    private void stopUpdate(Context context) {
        Log.d(TAG, "stopUpdate() called with: " + "context = [" + context + "]");
        context.stopService(new Intent(context, PlayerWidgetService.class));
    }

}

@@ -543,152 +543,489 @@
     
-@Override%0A    public void onReceive(Context context, Intent intent) %7B%0A        Log.d(TAG, %22onReceive%22);%0A        super.onReceive(context, intent);
+// static because there should only ever be one widget...%0A    // and otherwise it just keeps getting reset when it gets messages%0A    private static boolean enabled = false;%0A%0A    @Override%0A    public void onReceive(Context context, Intent intent) %7B%0A        Log.d(TAG, %22onReceive%22);%0A        super.onReceive(context, intent);%0A        if (!enabled) %7B%0A            // do nothing%0A            return;%0A        %7D%0A%0A        // these come from the PlaybackService when things should get updated
 %0A   
@@ -1282,25 +1282,24 @@
 ;%0A        %7D%0A
-%0A
     %7D%0A%0A    @
@@ -1420,24 +1420,78 @@
  enabled%22);%0A
+        enabled = true;%0A        startUpdate(context);%0A
     %7D%0A%0A    @
@@ -1933,161 +1933,61 @@
     
-stopUpdate(context);%0A    %7D%0A%0A    @Override%0A    public void onDeleted(Context context, int%5B%5D appWidgetIds) %7B%0A        super.onDeleted(context, appWidgetIds)
+Log.d(TAG, %22Widet disabled%22);%0A        enabled = false
 ;%0A  
@@ -2448,11 +2448,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

enable and disable the widget as needed