package fr.mantal.wcdl;

import com.google.gson.Gson;
import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Paths;

public class Downloader
{
	private String baseUrl;
	private String startPath;
	private String localPath;

	private Property image;
	private Property next;
	private Property title;

	private String imageFormat = null;

	static Downloader FromConfig(String workingDirectory)//todo better param name
	{
		String file = null;
		try {
			file = FileUtils.readFileToString(new File(Paths.get(workingDirectory, ".wcdl.config").toString()), "utf-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
		Downloader res = new Gson().fromJson(file, Downloader.class);
		res.localPath = workingDirectory + File.separator;
		return res;
	}

	Downloader(String imageSelector, String nextLinkSelector, String baseUrl, String startPath, String localPath)
	{
		this.image = new Property(imageSelector, "src");
		this.next = new Property(nextLinkSelector, "href");
		this.title = new Property();
		this.baseUrl = baseUrl;
		this.localPath = localPath + File.separator;
		this.startPath = startPath;
	}

	Downloader Title(Property title)
	{
		this.title = title;
		return this;
	}

	Downloader ImageFormat(String format)
	{
		this.imageFormat = "." + format;
		return this;
	}

	void download()
	{
		URL url;

		try
		{
			url = new URL(baseUrl + startPath);
		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
			return;
		}

		Document document = null;
		int i = 1;

		do
		{
			try
			{
				document = Jsoup.connect(url.toString()).get();
			}
			catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (imageFormat == null)
				imageFormat = getFileFormat(document);

			File file = Paths.get(localPath + i + " - " + escapePath(title.get(document)) + imageFormat).toFile();

			i++;
			if (fileExist(file))
				continue;

			copyImageToFile(document, file);

		} while((url = getNextUrl(document)) != null);
	}

	private String getFileFormat(Document document) {
		String imgUrl = image.get(document);

		return imgUrl.substring(imgUrl.lastIndexOf('.'), imgUrl.length());
	}

	private String escapePath(String path)
	{
		return path.replace('/', '-').replace('\\', '-').replace('?', '-');//todo regex
	}

	private boolean copyImageToFile(Document document, File file)
	{
		String url = image.get(document);

		url = fixUrl(url);
		try
		{
			FileUtils.copyURLToFile(new URL(url), file);
		}
		catch (IOException e)
		{
			e.printStackTrace();
			return false;
		}
		return true;
	}

	private URL getNextUrl(Document document)
	{
		String url = next.get(document);

		if (url.isEmpty())
			return null;

		url = fixUrl(url);
		try
		{
			return new URL(url);
		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		}
		return null;
	}

	private String fixUrl(String url)
	{
		if (!url.startsWith("http"))
		{
			if (url.charAt(0) == '/')
				url = url.substring(1);
			url = baseUrl + url;
		}
		return url;
	}

	private static boolean fileExist(File file)
	{
		return file.isFile() && file.exists();//todo check
	}
}
@@ -520,17 +520,17 @@
 nloader 
-F
+f
 romConfi
@@ -538,32 +538,20 @@
 (String 
-workingDirectory
+path
 )//todo 
@@ -651,124 +651,165 @@
 ile(
-Paths.get(workingDirectory, %22.wcdl.config%22).toString()), %22utf-8%22);%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09e.printStackTrace(
+path), %22utf-8%22);%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09System.out.println(%22Can not open config file: %22 + e.getMessage());%0A%09%09%09System.exit(ExitError.CONFIG_OPEN_ERROR
 );%0A%09
@@ -897,24 +897,46 @@
 h = 
-workingDirectory
+Paths.get(path).getParent().toString()
  + F
@@ -964,24 +964,37 @@
 rn res;%0A%09%7D%0A%0A
+%09@Deprecated%0A
 %09Downloader(
@@ -1335,24 +1335,31 @@
 tPath;%0A%09%7D%0A%0A%09
+public 
 Downloader T
@@ -1424,16 +1424,23 @@
 s;%0A%09%7D%0A%0A%09
+public 
 Download

Exit with an error if the config file can not be open