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.library.entity.ArticleEntity;
import com.peetz.library.entity.BookEntity;
import com.peetz.library.entity.BookshelfEntity;
import com.peetz.library.entity.ShelfboardEntity;
import com.peetz.library.service.LibraryService;

public class ExportLibrary
{
	public Element backupLibrary(Document document)
	{
		Element library = document.createElement("library");
		library.appendChild(document.createTextNode("\n"));
		LibraryService libraryService = getLibraryService();

		Collection<BookEntity> books = libraryService.getAllBooks();
		Iterator<BookEntity> iterator_books = books.iterator();
		while(iterator_books.hasNext()) {
			BookEntity book = iterator_books.next();
			Element bookNode = document.createElement("book");
			bookNode.setAttribute("id", book.getId().toString());
			bookNode.setAttribute("title", book.getTitle());
			bookNode.setAttribute("author", book.getAuthor());
			bookNode.setAttribute("edition", book.getEdition());
			bookNode.setAttribute("isbn", book.getIsbn());
			bookNode.setAttribute("pages", book.getPage().toString());
			bookNode.setAttribute("publisher", book.getPublisher());
			library.appendChild(bookNode);
			library.appendChild(document.createTextNode("\n"));
		}
		
		Collection<BookshelfEntity> bookshelfs = libraryService.getAllBookshelfs();
		Iterator<BookshelfEntity> iterator_bookshelfs = bookshelfs.iterator();
		while(iterator_bookshelfs.hasNext()) {
			BookshelfEntity bookshelf = iterator_bookshelfs.next();
			Element shelfNode = document.createElement("shelf");
			shelfNode.setAttribute("id", bookshelf.getId().toString());
			shelfNode.setAttribute("title", bookshelf.getTitle());
			
			shelfNode.appendChild(document.createTextNode("\n"));
			Collection<ShelfboardEntity> shelfboards = bookshelf.getShelfBoards();
			Iterator<ShelfboardEntity> iterator_shelfboards = shelfboards.iterator();
			while(iterator_shelfboards.hasNext()) {
				ShelfboardEntity shelfboard = iterator_shelfboards.next();
				Element boardNode = document.createElement("board");
				boardNode.setAttribute("id", shelfboard.getId().toString());
				boardNode.setAttribute("title", shelfboard.getTitle());
				shelfNode.appendChild(boardNode);
				shelfNode.appendChild(document.createTextNode("\n"));
			}
			library.appendChild(shelfNode);
			library.appendChild(document.createTextNode("\n"));
		}
		
		Collection<ArticleEntity> articles = libraryService.getAllArticles();
		Iterator<ArticleEntity> iterator_articles = articles.iterator();
		while(iterator_articles.hasNext()) {
			ArticleEntity article = iterator_articles.next();
			Element articleNode = document.createElement("article");
			articleNode.setAttribute("id", article.getId().toString());
			articleNode.setAttribute("title", article.getTitle());

			Collection<ArticleEntity> origins = article.getOriginArticles();
			Iterator<ArticleEntity> iterator_origins = origins.iterator();
			while(iterator_origins.hasNext()) {
				ArticleEntity origin = iterator_origins.next();
				Element originNode = document.createElement("article");
				originNode.setAttribute("id", origin.getId().toString());
				articleNode.appendChild(originNode);
				articleNode.appendChild(document.createTextNode("\n"));
			}
			
			Collection<ArticleEntity> relateds = article.getRelatedArticles();
			Iterator<ArticleEntity> iterator_relateds = relateds.iterator();
			articleNode.appendChild(document.createTextNode("\n"));
			while(iterator_relateds.hasNext()) {
				ArticleEntity related = iterator_relateds.next();
				Element relatedNode = document.createElement("article");
				relatedNode.setAttribute("id", related.getId().toString());
				articleNode.appendChild(relatedNode);
				articleNode.appendChild(document.createTextNode("\n"));
			}
			library.appendChild(articleNode);
			library.appendChild(document.createTextNode("\n"));
		}
		return library;
	}
	
	private LibraryService getLibraryService()
	{
		return null;
	}
}
