package de.danoeh.antennapod.core.service.playback;

import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.feed.FeedPreferences;
import de.danoeh.antennapod.core.feed.VolumeAdaptionSetting;
import de.danoeh.antennapod.core.util.playback.Playable;

class PlaybackVolumeUpdater {

    public void updateVolumeIfNecessary(PlaybackServiceMediaPlayer mediaPlayer, long feedId,
                                        VolumeAdaptionSetting volumeAdaptionSetting) {
        Playable playable = mediaPlayer.getPlayable();
        boolean isFeedMedia = playable instanceof FeedMedia;
        boolean isPlayableLoaded = isPlayableLoaded(mediaPlayer.getPlayerStatus());

        if (isFeedMedia && isPlayableLoaded) {
            updateFeedMediaVolumeIfNecessary(mediaPlayer, feedId, volumeAdaptionSetting, (FeedMedia) playable);
        }
    }

    private void updateFeedMediaVolumeIfNecessary(PlaybackServiceMediaPlayer mediaPlayer, long feedId,
                                                  VolumeAdaptionSetting volumeAdaptionSetting, FeedMedia feedMedia) {
        if (feedMedia.getItem().getFeed().getId() == feedId) {
            FeedPreferences preferences = feedMedia.getItem().getFeed().getPreferences();
            preferences.setVolumeAdaptionSetting(volumeAdaptionSetting);

            if (mediaPlayer.getPlayerStatus() == PlayerStatus.PLAYING) {
                forceUpdateVolume(mediaPlayer);
            }
        }
    }

    private static boolean isPlayableLoaded(PlayerStatus playerStatus) {
        return playerStatus == PlayerStatus.PLAYING
                || playerStatus == PlayerStatus.PAUSED
                || playerStatus == PlayerStatus.SEEKING
                || playerStatus == PlayerStatus.PREPARING
                || playerStatus == PlayerStatus.PREPARED
                || playerStatus == PlayerStatus.INITIALIZING;
    }

    private void forceUpdateVolume(PlaybackServiceMediaPlayer mediaPlayer) {
        mediaPlayer.pause(false, false);
        mediaPlayer.resume();
    }

}

@@ -535,24 +535,25 @@
 able();%0A
+%0A
         
 boolean 
@@ -548,30 +548,12 @@
     
-boolean isFeedMedia = 
+if (
 play
@@ -581,138 +581,8 @@
 edia
-;%0A        boolean isPlayableLoaded = isPlayableLoaded(mediaPlayer.getPlayerStatus());%0A%0A        if (isFeedMedia && isPlayableLoaded
 ) %7B%0A
@@ -1314,428 +1314,8 @@
  %7D%0A%0A
-    private static boolean isPlayableLoaded(PlayerStatus playerStatus) %7B%0A        return playerStatus == PlayerStatus.PLAYING%0A                %7C%7C playerStatus == PlayerStatus.PAUSED%0A                %7C%7C playerStatus == PlayerStatus.SEEKING%0A                %7C%7C playerStatus == PlayerStatus.PREPARING%0A                %7C%7C playerStatus == PlayerStatus.PREPARED%0A                %7C%7C playerStatus == PlayerStatus.INITIALIZING;%0A    %7D%0A%0A
     

Fix volume adaption while paused