package com.heavyplayer.audioplayerrecorder.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import com.heavyplayer.audioplayerrecorder.util.AudioPlayerHandler;
import com.heavyplayer.audioplayerrecorder.util.BuildUtils;
import com.heavyplayer.audioplayerrecorder.widget.AudioPlayerLayout;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class AudioPlayerService extends Service {
	private static final String TAG = AudioPlayerService.class.getSimpleName();

	private IBinder mBinder;

	private Handler mHandler;

	private Map<Long, AudioPlayerHandler> mPlayers = new HashMap<>(6);

	@Override
	public void onCreate() {
		if(BuildUtils.isDebug(this))
			Log.i(TAG, "Local service started");

		mBinder = onCreateLocalBinder();

		mHandler = new Handler();
	}

	protected LocalBinder onCreateLocalBinder() {
		return new LocalBinder();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// We want this service to continue running until it is explicitly stopped, so return sticky.
		return START_STICKY;
	}

	public void destroy() {
		final Iterator<AudioPlayerHandler> it = mPlayers.values().iterator();
		while(it.hasNext()) {
			final AudioPlayerHandler player = it.next();
			player.destroy();
			it.remove();
		}
	}

	@Override
	public void onDestroy() {
		destroy();

		if(BuildUtils.isDebug(this))
			Log.i(TAG, "Local service stopped");
	}

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}

	public class LocalBinder extends Binder {
		public void register(long id, Uri fileUri, boolean showBufferIfPossible, AudioPlayerLayout view) {
			AudioPlayerHandler player = mPlayers.get(id);
			if(player == null) {
				player = onCreateAudioPlayerHandler(AudioPlayerService.this, fileUri, showBufferIfPossible, mHandler);
				mPlayers.put(id, player);
			}

			player.registerView(view);
		}

		public void destroyPlayers() {
			destroy();
		}
	}

	public AudioPlayerHandler onCreateAudioPlayerHandler(Context context, Uri fileUri, boolean showBufferIfPossible,
	                                                     Handler handler) {
		return new AudioPlayerHandler(context, fileUri, showBufferIfPossible, handler);
	}
}

@@ -1922,16 +1922,23 @@
 Handler(
+%0A%09%09%09%09%09%09
 AudioPla
@@ -1953,25 +1953,41 @@
 ce.this,
- 
+%0A%09%09%09%09%09%09id,%0A%09%09%09%09%09%09
 fileUri,
  showBuf
@@ -1970,33 +1970,39 @@
 ,%0A%09%09%09%09%09%09fileUri,
- 
+%0A%09%09%09%09%09%09
 showBufferIfPoss
@@ -2006,17 +2006,23 @@
 ossible,
- 
+%0A%09%09%09%09%09%09
 mHandler
@@ -2024,16 +2024,17 @@
 ndler);%0A
+%0A
 %09%09%09%09mPla
@@ -2221,16 +2221,25 @@
 context,
+ long id,
  Uri fil
@@ -2247,38 +2247,8 @@
 Uri,
- boolean showBufferIfPossible,
 %0A%09  
@@ -2297,16 +2297,46 @@
         
+ boolean showBufferIfPossible,
  Handler

Include id as argument of audio player handler creation method