package stsc.news.feedzilla.export.csv;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import stsc.news.feedzilla.FeedzillaFileStorage;
import stsc.news.feedzilla.FeedzillaFileStorageReceiver;
import stsc.news.feedzilla.file.schema.FeedzillaFileArticle;
import stsc.news.feedzilla.file.schema.FeedzillaFileCategory;
import stsc.news.feedzilla.file.schema.FeedzillaFileSubcategory;

final class FeedzillaToCsvExporter implements FeedzillaFileStorageReceiver {

	private final OutputStreamWriter out;

	FeedzillaToCsvExporter(FeedzillaToCsvSettings settings) throws FileNotFoundException, IOException {
		preValidate(settings);

		try (OutputStreamWriter os = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(settings.getOutputFileName())))) {
			this.out = os;
			final FeedzillaFileStorage fileStorage = new FeedzillaFileStorage(settings.getFeedDataFolder(), settings.getDateBackDownloadFrom(), false);
			fileStorage.addReceiver(this);
			fileStorage.readData();
		}
	}

	private void preValidate(FeedzillaToCsvSettings settings) {
		if (new File(settings.getOutputFileName()).exists()) {
			throw new IllegalArgumentException("output file " + settings.getOutputFileName() + " exists, please choose another one");
		}
		final File feedDataFolder = new File(settings.getFeedDataFolder());
		if (!feedDataFolder.exists() || !feedDataFolder.isDirectory()) {
			throw new IllegalArgumentException("feedzilla data path " + settings.getFeedDataFolder() + " not exists or is not a directory");
		}
	}

	public final static void main(final String args[]) {
		try {
			if (args.length > 0 && args[0].equals("--help")) {
				FeedzillaToCsvSettings.printUsage();
				return;
			}
			new FeedzillaToCsvExporter(new FeedzillaToCsvSettings(args));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean addArticle(FeedzillaFileArticle article) throws IOException {
		outputCategory(article.getCategory());
		outputSubcategory(article.getSubcategory());
		outputArticle(article);
		return false;
	}

	private void outputCategory(FeedzillaFileCategory category) throws IOException {
		out.append(category.getEnglishCategoryName());
	}

	private void outputSubcategory(FeedzillaFileSubcategory subcategory) throws IOException {
		out.append("\t").append(subcategory.getEnglishSubcategoryName());
	}

	private void outputArticle(FeedzillaFileArticle article) throws IOException {
		out.append("\t").append(article.getSourceUrl()).append("\t").append(article.getPublishDate().toString()).append("\t").append(article.getTitle())
				.append("\n");
	}

}

@@ -2271,16 +2271,18 @@
 .append(
+s(
 category
@@ -2299,32 +2299,33 @@
 hCategoryName())
+)
 ;%0A%09%7D%0A%0A%09private v
@@ -2431,16 +2431,18 @@
 .append(
+s(
 subcateg
@@ -2473,16 +2473,17 @@
 yName())
+)
 ;%0A%09%7D%0A%0A%09p
@@ -2577,32 +2577,34 @@
 nd(%22%5Ct%22).append(
+s(
 article.getSourc
@@ -2610,16 +2610,17 @@
 ceUrl())
+)
 .append(
@@ -2624,32 +2624,34 @@
 nd(%22%5Ct%22).append(
+s(
 article.getPubli
@@ -2670,16 +2670,17 @@
 tring())
+)
 .append(
@@ -2684,24 +2684,29 @@
 nd(%22%5Ct%22)
+%0A%09%09%09%09
 .append(
 article.
@@ -2697,16 +2697,18 @@
 .append(
+s(
 article.
@@ -2722,24 +2722,123 @@
 e())
-%0A%09%09%09%09.append(%22%5Cn
+).append(%22%5Cn%22);%0A%09%7D%0A%0A%09private String s(String param) %7B%0A%09%09return param.replaceAll(%22%5Cn%22, %22%3C/br%3E%22).replaceAll(%22%5Ct%22, %22  
 %22);%0A

substitute \t to two space symbol and \n to </br> for all string data into articles