package de.danoeh.antennapod.dialog;

import android.content.Context;

import androidx.appcompat.app.AlertDialog;

import org.greenrobot.eventbus.EventBus;

import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.event.UnreadItemsUpdateEvent;
import de.danoeh.antennapod.core.preferences.UserPreferences;

public class FeedFilterDialog {
    public static void showDialog(Context context) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle(context.getString(R.string.pref_filter_feed_title));
        dialog.setNegativeButton(android.R.string.cancel, (d, listener) -> d.dismiss());

        int selectedIndexTemp = 0;
        int selected = UserPreferences.getFeedFilter();
        String[] entryValues = context.getResources().getStringArray(R.array.nav_drawer_feed_filter_values);
        for (int i = 0; i < entryValues.length; i++) {
            if (Integer.parseInt(entryValues[i]) == selected) {
                selectedIndexTemp = i;
            }
        }

        final int selectedIndex = selectedIndexTemp;
        String[] items = context.getResources().getStringArray(R.array.nav_drawer_feed_filter_options);
        dialog.setSingleChoiceItems(items, selectedIndex, (d, which) -> {
            if (selectedIndex != which) {
                UserPreferences.setFeedFilter(entryValues[which]);
                //Update subscriptions
                EventBus.getDefault().post(new UnreadItemsUpdateEvent());
            }
            d.dismiss();
        });
        dialog.show();
    }
}

@@ -150,16 +150,65 @@
 ntBus;%0A%0A
+import java.util.Arrays;%0Aimport java.util.List;%0A%0A
 import d
@@ -683,43 +683,8 @@
 );%0A%0A
-        int selectedIndexTemp = 0;%0A
     
@@ -743,24 +743,28 @@
         
+List%3C
 String
-%5B%5D
+%3E
  entryVa
@@ -769,17 +769,47 @@
 Values =
- 
+%0A                Arrays.asList(
 context.
@@ -876,16 +876,17 @@
 _values)
+)
 ;%0A      
@@ -892,234 +892,69 @@
    f
-or (int i = 0; i %3C entryValues.length; i++) %7B%0A            if (Integer.parseInt(entryValues%5Bi%5D) == selected) %7B%0A                selectedIndexTemp = i;%0A            %7D%0A        %7D%0A%0A        final int selectedIndex = selectedIndexTemp;
+inal int selectedIndex = entryValues.indexOf(%22%22 + selected);%0A
 %0A   
@@ -1231,15 +1231,19 @@
 lues
-%5B
+.get(
 which
-%5D
+)
 );%0A 

update FeedFilterDialog