package org.gateway.home.tmaestroTeacherService.iface;

import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Set;
import java.util.Vector;
import java.util.Map.Entry;

import org.gateway.home.dsmanager.serviceaccess.DSBaseClass;
import org.gateway.home.dsmanager.serviceaccess.DSBaseEvent;
import org.gateway.home.dsmanager.serviceaccess.DSBaseManager;
//import org.gateway.home.tmaestroService.iface.CoursesContainer;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

public class TMaestroTeacherServiceImpl extends DSBaseManager {

	private TMaestroTeacherServiceHandle tmaestroTeacherServiceHandle;
	private DataConverter converter;
	public static String reloadURL;
	public static String icon;
	
	public TMaestroTeacherServiceImpl(BundleContext context) {
		tmaestroTeacherServiceHandle = new TMaestroTeacherServiceHandle(this, context);
		converter = new DataConverter(this);
		
		//Leo del manifest la url donde se localiza el reload
		Bundle bundle = context.getBundle();
		Dictionary<String,String> manifest = bundle.getHeaders();
		reloadURL = manifest.get("reloadURL");
		icon = manifest.get("icon");
		
	}
	
	public void subscriptionReceiptEnd() {
		System.out.println("Se invocó el método subscriptionReceiptEnd");
		//Hashtable<String,CoursesContainer>  newContainers = tmaestroServiceHandle.loadCourses();
		//converter.publishCoursesContainers(newContainers);
		//Publicamos las herramientas disponibles para el profesor (en este caso la de creación de cursos)
		Hashtable<String, TeacherToolsGroup> newToolsGroups = tmaestroTeacherServiceHandle.loadTeacherTools();
		converter.publishTools(newToolsGroups);
		System.out.println("Fin del método subscriptionReceiptEnd");
	}
	
	public String getListTeacherToolsGroups(){
		//return tmaestroServiceHandle.getListCoursesContainers();
		return tmaestroTeacherServiceHandle.getListTeacherToolsGroup();
	}
	
	
	public void objectUpdateRequest(String requestService,String className,String id,Hashtable<String,String>attributes) {
		System.out.println("###Se solicitó la actualización del objeto "+id+" de la clase "+className+", el solicitante es "+requestService);
		System.out.println("   Atributos:");
		for (Entry<String,String> pair:attributes.entrySet()) {
			System.out.println("      - "+pair.getKey()+": "+pair.getValue());
		}
		super.objectUpdateRequest(requestService,className,id,attributes);
		//dataService.objectUpdateRequest(bundleId, className, id, attributes);

	}

	public void objectUpdateRequest(String requestService,String className,String id,String name,String value){
		//dataService.objectUpdateRequest(bundleId, className, id, name, value);
		System.out.println("###Se solicitó la actualización del objeto "+id+" de la clase "+className+", el solicitante es "+requestService);
		System.out.println("   Atributo:");
		System.out.println("      - "+name+": "+value);
		super.objectUpdateRequest(requestService, className, id, name, value);
	}

	
	public void objectUpdateNotification(DSBaseClass object,Set<String> updatedAttributes) {
		
		for (String attr:updatedAttributes) {		
			System.out.println(attr+": "+object.getAttributeValue(attr));
		}
		if (object.getClassName().equals("userService") &&  object.getId().equals("USER_SRV_TMAESTRO"))
		{
			if (updatedAttributes.contains("path") || updatedAttributes.contains("serverUrl"))
			{
				//converter.removeCoursesContainers(tmaestroServiceHandle.getContainers());
				//Hashtable<String,CoursesContainer> newContainers = tmaestroServiceHandle.loadCourses();
				//converter.publishCoursesContainers(newContainers);
			}
		}
	}
	
	public void objectCreationNotification(DSBaseClass object){
		System.out.println("**** Se recibió una notificación de creación de objeto de clase "+object.getClassName());
	}
	
	public void objectRemovalNotification(DSBaseClass object){
		System.out.println("**** Se recibió una notificación de destrucción de objeto de clase "+object.getClassName());
	}
	
	/**
	 * Recibe la notificación del lanzamiento de un evento al que el servicio esté suscrito
	 * @param even Evento lanzado
	 */
	public void eventNotification(DSBaseEvent event) {
		
	
		
	}

	public void objectStockNotification(String arg0, String arg1, Vector<DSBaseClass> arg2) {
		// TODO Auto-generated method stub
		
	}

}
