package be.iminds.iot.things.repository.simple.provider;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.osgi.framework.BundleContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;

import aQute.lib.json.JSONCodec;
import aQute.lib.converter.TypeReference;
import be.iminds.iot.things.api.Thing;
import be.iminds.iot.things.repository.api.Repository;
import be.iminds.iot.things.repository.api.ThingDTO;

/**
 * 
 */
@Component(property={"event.topics=be/iminds/iot/thing/*"})
public class ThingsRepository implements Repository, EventHandler {
	
	private final static JSONCodec json = new JSONCodec();
	
	private Map<UUID, ThingDTO> things = new HashMap<>();
	
	
	@Activate
	public void activate(BundleContext context){
		try {
			things = (Map<UUID, ThingDTO>) json.dec().from(new File("things.txt")).get(new TypeReference<Map<UUID,ThingDTO>>(){});
		} catch(Exception e){
			e.printStackTrace();
		}
	}
	
	@Deactivate
	public void deactivate(){
		try {
			json.enc().indent("\t").to(new File("things.txt")).put(things).close();
		} catch(Exception e){
			e.printStackTrace();
		}
	}
	
	@Override
	public ThingDTO getThing(UUID id) {
		System.out.println("GET "+id);
		return things.get(id);
	}

	@Override
	public Collection<ThingDTO> getThings() {
		System.out.println("LIST "+things.values().size());
		return Collections.unmodifiableCollection(new ArrayList(things.values()));
	}

	@Override
	public void putThing(ThingDTO thing) {
		System.out.println("PUT");
		things.put(thing.id, thing);
	}

	@Override
	public void handleEvent(Event event) {
		// TODO Auto-generated method stub
		UUID id = (UUID) event.getProperty(Thing.ID);
		if(!things.containsKey(id)){
			ThingDTO thing = new ThingDTO();
			thing.id = id;
			thing.gateway = (UUID) event.getProperty(Thing.GATEWAY);
			thing.device = (String) event.getProperty(Thing.DEVICE);
			thing.service = (String) event.getProperty(Thing.SERVICE);
			thing.type = (String) event.getProperty(Thing.TYPE);
			thing.name = thing.service;
			
			things.put(id, thing);
		}

		// TODO keep all events
		// TODO keep track of online/offline thing status
	}

}

@@ -104,33 +104,8 @@
 st;%0A
-import java.util.Arrays;%0A
 impo
@@ -202,19 +202,67 @@
 va.util.
-Map
+HashSet;%0Aimport java.util.Map;%0Aimport java.util.Set
 ;%0Aimport
@@ -577,41 +577,8 @@
 r;%0A%0A
-import aQute.lib.json.JSONCodec;%0A
 impo
@@ -615,16 +615,49 @@
 erence;%0A
+import aQute.lib.json.JSONCodec;%0A
 import b
@@ -1051,16 +1051,65 @@
 ap%3C%3E();%0A
+%09private Set%3CThingDTO%3E online = new HashSet%3C%3E();%0A
 %09%0A%09%0A%09@Ac
@@ -1688,24 +1688,55 @@
 tThings() %7B%0A
+%09%09// only return online things%0A
 %09%09System.out
@@ -1835,31 +1835,22 @@
 rayList(
-things.values()
+online
 ));%0A%09%7D%0A%0A
@@ -2019,45 +2019,8 @@
 ) %7B%0A
-%09%09// TODO Auto-generated method stub%0A
 %09%09UU
@@ -2069,49 +2069,63 @@
 ;%0A%09%09
-if(!things.containsKey
+ThingDTO thing = things.get
 (id)
-)%7B%0A%09%09%09ThingDTO 
+;%0A%09%09if(thing==null)%7B%0A%09%09%09
 thin
@@ -2465,16 +2465,18 @@
 g);%0A%09%09%7D%0A
+%09%09
 %0A%09%09// TO
@@ -2482,74 +2482,253 @@
 ODO 
-keep all events%0A%09%09// TODO keep track of online/offline thing statu
+implement more correct check%0A%09%09if(event.getTopic().startsWith(%22be/iminds/iot/thing/online/%22))%7B%0A%09%09%09online.add(thing);%0A%09%09%7D else if(event.getTopic().startsWith(%22be/iminds/iot/thing/offline/%22))%7B%0A%09%09%09online.remove(thing);%0A%09%09%7D%0A%0A%09%09// TODO keep all event
 s%0A%09%7D

return only online things from repository