package org.cru.godtools.api.v2.functions;

import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.cru.godtools.api.packages.utils.FileZipper;
import org.cru.godtools.api.translations.GodToolsTranslation;
import org.cru.godtools.domain.GuavaHashGenerator;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipOutputStream;

public class TranslationPackager
{
	FileZipper fileZipper = new FileZipper();

	Set<String> imagesAlreadyZipped = Sets.newHashSet();

	boolean textOnly = false;

	ByteArrayOutputStream bundledOutputStream = new ByteArrayOutputStream();
	ZipOutputStream zipOutputStream = new ZipOutputStream(bundledOutputStream);

	public InputStream compress(List<GodToolsTranslation> godToolsTranslationList)
	{
		try
		{
			if (!godToolsTranslationList.isEmpty())
			{
				fileZipper.zipContentsFile(createContentsFile(godToolsTranslationList), zipOutputStream);

				for (GodToolsTranslation godToolsTranslation : godToolsTranslationList)
				{
					addTranslationToZipStream(godToolsTranslation);
				}
			}

			zipOutputStream.close();
			bundledOutputStream.close();

			return new ByteArrayInputStream(bundledOutputStream.toByteArray());
		}
		catch(Exception e)
		{
			throw Throwables.propagate(e);
		}
	}

	public InputStream compress(GodToolsTranslation godToolsTranslation)
	{
		try
		{
			fileZipper.zipContentsFile(createContentsFile(Lists.newArrayList(godToolsTranslation)), zipOutputStream);

			addTranslationToZipStream(godToolsTranslation);

			zipOutputStream.close();
			bundledOutputStream.close();

			return new ByteArrayInputStream(bundledOutputStream.toByteArray());
		}
		catch (Exception e)
		{
			throw Throwables.propagate(e);
		}
	}

	public InputStream compressTextOnly(GodToolsTranslation godToolsTranslation)
	{
		// when creating a text only version, no picture resources will be included
		textOnly = true;
		InputStream stream = compress(godToolsTranslation);

		// change back to false so that in case the class is reused.
		textOnly = false;
		return stream;
	}

	private void addTranslationToZipStream(GodToolsTranslation godToolsTranslation) throws Exception
	{
		fileZipper.zipPackageFile(godToolsTranslation.getPackageStructure(),
				godToolsTranslation.getTranslation(),
				zipOutputStream);

		fileZipper.zipPageFiles(godToolsTranslation.getPageStructureList(), zipOutputStream);

		if (!textOnly)
		{
			fileZipper.zipImageFiles(godToolsTranslation.getImages(), zipOutputStream, imagesAlreadyZipped);
		}
	}

	private Document createContentsFile(List<GodToolsTranslation> godToolsTranslationList) throws ParserConfigurationException
	{
		Document contents = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();

		Element rootElement = contents.createElement("content");
		contents.appendChild(rootElement);

		for(GodToolsTranslation godToolsTranslation : godToolsTranslationList)
		{
			Element resourceElement = contents.createElement("resource");
			resourceElement.setAttribute("package", godToolsTranslation.getPackageCode());
			resourceElement.setAttribute("language", godToolsTranslation.getLanguage().getPath());
			resourceElement.setAttribute("config", godToolsTranslation.getTranslation().getId() + ".xml");
			resourceElement.setAttribute("status", godToolsTranslation.isDraft() ? "draft" : "live");
			resourceElement.setAttribute("name", godToolsTranslation.getPackageStructure().getPackageName());
			resourceElement.setAttribute("version", godToolsTranslation.getVersionNumber().toPlainString());

			if (!textOnly)
			{
				if (godToolsTranslation.getIcon() != null)
				{
					resourceElement.setAttribute("icon",
							GuavaHashGenerator.calculateHash(godToolsTranslation.getIcon().getImageContent()) + ".png");
				} else
				{
					resourceElement.setAttribute("icon", "missing");
				}
				rootElement.appendChild(resourceElement);
			}
		}
		return contents;
	}
}


@@ -818,31 +818,35 @@
 boolean 
-textOnly = fals
+includeImages = tru
 e;%0A%0A%09Byt
@@ -2193,22 +2193,28 @@
 d%0A%09%09
-textOnly = tru
+includeImages = fals
 e;%0A%09
@@ -2336,23 +2336,27 @@
 .%0A%09%09
-textOnly = fals
+includeImages = tru
 e;%0A%09
@@ -2703,33 +2703,37 @@
 am);%0A%0A%09%09if (
-!textOnly
+includeImages
 )%0A%09%09%7B%0A%09%09%09fil
@@ -3872,17 +3872,21 @@
 if (
-!textOnly
+includeImages
 )%0A%09%09

refactor boolean