package com.peetz.kontor.data;

import java.util.Collection;
import java.util.Iterator;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.peetz.comics.entity.ArtistEntity;
import com.peetz.comics.entity.ComicEntity;
import com.peetz.comics.entity.IssueEntity;
import com.peetz.comics.entity.PublisherEntity;
import com.peetz.comics.entity.StoryArcEntity;
import com.peetz.comics.service.ComicService;

public class ExportComics
{
	public Element backupComics(Document document)
	{
		Element comics = document.createElement("comics");
		ComicService comicService = getComicService(); 
		if (comicService == null) return comics;
		
		Collection<PublisherEntity> publishers = comicService.getAllPublisher();
		Iterator<PublisherEntity> publisher_iterator = publishers.iterator();
		while (publisher_iterator.hasNext()) {
			PublisherEntity publisher = publisher_iterator.next();
			Element publisherNode = document.createElement("publisher");
			publisherNode.setAttribute("id", publisher.getId().toString());
			publisherNode.setAttribute("name", publisher.getName());
			comics.appendChild(publisherNode);
			comics.appendChild(document.createTextNode("\n"));
		}
		Collection<ArtistEntity> artists = comicService.getAllArtists();
		Iterator<ArtistEntity> artist_iterator = artists.iterator();
		while(artist_iterator.hasNext()) {
			ArtistEntity artist = artist_iterator.next();
			Element artistNode = document.createElement("artist");
			artistNode.setAttribute("id", artist.getId().toString());
			artistNode.setAttribute("name", artist.getName());
			comics.appendChild(artistNode);
			comics.appendChild(document.createTextNode("\n"));
		}
		Collection<ComicEntity> comicList = comicService.getAllComics();
		Iterator<ComicEntity> comics_iterator = comicList.iterator();
		while(comics_iterator.hasNext()) {
			ComicEntity comic = comics_iterator.next();
			Element comicNode = document.createElement("comic");
			comicNode.setAttribute("id", comic.getId().toString());
			comicNode.setAttribute("title", comic.getTitle());
			String completed = "false";
			if (comic.getCompleted() != null) completed = comic.getCompleted().toString();
			comicNode.setAttribute("complete", completed);
			String currentOrder = "false";
			if (comic.getCurrentOrder() != null) currentOrder = comic.getCurrentOrder().toString();
			comicNode.setAttribute("order", currentOrder);
			Collection<IssueEntity> issues = comicService.getAllIssuesForComic(comic);
			Iterator<IssueEntity> issues_iterator = issues.iterator();
			comicNode.appendChild(document.createTextNode("\n"));
			while(issues_iterator.hasNext()) {
				IssueEntity issue = issues_iterator.next();
				Element issueNode = document.createElement("issue");
				issueNode.setAttribute("id", issue.getId().toString());
				issueNode.setAttribute("number", issue.getNumber());
				comicNode.appendChild(issueNode);
				comicNode.appendChild(document.createTextNode("\n"));
			}
			comics.appendChild(comicNode);
			comics.appendChild(document.createTextNode("\n"));
		}
		Collection<StoryArcEntity> storyArcs = comicService.getAllStoryArcs();
		Iterator<StoryArcEntity> iterator_storyArcsIterator = storyArcs.iterator();
		while(iterator_storyArcsIterator.hasNext()) {
			StoryArcEntity storyArc = iterator_storyArcsIterator.next();
			Element storyNode = document.createElement("storyArc");
			storyNode.setAttribute("id", storyArc.getId().toString());
			storyNode.setAttribute("title", storyArc.getTitle());
			comics.appendChild(storyNode);
			comics.appendChild(document.createTextNode("\n"));
		}
		return comics;
	}

	private ComicService getComicService()
	{
		return null;
	}
}
