package com.peetz.kontor.data;

import java.util.ArrayList;
import java.util.Collection;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.peetz.medien.entity.AudioCDEntity;
import com.peetz.medien.service.MedienService;

public class ImportMedien
{
	public void parseNode(Node xmlNode)
	{
		NodeList childNodes = xmlNode.getChildNodes();
		for (int i=0; i<childNodes.getLength(); i++)
		{
			Node node = childNodes.item(i);
			if (node.getNodeName().equals("audioCD")) parseAudioCD(node);
			if (node.getNodeName().equals("film")) parseFilm(node);
			if (node.getNodeName().equals("boxSet")) parseBoxSet(node);
		}
	}

	public void parseAudioCD(Node node)
	{
		NamedNodeMap attr = node.getAttributes();
		Node idNode = attr.getNamedItem("id");
		String id = idNode.getNodeValue();
		
		Node albumNode = attr.getNamedItem("album");
		String album = albumNode.getNodeValue();
		
		Node artistNode = attr.getNamedItem("artist");
		String artist = artistNode.getNodeValue();
		
		String category = null;
		
		String year = null;
		
		Boolean wantList = Boolean.FALSE;
		
		Collection<String> songs = new ArrayList<String>();
		
		System.out.println("AudioCD: " + id + ", " + album + "/" + artist);
		AudioCDEntity audioCD = getMedienService().addCD(album);
		audioCD.setArtist(artist);
		audioCD.setCategory(category);
		audioCD.setReleaseYear(year);
		audioCD.setWantList(wantList);
		audioCD.getSongs().addAll(songs);
		getMedienService().saveCD(audioCD);
	}
	
	public void parseFilm(Node node)
	{
		NamedNodeMap attr = node.getAttributes();
		Node idNode = attr.getNamedItem("id");
		String id = idNode.getNodeValue();
		
		Node titleNode = attr.getNamedItem("title");
		String title = titleNode.getNodeValue();
		
		System.out.println("DVD: " + id + ", " + title);
		MedienService service = getMedienService();

		service.addDVD(title);
	}
	
	public void parseBoxSet(Node node)
	{
		NamedNodeMap attr = node.getAttributes();
		Node idNode = attr.getNamedItem("id");
		String id = idNode.getNodeValue();
		
		Node titleNode = attr.getNamedItem("title");
		String title = titleNode.getNodeValue();
		
		System.out.println("BoxSet: " + id + ", " + title);
		MedienService service = getMedienService();
		
		service.addBoxSet(title);
	}
	
	private MedienService getMedienService()
	{
		return null;
	}
}
