// Path: app/src/main/java/com/mypodcasts/episodes/EpisodeCheckpoint.java
// public class EpisodeCheckpoint {
//   public static final String STATE = EpisodeCheckpoint.class.toString();
//   private final Context context;
// 
//   @Inject
//   public EpisodeCheckpoint(Context context) {
//     this.context = context;
//   }
// 
//   public int markCheckpoint(Episode episode, int currentPosition) {
//     SharedPreferences.Editor editor = getSharedPreferences().edit();
// 
//     Log.i(MYPODCASTS_TAG, "Mark playing episode checkpoint: " + episode);
// 
//     editor.putInt(episodeKey(episode), currentPosition).apply();
// 
//     return currentPosition;
//   }
// 
//   public int getLastCheckpointPosition(Episode episode, int defaultPosition) {
//     SharedPreferences sharedPreferences = getSharedPreferences();
// 
//     return sharedPreferences.getInt(episodeKey(episode), defaultPosition);
//   }
// 
//   private SharedPreferences getSharedPreferences() {
//     return context.getSharedPreferences(STATE, MODE_PRIVATE);
//   }
// 
//   private String episodeKey(Episode episode) {
//     return format(
//         "podcast_%s#episode_%s",
//         episode.getFeed().getId(),
//         replaceFromSpaceToUnderscore(episode.getTitle())
//     );
//   }
// 
//   private String replaceFromSpaceToUnderscore(String value) {
//     return value.toLowerCase().replace(" ", "_");
//   }
// }
// 
// Path: app/src/main/java/com/mypodcasts/player/events/AudioPlayingEvent.java
// public class AudioPlayingEvent {
//   private final AudioPlayer audioPlayer;
// 
//   public AudioPlayingEvent(AudioPlayer audioPlayer) {
//     this.audioPlayer = audioPlayer;
//   }
// 
//   public AudioPlayer getAudioPlayer() {
//     return audioPlayer;
//   }
// }
// 
// Path: app/src/main/java/com/mypodcasts/player/events/AudioStoppedEvent.java
// public class AudioStoppedEvent {}
// 
// Path: app/src/main/java/com/mypodcasts/repositories/models/Episode.java
// public class Episode implements Serializable {
// 
//   private String title;
//   private String publishedDate;
//   private String description;
//   private String duration;
//   private Audio audio;
//   private Feed podcast;
//   private Image image;
// 
//   public Image getImage() {
//     return image;
//   }
// 
//   public String getTitle() {
//     return title;
//   }
// 
//   public Date getPublishedDate() {
//     if (publishedDate == null) { return null; }
// 
//     return dateTimeParser().parseDateTime(publishedDate).toDate();
//   }
// 
//   public String getDescription() {
//     return description == null ? "" : description;
//   }
// 
//   public String getDuration() {
//     return duration;
//   }
// 
//   public Feed getFeed() {
//     if (podcast == null) { return new Feed(); }
// 
//     return podcast;
//   }
// 
//   public String getAudioFilePath() {
//     return getFeed().getId() + "/" + getTitle() + ".mp3";
//   }
// 
//   public String getAudioUrl() {
//     return getAudio().getUrl();
//   }
// 
//   public String getAudioLength() {
//     return getAudio().getLength();
//   }
// 
//   protected Audio getAudio() {
//     if (audio == null) { return new EmptyAudio(); }
// 
//     return audio;
//   }
// 
//   @Override
//   public String toString() {
//     return format(
//         "{ \"title\": %s, " +
//             "\"audioFilePath\": %s, " +
//             "\"feed\": { \"id\": %s, \"title\": %s } " +
//         "}",
//         getTitle(),
//         getAudioFilePath(),
//         getFeed().getId(),
//         getFeed().getTitle()
//     );
//   }
// 
//   private class EmptyAudio extends Audio {
//     @Override
//     public String getUrl() {
//       return "";
//     }
// 
//     @Override
//     public String getLength() {
//       return "0";
//     }
//   }
// }
// 
// Path: app/src/main/java/com/mypodcasts/support/Support.java
// public class Support {
//   public static final String MYPODCASTS_TAG = "[mypodcasts-android]";
// }
// 
// Path: app/src/main/java/com/mypodcasts/player/AudioPlayerService.java
// public static final String ACTION_PLAY = "com.mypodcasts.player.action.play";
// 
// Path: app/src/main/java/com/mypodcasts/support/Support.java
// public static final String MYPODCASTS_TAG = "[mypodcasts-android]";


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.TextView;
import com.mypodcasts.R;
import com.mypodcasts.episodes.EpisodeCheckpoint;
import com.mypodcasts.player.events.AudioPlayingEvent;
import com.mypodcasts.player.events.AudioStoppedEvent;
import com.mypodcasts.repositories.models.Episode;
import com.mypodcasts.support.Support;
import javax.inject.Inject;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import roboguice.activity.RoboActionBarActivity;
import roboguice.inject.ContentView;
import roboguice.inject.InjectView;
import static android.text.Html.fromHtml;
import static com.mypodcasts.player.AudioPlayerService.ACTION_PLAY;
import static com.mypodcasts.support.Support.MYPODCASTS_TAG;
  @Subscribe
  public void onEvent(AudioPlayingEvent event) {
    Log.d(Support.MYPODCASTS_TAG, "[AudioPlayerActivity][onEvent][AudioPlayingEvent]");

    audioPlayer = event.getAudioPlayer();
    audioPlayer.seekTo(
        episodeCheckpoint.getLastCheckpointPosition(episode, playerCurrentPosition)
    );

    mediaController.setMediaPlayer(audioPlayer);
    mediaController.setAnchorView(findViewById(R.id.audio_view));
    mediaController.show();

    if (episode != null) {
      episodeDescription.setText(fromHtml(episode.getDescription()));
    }
  }

  @Subscribe
  public void onEvent(AudioStoppedEvent event) {
    Log.d(Support.MYPODCASTS_TAG, "[AudioPlayerActivity][onEvent][AudioStoppedEvent]");

    finish();
  }

  private Episode playAudio() {
    Intent intent = new Intent(AudioPlayerActivity.this, AudioPlayerService.class);
    intent.setAction(ACTION_PLAY);

    Episode episode = getEpisode();
// Path: app/src/main/java/com/mypodcasts/episodes/EpisodeCheckpoint.java
// public class EpisodeCheckpoint {
//   public static final String STATE = EpisodeCheckpoint.class.toString();
//   private final Context context;
// 
//   @Inject
//   public EpisodeCheckpoint(Context context) {
//     this.context = context;
//   }
// 
//   public int markCheckpoint(Episode episode, int currentPosition) {
//     SharedPreferences.Editor editor = getSharedPreferences().edit();
// 
//     Log.i(MYPODCASTS_TAG, "Mark playing episode checkpoint: " + episode);
// 
//     editor.putInt(episodeKey(episode), currentPosition).apply();
// 
//     return currentPosition;
//   }
// 
//   public int getLastCheckpointPosition(Episode episode, int defaultPosition) {
//     SharedPreferences sharedPreferences = getSharedPreferences();
// 
//     return sharedPreferences.getInt(episodeKey(episode), defaultPosition);
//   }
// 
//   private SharedPreferences getSharedPreferences() {
//     return context.getSharedPreferences(STATE, MODE_PRIVATE);
//   }
// 
//   private String episodeKey(Episode episode) {
//     return format(
//         "podcast_%s#episode_%s",
//         episode.getFeed().getId(),
//         replaceFromSpaceToUnderscore(episode.getTitle())
//     );
//   }
// 
//   private String replaceFromSpaceToUnderscore(String value) {
//     return value.toLowerCase().replace(" ", "_");
//   }
// }
// 
// Path: app/src/main/java/com/mypodcasts/player/events/AudioPlayingEvent.java
// public class AudioPlayingEvent {
//   private final AudioPlayer audioPlayer;
// 
//   public AudioPlayingEvent(AudioPlayer audioPlayer) {
//     this.audioPlayer = audioPlayer;
//   }
// 
//   public AudioPlayer getAudioPlayer() {
//     return audioPlayer;
//   }
// }
// 
// Path: app/src/main/java/com/mypodcasts/player/events/AudioStoppedEvent.java
// public class AudioStoppedEvent {}
// 
// Path: app/src/main/java/com/mypodcasts/repositories/models/Episode.java
// public class Episode implements Serializable {
// 
//   private String title;
//   private String publishedDate;
//   private String description;
//   private String duration;
//   private Audio audio;
//   private Feed podcast;
//   private Image image;
// 
//   public Image getImage() {
//     return image;
//   }
// 
//   public String getTitle() {
//     return title;
//   }
// 
//   public Date getPublishedDate() {
//     if (publishedDate == null) { return null; }
// 
//     return dateTimeParser().parseDateTime(publishedDate).toDate();
//   }
// 
//   public String getDescription() {
//     return description == null ? "" : description;
//   }
// 
//   public String getDuration() {
//     return duration;
//   }
// 
//   public Feed getFeed() {
//     if (podcast == null) { return new Feed(); }
// 
//     return podcast;
//   }
// 
//   public String getAudioFilePath() {
//     return getFeed().getId() + "/" + getTitle() + ".mp3";
//   }
// 
//   public String getAudioUrl() {
//     return getAudio().getUrl();
//   }
// 
//   public String getAudioLength() {
//     return getAudio().getLength();
//   }
// 
//   protected Audio getAudio() {
//     if (audio == null) { return new EmptyAudio(); }
// 
//     return audio;
//   }
// 
//   @Override
//   public String toString() {
//     return format(
//         "{ \"title\": %s, " +
//             "\"audioFilePath\": %s, " +
//             "\"feed\": { \"id\": %s, \"title\": %s } " +
//         "}",
//         getTitle(),
//         getAudioFilePath(),
//         getFeed().getId(),
//         getFeed().getTitle()
//     );
//   }
// 
//   private class EmptyAudio extends Audio {
//     @Override
//     public String getUrl() {
//       return "";
//     }
// 
//     @Override
//     public String getLength() {
//       return "0";
//     }
//   }
// }
// 
// Path: app/src/main/java/com/mypodcasts/support/Support.java
// public class Support {
//   public static final String MYPODCASTS_TAG = "[mypodcasts-android]";
// }
// 
// Path: app/src/main/java/com/mypodcasts/player/AudioPlayerService.java
// public static final String ACTION_PLAY = "com.mypodcasts.player.action.play";
// 
// Path: app/src/main/java/com/mypodcasts/support/Support.java
// public static final String MYPODCASTS_TAG = "[mypodcasts-android]";


// Path: app/src/main/java/com/mypodcasts/player/AudioPlayerActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.TextView;
import com.mypodcasts.R;
import com.mypodcasts.episodes.EpisodeCheckpoint;
import com.mypodcasts.player.events.AudioPlayingEvent;
import com.mypodcasts.player.events.AudioStoppedEvent;
import com.mypodcasts.repositories.models.Episode;
import com.mypodcasts.support.Support;
import javax.inject.Inject;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import roboguice.activity.RoboActionBarActivity;
import roboguice.inject.ContentView;
import roboguice.inject.InjectView;
import static android.text.Html.fromHtml;
import static com.mypodcasts.player.AudioPlayerService.ACTION_PLAY;
import static com.mypodcasts.support.Support.MYPODCASTS_TAG;

  @Subscribe
  public void onEvent(AudioPlayingEvent event) {
    Log.d(Support.MYPODCASTS_TAG, "[AudioPlayerActivity][onEvent][AudioPlayingEvent]");

    audioPlayer = event.getAudioPlayer();
    audioPlayer.seekTo(
        episodeCheckpoint.getLastCheckpointPosition(episode, playerCurrentPosition)
    );

    mediaController.setMediaPlayer(audioPlayer);
    mediaController.setAnchorView(findViewById(R.id.audio_view));
    mediaController.show();

    if (episode != null) {
      episodeDescription.setText(fromHtml(episode.getDescription()));
    }
  }

  @Subscribe
  public void onEvent(AudioStoppedEvent event) {
    Log.d(Support.MYPODCASTS_TAG, "[AudioPlayerActivity][onEvent][AudioStoppedEvent]");

    finish();
  }

  private Episode playAudio() {
    Intent intent = new Intent(AudioPlayerActivity.this, AudioPlayerService.class);
    intent.setAction(ACTION_PLAY);

    Episode episode = getEpisode();
    intent.putExtra(Episode.class.toString(), episode);