package de.dass_it.vanhelsing.gui;

import javax.faces.event.ActionEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import de.dass_it.www.vanhelsing.VanHelsingStub.*;
import de.dass_it.vanhelsing.gui.items.ConcreteUserObjectItem;

/*
 * JobSchedule will list all schedules and the jobs which are associated with a given schedule.
 */
public class JobScheduleBean  {
	private DataTree dataTree;
	private ConcreteUserObjectItem selectedObject;

	public JobScheduleBean(){
		init();
	}
	private void init(){
		Client c = new Client();
		UserObjectItemFactory UOFactory = new UserObjectItemFactory();
		
		ResourceInfo riSched[];
		ResourceInfo riJobs[];
		SimpleResource sr;
		DefaultMutableTreeNode parent;
		
		dataTree = new DataTree();
		dataTree.setTree("JobSchedule");

		
		//TODO: Director auslesen
		riSched = c.getListResources("bacula-dir", "Schedule");
		for (ResourceInfo rinfo : riSched){
			parent = dataTree.createNode(dataTree.getRoot(), 
					UOFactory.createUserObjectItem(rinfo));

			riJobs = c.getListResources(rinfo.getDirector(), "Job");
			for (ResourceInfo rJobs : riJobs){

				sr = c.getSimpleResource(rJobs.getDirector(), rJobs.getResId());
				for (ResourceAttributeType rat : sr.getResourceAttributeType()){
					if (rat.getKey().toLowerCase().equals("schedule") &&
						rat.getValue().equals(rinfo.getResName())){	
						dataTree.createNode(parent, 
								UOFactory.createUserObjectItem(rJobs));
					}
				}
			}
		}
	}
	
	public void createNodeListener(ActionEvent ae){
		//Methode kann nur mit ausgewählten ParentNode ausgeführt werden
		DefaultMutableTreeNode parent = dataTree.getNodeByName("horst");
		UserObjectItemFactory UOFactory = new UserObjectItemFactory();
		
		//UOFactory.createUserObjectItem(new ResourceAttributeType[], )
		//dataTree.createNode(parent, userObject);
	}
	public void updateNodeListener(ActionEvent ae){
		BeanUtil.setInfoMessage(null, "Die Methode ist noch nicht implementiert");
		
	}
	public void deleteNodeListener(ActionEvent ae){
		BeanUtil.setInfoMessage(null, "Die Methode ist noch nicht implementiert");
		
	}
	public void reloadTreeListener(ActionEvent ae){
		init();
	}
	public void selectNode(ActionEvent ae){
		BeanUtil.setInfoMessage(null, "Die Methode ist noch nicht implementiert");
	}

	public DataTree getDataTree(){
		return dataTree;
	}

	public void setDataTree(DataTree dataTree){
		this.dataTree = dataTree;
	}
}
