package com.example.utils01;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class PlayerActivity extends Activity{
	
	
	private MediaPlayer mediaPlayer;
	public TextView songName, duration, timer;
	private double timeElapsed = 0, finalTime = 0;
	private int forwardTime = 2000, backwardTime = 2000;
	private Handler durationHandler = new Handler();
	private SeekBar seekbar;
	private String mp3;
	
	 public void onCreate(Bundle savedInstanceState)
	    {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.player_main);
	        
	        /* OK valide*/
	       // MediaPlayer mp=MediaPlayer.create(this, R.raw.son);
	       // mp.start();
	        
	        
	       /* OK valide */
/*	        
	        MediaPlayer mPlayer = new MediaPlayer();
	        Uri myUri = Uri.parse("file:///sdcard/Enigma.mp3");
	        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
	        try {
				mPlayer.setDataSource(getApplicationContext(), myUri);
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        try {
				mPlayer.prepare();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        mPlayer.start();
*/	        
	        
	        Bundle extras = getIntent().getExtras();
            mp3 = extras.getString("name_mp3") + ".mp3";
	        
         
            
	        initializeViews();
	        
	    }     
	 
	 
	 public void initializeViews()
	 {
			songName = (TextView) findViewById(R.id.songName);
			
			String chemin = "file:///sdcard/" + mp3 ;
			Toast.makeText(getApplicationContext(), chemin, Toast.LENGTH_SHORT).show();
			//MediaPlayer player = MediaPlayer.create(this, Uri.parse("http://www.analog-design.net/MP3/abc.mp3"));
			
			
			
			chemin = "http://analog-design.net/MP3/" +mp3 ;
		    Uri myUri = Uri.parse(chemin);
		    mediaPlayer = MediaPlayer.create(this, myUri);

		    
		    
//		    mediaPlayer = MediaPlayer.create(this, Uri.parse("http://analog-design.net/MP3/xyz.mp3"));
		    

			
			//mediaPlayer = MediaPlayer.create(this, R.raw.son);
			finalTime = mediaPlayer.getDuration();
			
			duration = (TextView) findViewById(R.id.duration);
			timer = (TextView) findViewById(R.id.timer);
			
			seekbar = (SeekBar) findViewById(R.id.seekBar);
			songName.setText(mp3);
			
			seekbar.setMax((int) finalTime);
			seekbar.setClickable(false);
			
			duration.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes((long) finalTime), TimeUnit.MILLISECONDS.toSeconds((long) finalTime) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) finalTime))));
			
		} 

		// play mp3 song
		public void play(View view) {
			mediaPlayer.start();
			timeElapsed = mediaPlayer.getCurrentPosition();
			seekbar.setProgress((int) timeElapsed);
			durationHandler.postDelayed(updateSeekBarTime, 100);
		}

		//handler to change seekBarTime
		private Runnable updateSeekBarTime = new Runnable() {
			public void run() {
				//get current position
				timeElapsed = mediaPlayer.getCurrentPosition();
				//set seekbar progress
				seekbar.setProgress((int) timeElapsed);
				//set time remaing
				double timeRemaining = finalTime - timeElapsed;
				//duration.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining), TimeUnit.MILLISECONDS.toSeconds((long) timeRemaining) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining))));
				
				timer.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes((long) timeElapsed), TimeUnit.MILLISECONDS.toSeconds((long) timeElapsed) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) timeElapsed))));
				duration.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes((long) finalTime), TimeUnit.MILLISECONDS.toSeconds((long) finalTime) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) finalTime))));
				
				//repeat yourself that again in 100 miliseconds
				durationHandler.postDelayed(this, 100);
			}
		};

		// pause mp3 song
		public void pause(View view) {
			mediaPlayer.pause();
		}

		// go forward at forwardTime seconds
		public void forward(View view) {
			//check if we can go forward at forwardTime seconds before song endes
			if ((timeElapsed + forwardTime) <= finalTime) {
				timeElapsed = timeElapsed + forwardTime;

				//seek to the exact second of the track
				mediaPlayer.seekTo((int) timeElapsed);
			}
		}

		// go backwards at backwardTime seconds
		public void rewind(View view) {
			//check if we can go back at backwardTime seconds after song starts
			if ((timeElapsed - backwardTime) > 0) {
				timeElapsed = timeElapsed - backwardTime;
				
				//seek to the exact second of the track
				mediaPlayer.seekTo((int) timeElapsed);
			}
		}
}
