package org.gateway.home.tmaestroTeacherService.iface;

import java.util.Hashtable;
import java.util.Set;
import java.util.Vector;

public class DataConverter {
	
	private TMaestroTeacherServiceImpl tmaestroTeacherService = null;

	public DataConverter(TMaestroTeacherServiceImpl tmaestroTeacherService) {
		// TODO Auto-generated constructor stub
		this.tmaestroTeacherService = tmaestroTeacherService;
	}

	public void publishTools(Hashtable<String, TeacherToolsGroup> toolsGroups) {
		// TODO Auto-generated method stub
		Set<String> keys = toolsGroups.keySet();
		
		for (String key : keys) {
			TeacherToolsGroup toolsGroup = toolsGroups.get(key);
			publishTeacherToolsGroup(toolsGroup);
		}
		
		tmaestroTeacherService.updateObject("userService", "USER_SRV_TMAESTRO_TEACHER", "childList", tmaestroTeacherService.getListTeacherToolsGroups());
		
	}

	public void publishTeacherToolsGroup(TeacherToolsGroup toolsGroup) {
		// TODO Auto-generated method stub
		Hashtable<String,String> attributes = new Hashtable<String, String>();
		attributes.put("itemName", toolsGroup.getItemName());
		attributes.put("title",toolsGroup.getTitle());
		attributes.put("state", toolsGroup.getState());
		attributes.put("parent", "USER_SRV_TMAESTRO_TEACHER");
		attributes.put("childList", toolsGroup.getListTeacherTools());
		
		System.out.println("ListTeacherTools: "+toolsGroup.getListTeacherTools());
		
		tmaestroTeacherService.createObject("teacherToolsGroup", toolsGroup.getId(), attributes);
		
		Vector<TeacherTool> tools = toolsGroup.getTeacherTools();
		
		for (TeacherTool tool : tools) {
			publishTeacherTool(tool);
		}
	}

	public void publishTeacherTool(TeacherTool tool) {
		// TODO Auto-generated method stub
		Hashtable<String,String> attributes = new Hashtable<String, String>();
		
		attributes.put("parent", tool.getToolsGroup());
		//attributes.put("itemName", course.getItemName());
		attributes.put("itemName", tool.getTitle());
		attributes.put("title",tool.getTitle());
		attributes.put("state", tool.getState());	
		//attributes.put("resourceUrl", course.getResourceUrl());
		attributes.put("icon", tool.getIcon());
		attributes.put("littleIcon",tool.getLitteIcon());
		attributes.put("action", tool.getAction());
		attributes.put("targetUrl", tool.getTargetUrl());
		System.out.println(tool.getTargetUrl());
		System.out.println(attributes);
		//attributes.put("itemDescription", course.getTitle());
		attributes.put("itemDescription", tool.getDescription());
		
		tmaestroTeacherService.createObject("teacherTool", tool.getId(), attributes);
		
	}
	
	public void removeTeacherToolsGroups(Hashtable<String, TeacherToolsGroup> toolsGroups) {
		Set<String> keys = toolsGroups.keySet();
		
		for (String key: keys) {
			TeacherToolsGroup toolsGroup = toolsGroups.get(key);
			removeTeacherToolsGroup(toolsGroup);
		}
		
		tmaestroTeacherService.updateObject("userService", "USER_SRV_TMAESTRO_TEACHER", "childList", "");
	}

	private void removeTeacherToolsGroup(TeacherToolsGroup toolsGroup) {
		// TODO Auto-generated method stub
		tmaestroTeacherService.removeObject("teacherToolsGroup", toolsGroup.getId());
		
	}

}
