package com.mantono.blink.importing;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Set;

import com.mantono.blink.Bookmark;

public class DeliciousImporter
{
	private final File data;
	
	public DeliciousImporter(final File data)
	{
		this.data = data;
	}
	
	public static void main(String[] args) throws IOException, NoSuchAlgorithmException
	{
		DeliciousImporter di = new DeliciousImporter(new File("/home/anton/.blink/delicious.html"));
		System.out.println(di.importData());
	}
	
	public int importData() throws IOException, NoSuchAlgorithmException
	{
		int imported = 0;
		List<String> lines = Files.readAllLines(data.toPath(), Charset.defaultCharset());
		for(String line : lines)
		{
			if(!line.contains("A HREF="))
				continue;
			line = line.replaceAll("(<DT><)|(>[^>]*</A>)", "");
//			line = line.replaceAll("\"", "");
//			final String[] fields = line.split("(=)|(\\s)");
//			assert fields.length >= 8: fields.length + " --> " + line;
			final Bookmark bookmark = createBookmark(line);
			if(saveBookmark(bookmark))
			{
				System.out.println(line);
				imported++;
			}
		}
		return imported;
	}

	private Bookmark createBookmark(String line) throws MalformedURLException
	{
		LineParser parser = new LineParser(line);
		final URL url = parser.getUrl();
		final long timestamp = parser.getTimestamp();
		final Set<String> labels = parser.getLabels();
		return new Bookmark(url, timestamp, labels);
	}
	//		final List<String> labels = Arrays.asList(labelField.split(","));

	private boolean saveBookmark(Bookmark bookmark) throws NoSuchAlgorithmException, FileNotFoundException, IOException
	{
		final CharSequence hash = bookmark.getHash();
		final String domain = bookmark.getDomain();
		final File folder = new File(System.getProperty("user.home") + "/.blink/bookmarks/" + domain);
		if(!folder.exists())
			Files.createDirectories(folder.toPath());
		final File file = new File(folder.getAbsolutePath() + "/" + hash);
		if(file.exists())
			return false;
		else
			file.createNewFile();
		try(FileOutputStream fileStream = new FileOutputStream(file);
			PrintStream printStream = new PrintStream(fileStream);)
		{
			printStream.print(bookmark.getUrl().toString()+"\n");
			printStream.print(bookmark.getTimestamp()+"\n");
			for(String label : bookmark.getLabels())
				printStream.print(label+"\n");
			printStream.flush();
		}
		return true;
	}
}

@@ -488,18 +488,17 @@
 e data;%0A
-%09
 %0A
+
 %09public 
@@ -554,26 +554,25 @@
  = data;%0A%09%7D%0A
-%09
 %0A
+
 %09public stat
@@ -788,10 +788,9 @@
 %0A%09%7D%0A
-%09
 %0A
+
 %09pub
@@ -1099,165 +1099,8 @@
 %22);%0A
-//%09%09%09line = line.replaceAll(%22%5C%22%22, %22%22);%0A//%09%09%09final String%5B%5D fields = line.split(%22(=)%7C(%5C%5Cs)%22);%0A//%09%09%09assert fields.length %3E= 8: fields.length + %22 --%3E %22 + line;%0A
 %09%09%09f
@@ -1564,81 +1564,10 @@
 s);%0A
+
 %09%7D
-%0A%09//%09%09final List%3CString%3E labels = Arrays.asList(labelField.split(%22,%22));
 %0A%0A%09p

Remove redundant lines.