package com.roseindia.rcfaces.tree;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import javax.faces.model.SelectItem;
import javax.faces.model.SelectItemGroup;

import org.rcfaces.core.item.BasicImagesSelectItem;
import org.rcfaces.core.item.BasicSelectItemGroup;

public class TreeDemo {

	private static String imageURL;

	public String getImageURL(){
		return "images/error.gif";
	}

	public void setImageURL(String imageURL){
		this.imageURL= imageURL;
	}

	private static final List servers = new ArrayList();
	static {
		Server server = new Server();
		server.setName("IBM Server");
		
		ServerComponent comp1 = new ServerComponent();
		comp1.setName("Memory");
		
		Description desc1 = new Description();
		desc1.setFirstname("512MB");
		desc1.setId(0);

		comp1.setDescriptions(Arrays.asList(new Description[] { desc1}));

		ServerComponent comp2 = new ServerComponent();
		comp2.setName("RAM");

		Description desc2 = new Description();
		desc2.setFirstname("64MB");
		desc2.setId(0);

		comp2.setDescriptions(Arrays.asList(new Description[] { desc2 }));
		
		ServerComponent comp3 = new ServerComponent();
		comp3.setName("SMPS");
		
		server.setServerComponents(Arrays.asList(new ServerComponent[] { comp1, comp2, comp3 }));
		servers.add(server);
	}

	private static SelectItemGroup serversItem;

	{
		serversItem = new SelectItemGroup("Servers");
		List ch1 = new ArrayList();
		ListIterator it = servers.listIterator();
		while(it.hasNext()) {
			Server server = (Server) it.next();
			SelectItemGroup serverItem = new SelectItemGroup(server.getName());
			ch1.add(serverItem);
			List ch2 = new ArrayList();
			for (ListIterator it2 = server.getServerComponents().listIterator(); it2.hasNext();) {
				ServerComponent comp = (ServerComponent) it2.next();
				BasicSelectItemGroup compItem = new BasicSelectItemGroup(comp.getName());
				ch2.add(compItem);
				List ch3 = new ArrayList();
				for (ListIterator it3 = comp.getDescriptions().listIterator(); it3.hasNext();) {
					Description descp = (Description) it3.next();
					BasicImagesSelectItem descpItem = new BasicImagesSelectItem(
							String.valueOf(descp.getId()), descp.getFirstname());
					descpItem.setImageURL("images/card.gif");
					ch3.add(descpItem);
				}
				compItem.setSelectItems((SelectItem[]) ch3.toArray(new SelectItem[ch3.size()]));
			}
			serverItem.setSelectItems((SelectItem[]) ch2.toArray(new SelectItem[ch2.size()]));
		}
    	serversItem.setSelectItems((SelectItem[]) ch1.toArray(new SelectItem[ch1.size()]));
	}  

	public SelectItem getServers() {
		return serversItem;
	}

	public static class ServerComponent {
		private List descriptions = new ArrayList();

		private String name;

		public List getDescriptions() {
			return descriptions;
		}

		public void setDescriptions(List descriptions) {
			this.descriptions = descriptions;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

	}

	public static class Description {
		private int id;

		private String firstname;

		public int getId() {
			return id;
		}

		public void setId(int id) {
			this.id = id;
		}

		public String getFirstname() {
			return firstname;
		}

		public void setFirstname(String firstname) {
			this.firstname = firstname;
		}	
		
	}

	public static class Server {
		private String name;

		private List serverComponents = new ArrayList();

		public List getServerComponents() {
			return serverComponents;
		}

		public void setServerComponents(List serverComponents) {
			this.serverComponents = serverComponents;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

	}
}