/**
 * The MIT License (MIT)
 * Copyright (c) 2013 David Carver
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
 * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.github.kingargyle.plexappclient.core.services;

import java.util.ArrayList;
import java.util.List;

import com.bugsense.trace.BugSenseHandler;
import com.github.kingargyle.plexapp.model.impl.Directory;
import com.github.kingargyle.plexapp.model.impl.MediaContainer;
import com.github.kingargyle.plexappclient.core.model.impl.MenuItem;


import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;

/**
 * A IntentService that fetches the available Libraries used
 * for menu options from the Plex Media Server.
 * 
 * @author dcarver
 *
 */
public class MainMenuIntentService extends AbstractPlexRESTIntentService {
	
	private ArrayList<MenuItem> menuItems;

	/**
	 * @param name
	 */
	public MainMenuIntentService() {
		super("MainMenuIntentService");
		menuItems = new ArrayList<MenuItem>();
	}
		

	@Override
	protected void onHandleIntent(Intent intent) {
		fetchMenuItems();
		createSettingsMenu();
		sendMessageResults(intent);
	}
		
	
	protected void fetchMenuItems() {
		Log.i("MainMenuInterntService", "Retrieving Menu Items");
		
		if (factory != null) {
			loadMenuItems();
		}
	}
	
	protected void loadMenuItems() {
		// Fetch TV Shows and Movies
		try {
			MediaContainer mc = factory.retrieveSections();
			List<Directory> dirs = mc.getDirectories();
			
			for (Directory item : dirs) {
				MenuItem m = new MenuItem();
				m.setTitle(item.getTitle());
				m.setType(item.getType());
				m.setSection(item.getKey());
				menuItems.add(m);
			}
		} catch (Exception e) {
		  Log.e("MainMenuIntentService", "Unable to comminicate to server at " + factory.baseURL(), e);
		  BugSenseHandler.sendExceptionMessage("MainMenuIntentService", "loadMenuItems from " + factory.baseURL(), e);
		} 
	}
	
	/**
	 * Create the settings MenuItem since there is no option
	 * to retrieve this from Plex itself.
	 *  
	 */
	protected void createSettingsMenu() {
		MenuItem settingsMenuItem = new MenuItem();
		settingsMenuItem.setTitle("Settings");
		settingsMenuItem.setType("settings");
		settingsMenuItem.setSection("0");
		menuItems.add(settingsMenuItem);
	}


	@Override
	public void sendMessageResults(Intent intent) {
		Bundle extras = intent.getExtras();
		if (extras != null) {
			Messenger messenger = (Messenger) extras.get("MESSENGER");
			Message msg = Message.obtain();
			msg.obj = menuItems;
			try {
				messenger.send(msg);
			} catch (RemoteException ex) {
				Log.e(getClass().getName(), "Unable to send message", ex);
			  BugSenseHandler.sendExceptionMessage("MainMenuIntentService", "sendMessageResults", ex);
			}
		}
		
	}
	
}

@@ -2537,16 +2537,40 @@
 ();%0A%09%09%09%0A
+%09%09%09if (dirs != null) %7B%0A%09
 %09%09%09for (
@@ -2590,24 +2590,25 @@
 m : dirs) %7B%0A
+%09
 %09%09%09%09MenuItem
@@ -2628,24 +2628,25 @@
 Item();%0A%09%09%09%09
+%09
 m.setTitle(i
@@ -2662,24 +2662,25 @@
 tle());%0A%09%09%09%09
+%09
 m.setType(it
@@ -2690,24 +2690,25 @@
 getType());%0A
+%09
 %09%09%09%09m.setSec
@@ -2732,16 +2732,17 @@
 ));%0A%09%09%09%09
+%09
 menuItem
@@ -2751,16 +2751,180 @@
 add(m);%0A
+%09%09%09%09%7D%0A%09%09%09%7D else %7B%0A%09%09%09%09BugSenseHandler.sendEvent(%22loadMenuItems: No Directories for library/sections found. Server URL %22 + factory.baseURL() + %22/library/sections%22);%0A
 %09%09%09%7D%0A%09%09%7D

Fix NPE due to plex servers with out any libraries.