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.tradingcards.entity.BaseSetEntity;
import com.peetz.tradingcards.entity.InsertEntity;
import com.peetz.tradingcards.entity.ManufacturerEntity;
import com.peetz.tradingcards.entity.ParallelSetEntity;
import com.peetz.tradingcards.entity.PositionEntity;
import com.peetz.tradingcards.entity.SportCardEntity;
import com.peetz.tradingcards.entity.SportEntity;
import com.peetz.tradingcards.entity.TeamEntity;
import com.peetz.tradingcards.service.SportService;
import com.peetz.tradingcards.service.TradingcardService;

public class ExportTradingCards
{
	public Element backupTradingCards(Document document)
	{
		Element tradingcards = document.createElement("tradingcards");
		tradingcards.appendChild(document.createTextNode("\n"));
		
		SportService sportService = getSportService();
		backupSports(sportService, document, tradingcards);

		backupCards(document, tradingcards);
		
		return tradingcards;
	}
	
	private void backupSports(SportService sportService, Document document, Element tradingcards)
	{
		Collection<SportEntity> sports = sportService.getAllSports();
		Iterator<SportEntity> iterator_sports = sports.iterator();
		while(iterator_sports.hasNext()) {
			SportEntity sport = iterator_sports.next();
			Element sportNode = document.createElement("sport");
			sportNode.setAttribute("id", sport.getId().toString());
			sportNode.setAttribute("name", sport.getName());
			
			Collection<TeamEntity> teams = sportService.getTeams(sport);
			backupTeams(teams, document, sportNode);

			Collection<PositionEntity> positions = sportService.getPositions(sport);
			backupPositions(positions, document, sportNode);
			
			tradingcards.appendChild(sportNode);
			tradingcards.appendChild(document.createTextNode("\n"));
		}
	}
	
	private void backupTeams(Collection<TeamEntity> teams, Document document, Element sportNode)
	{
		Iterator<TeamEntity> iterator_teams = teams.iterator();
		while(iterator_teams.hasNext()) {
			TeamEntity team = iterator_teams.next();
			Element teamNode = document.createElement("team");
			teamNode.setAttribute("id", team.getId().toString());
			teamNode.setAttribute("name", team.getName());
			//TODO what happens with null attributes?
			//teamNode.setAttribute("short", teamView.getShortname());
			sportNode.appendChild(teamNode);
		}
	}
	
	private void backupPositions(Collection<PositionEntity> positions, Document document, Element sportNode)
	{
		Iterator<PositionEntity> iterator_positions = positions.iterator();
		while(iterator_positions.hasNext()) {
			PositionEntity position = iterator_positions.next();
			Element positionNode = document.createElement("position");
			positionNode.setAttribute("id", position.getId().toString());
			positionNode.setAttribute("name", position.getName());
			positionNode.setAttribute("short", position.getShortName());
			sportNode.appendChild(positionNode);
		}
		sportNode.appendChild(document.createTextNode("\n"));
	}
	
	private void backupCards(Document document, Element tradingcards)
	{
		Collection<ManufacturerEntity> manufacturers = getTradingcardService().getAllManufacturers();
		Iterator<ManufacturerEntity> iterator_manufacturers = manufacturers.iterator();
		while(iterator_manufacturers.hasNext()) {
			ManufacturerEntity manufacturer = iterator_manufacturers.next();
			Element manufacturerNode = document.createElement("manufacturer");
			manufacturerNode.setAttribute("id", manufacturer.getId().toString());
			manufacturerNode.setAttribute("name", manufacturer.getName());
			Collection<BaseSetEntity> baseSets = getTradingcardService().getBaseSetsByManufacturer(manufacturer);
			backupBaseSets(baseSets, document, manufacturerNode);
			Collection<ParallelSetEntity> parallelSets = getTradingcardService().getParallelSetsByManufacturer(manufacturer);
			backupParallelSets(parallelSets, document, manufacturerNode);
			Collection<InsertEntity> inserts = getTradingcardService().getInsertsByManufacturer(manufacturer);
			backupInserts(inserts, document, manufacturerNode);
			manufacturerNode.appendChild(document.createTextNode("\n"));
			tradingcards.appendChild(manufacturerNode);
			tradingcards.appendChild(document.createTextNode("\n"));
		}

		Collection<SportCardEntity> sportCards = getTradingcardService().getAllSportCards();
		Iterator<SportCardEntity> iterator_sportCards = sportCards.iterator();
		while(iterator_sportCards.hasNext()) {
			SportCardEntity sportCard = iterator_sportCards.next();
			Element cardNode = document.createElement("sportCard");
			cardNode.setAttribute("id", sportCard.getId().toString());
			cardNode.setAttribute("player", sportCard.getPlayer().getId().toString());
			cardNode.setAttribute("baseSet", sportCard.getBaseSet().getId().toString());
			if (sportCard.getParallelSet().getId() != null)
			{
				cardNode.setAttribute("parallelSet", sportCard.getParallelSet().getId().toString());
			}
			if (sportCard.getInsert().getId() != null)
			{
				cardNode.setAttribute("insert", sportCard.getInsert().getId().toString());
			}
			tradingcards.appendChild(cardNode);
			tradingcards.appendChild(document.createTextNode("\n"));
		}
	}
	
	private void backupBaseSets(Collection<BaseSetEntity> baseSets, Document document, Element manufacturerNode)
	{
		Iterator<BaseSetEntity> iterator_baseSets= baseSets.iterator();
		while(iterator_baseSets.hasNext()) {
			BaseSetEntity baseSet = iterator_baseSets.next();
			manufacturerNode.appendChild(document.createTextNode("\n"));
			Element baseSetNode = document.createElement("baseSet");
			baseSetNode.setAttribute("id", baseSet.getId().toString());
			baseSetNode.setAttribute("name", baseSet.getName());
			manufacturerNode.appendChild(baseSetNode);
		}
	}
	
	private void backupParallelSets(Collection<ParallelSetEntity> parallelSets, Document document, Element manufacturerNode)
	{
		Iterator<ParallelSetEntity> iterator_parallelSets = parallelSets.iterator();
		while(iterator_parallelSets.hasNext()) {
			ParallelSetEntity parallelSet = iterator_parallelSets.next();
			manufacturerNode.appendChild(document.createTextNode("\n"));
			Element parallelSetNode = document.createElement("parallelSet");
			parallelSetNode.setAttribute("id", parallelSet.getId().toString());
			parallelSetNode.setAttribute("name", parallelSet.getName());
			parallelSetNode.setAttribute("baseSet", parallelSet.getBaseSet().getId().toString());
			manufacturerNode.appendChild(parallelSetNode);
		}
	}
	
	private void backupInserts(Collection<InsertEntity> inserts, Document document, Element manufacturerNode)
	{
		Iterator<InsertEntity> iterator_inserts = inserts.iterator();
		while(iterator_inserts.hasNext()) {
			InsertEntity insert = iterator_inserts.next();
			manufacturerNode.appendChild(document.createTextNode("\n"));
			Element insertNode = document.createElement("insert");
			insertNode.setAttribute("id", insert.getId().toString());
			insertNode.setAttribute("name", insert.getName());
			insertNode.setAttribute("baseSet", insert.getBaseSet().getId().toString());
			manufacturerNode.appendChild(insertNode);
		}
	}
	
	private SportService getSportService()
	{
		return null;
	}
	
	private TradingcardService getTradingcardService()
	{
		return null;
	}
}
