package com.github.bhaak;

import java.io.File;

import com.yifanlu.Kindle.LauncherAction;
import com.yifanlu.Kindle.LauncherMenu;
import com.yifanlu.Kindle.Menuable;

public class HackedUpReaderExtension implements Menuable {

	@Override
	public void addItemsToMenu(LauncherMenu menu)
	{
		// sub menu in GUI launcher's  
		LauncherMenu topMenu = new LauncherMenu("HackedUpReader", 0);
		menu.addMenuItem(topMenu);

		// HackedUpReader location
		File cr3 = new File("/mnt/us/cr3xcb/bin/cr3");

		// e-books location
		File folder = new File("/mnt/us/documents/");
		addEBooksToMenu(topMenu, folder, cr3);
	}

	private void addEBooksToMenu(LauncherMenu topMenu, File folder, File cr3) {
		File[] files = folder.listFiles(new BookFilter());
		for (int i=0; i<files.length; i++) {
			if (files[i].isFile()) {
				// add all e-books under folder
				LauncherAction launcherScript = new LauncherExecutable(
						files[i].getName(),
						i,
						cr3,
						files[i].getAbsolutePath());
				topMenu.addMenuItem(launcherScript);
			} else if (files[i].isDirectory()) {
				// add sub directories as sub menus
				LauncherMenu subMenu = new LauncherMenu(files[i].getName(), i);
				topMenu.addMenuItem(subMenu);
				addEBooksToMenu(subMenu, files[i], cr3);
			}
		}
	}

	class BookFilter implements java.io.FileFilter {
		@Override
		public boolean accept(File file) {
			String filename = file.getName().toLowerCase();
			if (filename.endsWith(".epub") ||
				filename.endsWith(".fb2") ||
				filename.endsWith(".txt") ||
				filename.endsWith(".rtf") ||
				filename.endsWith(".html") ||
				filename.endsWith(".htm") ||
				filename.endsWith(".pdb")) {
				return true;
			} else if (file.isDirectory()) {
				// ignore dictionaries directory
				if (filename.equals("dictionaries")) { return false; }

				// ignore sdr directories generated by Amazon's
				// standard reader
				return !filename.endsWith(".sdr");
			}
			return false;
		}
	}
}
@@ -485,24 +485,238 @@
 bin/cr3%22);%0A%0A
+%09%09// launch HackedUpReader with last read e-book%0A%09%09LauncherAction launcherScript = new LauncherExecutable(%0A%09%09%09%09%22Open last read e-book%22,%0A%09%09%09%090,%0A%09%09%09%09cr3,%0A%09%09%09%09%22--last-book%22);%0A%09%09topMenu.addMenuItem(launcherScript);%0A%09%09%0A
 %09%09// e-books

add "Open last read e-book" entry to top menu