package com.mypodcasts.player;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.MotionEvent;

import com.mypodcasts.R;
import com.mypodcasts.repositories.models.Episode;

import javax.inject.Inject;

import de.greenrobot.event.EventBus;
import roboguice.activity.RoboActionBarActivity;
import roboguice.inject.ContentView;
import roboguice.inject.InjectView;

import static java.lang.String.format;

@ContentView(R.layout.audio_player)
public class AudioPlayerActivity extends RoboActionBarActivity {

  @InjectView(R.id.tool_bar)
  private Toolbar toolbar;

  @Inject
  private EventBus eventBus;
  private AudioPlayer audioPlayer;

  @Inject
  private AudioPlayerController mediaController;

  @Inject
  private ProgressDialog progressDialog;

  private Episode episode;
  private int playerCurrentPosition;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    eventBus.register(this);

    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    showProgressDialog();
    playAudio();
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) { onBackPressed(); }

    return super.onOptionsItemSelected(item);
  }

  @Override
  protected void onStop() {
    super.onStop();

    eventBus.unregister(this);
  }

  @Override
  protected void onPause() {
    super.onPause();

    if (audioPlayer != null) {
      playerCurrentPosition = audioPlayer.getCurrentPosition();
    }
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    mediaController.show();

    return super.onTouchEvent(event);
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt(AudioPlayer.class.toString(), playerCurrentPosition);
  }

  @Override
  protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    playerCurrentPosition = savedInstanceState.getInt(AudioPlayer.class.toString());
  }

  public void onEvent(AudioPlayingEvent event){
    dismissProgressDialog();

    audioPlayer = event.getAudioPlayer();
    audioPlayer.seekTo(playerCurrentPosition);

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

  private void showProgressDialog() {
    String episodeTitle = getEpisode() == null ? "" : getEpisode().getTitle();

    progressDialog.show();
    progressDialog.setMessage(
        format(
            getResources().getString(R.string.loading_episode),
            episodeTitle
        )
    );
  }

  private void dismissProgressDialog() {
    if (progressDialog != null && progressDialog.isShowing()) {
      progressDialog.dismiss();
    }
  }

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

    intent.putExtra(Episode.class.toString(), getEpisode());
    stopService(intent);
    startService(intent);
  }

  private Episode getEpisode() {
    if (episode == null) {
      episode = (Episode) getIntent().getSerializableExtra(Episode.class.toString());
    }

    return episode;
  }
}

@@ -91,16 +91,45 @@
 Intent;%0A
+import android.os.AsyncTask;%0A
 import a
@@ -1208,43 +1208,40 @@
     
-showProgressDialog();%0A    playAudio
+new PlayAudioAsyncTask().execute
 ();%0A
@@ -2582,31 +2582,446 @@
 oid 
-showProgressDialog() %7B%0A
+dismissProgressDialog() %7B%0A    if (progressDialog != null && progressDialog.isShowing()) %7B%0A      progressDialog.dismiss();%0A    %7D%0A  %7D%0A%0A  private class PlayAudioAsyncTask extends AsyncTask%3CVoid, Void, Episode%3E %7B%0A    @Override%0A    protected void onPreExecute() %7B%0A      showProgressDialog();%0A    %7D%0A%0A    @Override%0A    protected Episode doInBackground(Void... params) %7B%0A      return playAudio();%0A    %7D%0A%0A    private void showProgressDialog() %7B%0A  
     
@@ -3096,24 +3096,26 @@
 tle();%0A%0A    
+  
 progressDial
@@ -3121,24 +3121,26 @@
 log.show();%0A
+  
     progress
@@ -3158,16 +3158,18 @@
 essage(%0A
+  
         
@@ -3188,16 +3188,18 @@
         
+  
 getResou
@@ -3250,24 +3250,26 @@
             
+  
 episodeTitle
@@ -3279,158 +3279,20 @@
     
-  )%0A
     )
-;
 %0A  
-%7D%0A%0A  private void dismissProgressDialog() %7B%0A    if (progressDialog != null && progressDialog.isShowing()) %7B%0A      progressDialog.dismiss(
+    
 );%0A 
@@ -3288,37 +3288,35 @@
 %0A      );%0A    %7D%0A
+%0A
   
-%7D%0A%0A
   private void p
@@ -3309,20 +3309,23 @@
 private 
-void
+Episode
  playAud
@@ -3335,16 +3335,18 @@
 ) %7B%0A    
+  
 Intent i
@@ -3365,32 +3365,34 @@
 Intent(%0A        
+  
 AudioPlayerActiv
@@ -3401,32 +3401,34 @@
 y.this,%0A        
+ 
+ 
 AudioPlayerServi
@@ -3440,20 +3440,62 @@
 ass%0A    
+  
 );%0A%0A
+      Episode episode = getEpisode();%0A  
     inte
@@ -3536,27 +3536,25 @@
 (), 
-getE
+e
 pisode
-()
 );%0A
+%0A
     
+  
 stop
@@ -3574,16 +3574,18 @@
 t);%0A    
+ 
+ 
 startSer
@@ -3598,21 +3598,73 @@
 ntent);%0A
+%0A
+      return AudioPlayerActivity.this.episode;%0A  
   %7D%0A%0A
+  
   privat
@@ -3688,24 +3688,26 @@
 ode() %7B%0A    
+  
 if (episode 
@@ -3713,24 +3713,26 @@
  == null) %7B%0A
+  
       episod
@@ -3805,27 +3805,31 @@
 ing());%0A    
+  
 %7D%0A%0A
+  
     return e
@@ -3836,14 +3836,20 @@
 pisode;%0A
+    %7D%0A
   %7D%0A%7D%0A

Create async task to trigger audio service