package com.github.bhaak;

import java.io.File;
import java.text.Collator;
import java.util.Arrays;
import java.util.Comparator;

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/hackedupreader/bin/cr3");
		if (!cr3.exists()) {
			// path of old version
			cr3 = new File("/mnt/us/cr3xcb/bin/cr3");
		}

		// launch HackedUpReader with last read e-book
		LauncherAction launcherScript = new LauncherExecutable(
				"Open last book",
				-1,
				cr3,
				"--last-book");
		topMenu.addMenuItem(launcherScript);
		
		// e-books location
		File folder = new File("/mnt/us/documents/");
		addEBooksToMenu(topMenu, folder, cr3);
	}

	private FileComparator fileComparator = new FileComparator();
	private BookFilter bookfilter = new BookFilter();
	
	private int addEBooksToMenu(LauncherMenu topMenu, File folder, File cr3) {
		File[] files = folder.listFiles(bookfilter);
		if (files == null) { return 0; }
		// sort files
		Arrays.sort(files, fileComparator);
		
		int bookCount = 0;
		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);
				bookCount++;
			} else if (files[i].isDirectory()) {
				// add sub directories as sub menus
				LauncherMenu subMenu = new LauncherMenu(files[i].getName(), i);
				int dirBookCount = addEBooksToMenu(subMenu, files[i], cr3);
				if (dirBookCount > 0) {
					topMenu.addMenuItem(subMenu);
					bookCount += dirBookCount;
				}
			}
		}
		return bookCount;
	}

	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(".fb2.zip") ||
				(filename.endsWith(".txt") && !filename.endsWith(".bmk.txt")) || // skip bookmarks generated files
				filename.endsWith(".rtf") ||
				filename.endsWith(".html") ||
				filename.endsWith(".htm") ||
				filename.endsWith(".pdb") ||
				filename.endsWith(".mobi") ||
				filename.endsWith(".chm")) {
				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;
		}
	}

	class FileComparator implements Comparator<File> {
		Collator collator;

		public FileComparator() {
			collator = Collator.getInstance();
			// compare case insensitive
			collator.setStrength(Collator.SECONDARY);
		}
		public int compare(File file1, File file2) {
			return collator.compare(file1.getName(), file2.getName());
		}
	}
}
@@ -123,16 +123,74 @@
 rator;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 import c
@@ -223,16 +223,16 @@
 Action;%0A
-
 import c
@@ -361,16 +361,108 @@
 ble %7B%0A%09%0A
+    private static Logger logger = LoggerFactory.getLogger(HackedUpReaderExtension.class);%0A%0A
 %09@Overri
@@ -1150,16 +1150,72 @@
 3);%0A%09%7D%0A%0A
+%09private static Runtime runtime = Runtime.getRuntime();%0A
 %09private
@@ -1558,16 +1558,16 @@
 nt = 0;%0A
-
 %09%09for (i
@@ -1597,16 +1597,107 @@
  i++) %7B%0A
+%09%09%09logger.info(%22%7B%7D %7B%7D%22, Long.toString(runtime.totalMemory()), files%5Bi%5D.getAbsolutePath());%0A
 %09%09%09if (f

add some debug logging