package com.battlelancer.seriesguide.ui.shows;

import android.content.Context;
import android.view.MenuItem;
import android.widget.PopupMenu;
import androidx.fragment.app.FragmentManager;
import com.battlelancer.seriesguide.R;
import com.battlelancer.seriesguide.SgApp;
import com.battlelancer.seriesguide.provider.SeriesGuideContract;
import com.battlelancer.seriesguide.provider.SgRoomDatabase;
import com.battlelancer.seriesguide.sync.SgSyncAdapter;
import com.battlelancer.seriesguide.ui.episodes.EpisodeTools;
import com.battlelancer.seriesguide.ui.lists.ManageListsDialogFragment;

/**
* Item click listener for show item popup menu.
*/
public class ShowMenuItemClickListener implements PopupMenu.OnMenuItemClickListener {

    private final Context context;
    private final ShowTools showTools;
    private final FragmentManager fragmentManager;
    private final long showId;
    private final long nextEpisodeId;

    public ShowMenuItemClickListener(Context context, FragmentManager fm, long showId,
            long nextEpisodeId) {
        this.context = context;
        this.fragmentManager = fm;
        this.showId = showId;
        this.nextEpisodeId = nextEpisodeId;
        this.showTools = SgApp.getServicesComponent(context).showTools();
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        int showTvdbId = SgRoomDatabase.getInstance(context).sgShow2Helper().getShowTvdbId(showId);
        switch (item.getItemId()) {
            case R.id.menu_action_shows_watched_next: {
                EpisodeTools.episodeWatchedIfNotZero(context, nextEpisodeId);
                return true;
            }
            case R.id.menu_action_shows_favorites_add: {
                showTools.storeIsFavorite(showTvdbId, true);
                return true;
            }
            case R.id.menu_action_shows_favorites_remove: {
                showTools.storeIsFavorite(showTvdbId, false);
                return true;
            }
            case R.id.menu_action_shows_hide: {
                showTools.storeIsHidden(showId, true);
                return true;
            }
            case R.id.menu_action_shows_unhide: {
                showTools.storeIsHidden(showId, false);
                return true;
            }
            case R.id.menu_action_shows_manage_lists: {
                ManageListsDialogFragment.show(fragmentManager, showTvdbId,
                        SeriesGuideContract.ListItemTypes.SHOW);
                return true;
            }
            case R.id.menu_action_shows_update: {
                SgSyncAdapter.requestSyncSingleImmediate(context, true, showTvdbId);
                return true;
            }
            case R.id.menu_action_shows_remove: {
                RemoveShowDialogFragment.show(context, fragmentManager, showTvdbId);
                return true;
            }
            default:
                return false;
        }
    }

}

@@ -1440,16 +1440,21 @@
     
-switch (
+int itemId = 
 item
@@ -1465,19 +1465,17 @@
 ItemId()
-) %7B
+;
 %0A       
@@ -1471,32 +1471,37 @@
 ();%0A        
-    case
+if (itemId ==
  R.id.menu_a
@@ -1524,24 +1524,20 @@
 hed_next
-:
+)
  %7B%0A
-    
         
@@ -1606,36 +1606,32 @@
 d);%0A            
-    
 return true;%0A   
@@ -1635,38 +1635,36 @@
         
-   
- %7D%0A            case
+%7D else if (itemId ==
  R.id.me
@@ -1692,24 +1692,20 @@
 ites_add
-:
+)
  %7B%0A
-    
         
@@ -1757,36 +1757,32 @@
 e);%0A            
- 
-   
 return true;%0A   
@@ -1786,38 +1786,36 @@
         
-    %7D%0A            case
+%7D else if (itemId ==
  R.id.me
@@ -1838,33 +1838,33 @@
 favorites_remove
-:
+)
  %7B%0A             
@@ -1854,36 +1854,32 @@
 ) %7B%0A            
-    
 showTools.storeI
@@ -1912,36 +1912,32 @@
 e);%0A            
-    
 return true;%0A   
@@ -1941,38 +1941,36 @@
         
-    %7D%0A            case
+%7D else if (itemId ==
  R.id.me
@@ -1985,32 +1985,28 @@
 n_shows_hide
-:
+)
  %7B%0A
-    
             
@@ -2048,36 +2048,32 @@
 e);%0A            
-  
-  
 return true;%0A   
@@ -2077,38 +2077,36 @@
         
-    %7D%0A            case
+%7D else if (itemId ==
  R.id.me
@@ -2127,17 +2127,17 @@
 s_unhide
-:
+)
  %7B%0A     
@@ -2135,36 +2135,32 @@
 ) %7B%0A            
- 
-   
 showTools.storeI
@@ -2187,36 +2187,32 @@
 e);%0A            
-    
 return true;%0A   
@@ -2216,38 +2216,36 @@
         
-    %7D%0A            case
+%7D else if (itemId ==
  R.id.me
@@ -2272,24 +2272,20 @@
 ge_lists
-:
+)
  %7B%0A
-    
         
@@ -2368,20 +2368,16 @@
         
-   
- 
 SeriesGu
@@ -2413,36 +2413,32 @@
 W);%0A            
-    
 return true;%0A   
@@ -2442,38 +2442,36 @@
         
-    %7D%0A            case
+%7D else if (itemId ==
  R.id.me
@@ -2492,24 +2492,20 @@
 s_update
-:
+)
  %7B%0A
-    
         
@@ -2581,36 +2581,32 @@
 d);%0A            
-    
 return true;%0A   
@@ -2614,30 +2614,28 @@
     
-    %7D%0A            case
+%7D else if (itemId ==
  R.i
@@ -2660,17 +2660,17 @@
 s_remove
-:
+)
  %7B%0A     
@@ -2676,20 +2676,16 @@
         
-   
- 
 RemoveSh
@@ -2749,36 +2749,32 @@
 d);%0A            
-    
 return true;%0A   
@@ -2782,43 +2782,10 @@
     
-    %7D%0A            default:%0A        
+%7D%0A
     
@@ -2802,26 +2802,16 @@
  false;%0A
-        %7D%0A
     %7D%0A%0A%7D

replace switch with if for view IDs.