package com.androidhive.musicplayer;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.arkiwi.app.feed.FeedItem;
import org.arkiwi.app.feed.FeedItemFile;

public class SongsManager {
	@SuppressWarnings("unused")
	private final static String LOG_TAG = "SongsManager";
	private static SongsManager instance = null;
	// SDCard Path
	//final String MEDIA_PATH = new String("/sdcard/");
	private static ArrayList<HashMap<String, String>> songsList;
	
	private SongsManager(){
		songsList = new ArrayList<HashMap<String, String>>();
	}
	
	public static SongsManager getInstance() {
		if (instance == null) {
			instance = new SongsManager();
		} 
		
		return instance;
	}
	// Constructor
	public ArrayList<HashMap<String, String>> getSongsList(){
		return songsList;
	}
	
	public ArrayList<HashMap<String, String>> getPlayListFromWeb(HashMap<String, FeedItem> feeds){
		Iterator<String> iterator = feeds.keySet().iterator();
		while (iterator.hasNext()) {
			String key = (String) iterator.next();
			FeedItemFile feed = (FeedItemFile) feeds.get(key);
			HashMap<String, String> song = new HashMap<String, String>();
			
			song.put("songTitle", feed.getTitle());
			song.put("songPath", feed.getDownload());
			//Log.d(LOG_TAG,"getPlFromWeb() "+feed.getTitle()+" "+feed.getDownload());	
			// Adding each song to SongList
			songsList.add(song);
		}

		// return songs list array
		return songsList;
	}
	
	
	public ArrayList<HashMap<String, String>> getPlayListFromWeb(String uri){
		HashMap<String, String> song = new HashMap<String, String>();
		song.put("songTitle", "titolo della traccia");
		song.put("songPath", uri);
				
		// Adding each song to SongList
		songsList.add(song);

		// return songs list array
		return songsList;
	}
	
	/**
	 * Function to read all ogg files from sdcard
	 * and store the details in ArrayList
	 * */
	/*public ArrayList<HashMap<String, String>> getPlayListFromSd(){
		File home = new File(MEDIA_PATH);

		if (home.listFiles(new FileExtensionFilter()).length > 0) {
			for (File file : home.listFiles(new FileExtensionFilter())) {
				HashMap<String, String> song = new HashMap<String, String>();
				song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
				song.put("songPath", file.getPath());
				
				// Adding each song to SongList
				songsList.add(song);
			}
		}
		// return songs list array
		return songsList;
	}*/
	
	/**
	 * Class to filter files which are having .ogg extension
	 * */
	class FileExtensionFilter implements FilenameFilter {
		public boolean accept(File dir, String name) {
			return (name.endsWith(".oga") || name.endsWith(".ogg") || name.endsWith(".OGG"));
		}
	}
}
