package com.parnswir.unmp;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.parnswir.unmp.core.DatabaseUtils;
import com.parnswir.unmp.playlist.Playlist;
import com.parnswir.unmp.playlist.parser.WPLParser;
import com.parnswir.unmp.playlist.parser.WPLParser.WPLParserException;

public class PlaylistsFragment extends AbstractFragment {

	private ListView mPlaylists;
	private ArrayList<String> playlists = new ArrayList<String>();
	private ArrayList<String> playlistNames = new ArrayList<String>();
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    	rootView = super.onCreateView(inflater, container, savedInstanceState);
    	
    	inflate(R.layout.playlists_fragment);
    	showActionBar();
    	showTitle("Playlists");
    	
    	initPlaylistList();
    	
        return rootView; 
    }
	
	
	public void initPlaylistList() {
		playlists = DatabaseUtils.getAllPlaylists(DB);
		initLists();
		mPlaylists = (ListView) rootView.findViewById(R.id.playlists);
		mPlaylists.setAdapter(new ArrayAdapter<String>(activity, R.layout.drawer_list_item, playlistNames));
		mPlaylists.setOnItemClickListener(new PlaylistClickListener());
		mPlaylists.setOnItemLongClickListener(new PlaylistClickListener());
	}
	
	
	private void initLists() {
		for (String playlistPath : playlists) { 
			WPLParser parser = new WPLParser(new File(playlistPath), DB);
			playlistNames.add(parser.getName());
		}
		playlistNames.add(getString(R.string.addPlaylist));
	}
	
	
	private class PlaylistClickListener implements ListView.OnItemClickListener, ListView.OnItemLongClickListener {
	    @Override
	    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
	        handleAddPlaylistClick(view);
	        if (! clickedAddPlaylist(view)) {
				playPlaylist(position);
			}
	    	return;
	    }

		@Override
		public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
			handleAddPlaylistClick(view);
			if (! clickedAddPlaylist(view)) {
				deletePlaylist(position);
			}
			return true;
		}
		
		private void handleAddPlaylistClick(View view) {
			if (clickedAddPlaylist(view)) {
				addPlaylist();
			}
		}
		
		private boolean clickedAddPlaylist(View view) {
			TextView item = (TextView) view;
	        String selectedText = item.getText().toString();
	        String addPlaylist = getString(R.string.addPlaylist);
	        
	        return selectedText.equals(addPlaylist);
		}
	}
	
	
	private void deletePlaylist(int position) {
		
	}
	
	
	private void playPlaylist(int position) {
		String path = playlists.get(position);
		WPLParser parser = new WPLParser(new File(path), DB);
		Playlist playlist = new Playlist();
		try {
			playlist = parser.buildPlaylist();
			playPlaylist(playlist);
		} catch (IOException e) {
		} catch (WPLParserException e) {
		}
	}
	
	
	private void addPlaylist() {
		
	}    
	
}

@@ -99,16 +99,80 @@
 yList;%0A%0A
+import android.app.ProgressDialog;%0Aimport android.os.AsyncTask;%0A
 import a
@@ -2953,47 +2953,490 @@
 %7B%0A%09%09
-String path = playlists.get(position);%0A
+PlaylistLoader loader = new PlaylistLoader();%0A%09%09loader.execute(playlists.get(position));%0A%09%7D%0A%09%0A%09%0A%09private void addPlaylist() %7B%0A%09%09%0A%09%7D    %0A%09%0A%09%0A%09private class PlaylistLoader extends AsyncTask%3CString, Void, Playlist%3E %7B%0A%0A%09%09ProgressDialog dialog = new ProgressDialog(activity);%0A%0A%09%09@Override%0A%09%09protected void onPreExecute() %7B%0A%09%09%09dialog.setMessage(%22Loading Playlist...%22);%0A%09        dialog.show();%0A%09        super.onPreExecute();%0A%09    %7D%0A%09%0A%09%09protected Playlist doInBackground(String... args) %7B%0A%09
 %09%09WP
@@ -3479,20 +3479,23 @@
 ile(
-path
+args%5B0%5D
 ), DB);%0A
 %09%09Pl
@@ -3490,16 +3490,17 @@
 ), DB);%0A
+%09
 %09%09Playli
@@ -3535,14 +3535,16 @@
 ;%0A%09%09
+%09
 try %7B%0A
+%09
 %09%09%09p
@@ -3582,34 +3582,8 @@
 );%0A%09
-%09%09playPlaylist(playlist);%0A
 %09%09%7D 
@@ -3606,16 +3606,17 @@
 on e) %7B%0A
+%09
 %09%09%7D catc
@@ -3648,56 +3648,190 @@
 %7B%0A%09%09
+%09
 %7D%0A%09
-%7D%0A%09
+%09%09return playlist;%0A%09%09%7D
 %0A%09%0A
+%09
 %09pr
-ivate void addPlaylist() %7B%0A%09%09%0A%09%7D    
+otected void onPostExecute(Playlist result) %7B%0A%09%09%09playPlaylist(result);%0A%09%09%09if(dialog != null && dialog.isShowing())%7B%0A%09%09%09%09dialog.dismiss();%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D
 %0A%09%0A%7D

Add progress dialog when loading a playlist