package com.neeplayer;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import java.util.ArrayList;


public class NowPlayingActivity extends Activity {
    ArrayList<Album> albumList;
    String artistName;

    private MusicService musicService;
    private Intent playIntent;

    private Boolean musicBound = false;

    private ServiceConnection musicConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            MusicService.MusicBinder binder = (MusicService.MusicBinder) service;
            musicService = binder.getService();
            musicService.setList(albumList);
            musicService.setArtistName(artistName);
            musicBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            musicBound = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_now_playing);
        onNewIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        int albumPosition = intent.getIntExtra("ALBUM_POSITION", 0);
        int songPosition = intent.getIntExtra("SONG_POSITION", 0);
        albumList = (ArrayList<Album>) intent.getSerializableExtra("ALBUM_LIST");
        TextView view = (TextView)findViewById(R.id.intent_params);
        view.setText(String.format("%d %d", albumPosition, songPosition));
    }

    //    @Override
//    protected void onStart() {
//        super.onStart();
//        if (playIntent == null) {
//            playIntent = new Intent(this, MusicService.class);
//            bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
//            startService(playIntent);
//        }
//    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_now_playing, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        moveTaskToBack(true);
    }
}

@@ -464,24 +464,70 @@
 rtistName;%0A%0A
+    int albumPosition;%0A    int songPosition;%0A%0A
     private 
@@ -926,16 +926,17 @@
 vice();%0A
+%0A
         
@@ -1004,32 +1004,85 @@
 .set
-ArtistName(artistName
+Position(albumPosition, songPosition);%0A            musicService.playSong(
 );%0A
+%0A
     
@@ -1421,16 +1421,17 @@
 aying);%0A
+%0A
         
@@ -1560,27 +1560,24 @@
 ntent);%0A
- 
+%0A
        
-int
  albumPo
@@ -1630,28 +1630,24 @@
 0);%0A        
-int 
 songPosition
@@ -1771,16 +1771,313 @@
 LIST%22);%0A
+%0A        if (playIntent != null) %7B%0A            unbindService(musicConnection);%0A            stopService(playIntent);%0A        %7D%0A%0A        playIntent = new Intent(this, MusicService.class);%0A        bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);%0A        startService(playIntent);%0A%0A
         
@@ -2218,22 +2218,16 @@
 %0A    %7D%0A%0A
-    //
     @Ove
@@ -2232,18 +2232,16 @@
 verride%0A
-//
     prot
@@ -2257,91 +2257,28 @@
 d on
-Start() %7B%0A//        super.onStart();%0A//        if (playIntent == null
+Destroy(
 ) %7B%0A
-//
         
     
@@ -2277,164 +2277,52 @@
     
-    playIntent = new Intent(this, MusicService.class);%0A//            bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);%0A//            start
+unbindService(musicConnection);%0A        stop
 Serv
@@ -2342,18 +2342,16 @@
 t);%0A
-//
         
 %7D%0A//
@@ -2350,20 +2350,34 @@
     
-%7D%0A//
+super.onDestroy();%0A
     %7D%0A%0A
-%0A
     

Implement playing music from now playing activity