package osgi.enroute.trains.train.manager.example.provider;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import osgi.enroute.trains.cloud.api.TrackForTrain;
import osgi.enroute.trains.train.api.TrainController;
import aQute.bnd.annotation.component.Component;
import aQute.bnd.annotation.component.Reference;

/**
 * 
 */
@Component(name = "osgi.enroute.trains.train.manager.example")
public class ExampleTrainManagerImpl {

	@SuppressWarnings("unused")
	private TrackForTrain trackManager;
	
	private Map<TrainController, TrainDriver> trains = Collections.synchronizedMap(new HashMap<TrainController, TrainDriver>()); 
		

	@Reference
	public void setTrackManager(TrackForTrain t){
		this.trackManager = t;
	}
	
	@Reference(type='*')
	public void addTrainController(TrainController t){
		TrainDriver driver = new TrainDriver(t);
		trains.put(t, driver);
		driver.start();
	}
	
	public void removeTrainController(TrainController t){
		TrainDriver driver = trains.get(t);
		if(driver!=null){
			driver.stop();
		}
	}
	
	/**
	 * Simple class that keeps on driving a train infinitely
	 * without requesting segment access
	 */
	private class TrainDriver {

		private final TrainController train;
		
		public TrainDriver(TrainController t){
			this.train = t;
		}
		
		public void start(){
			this.train.move(50); // 5 cm per second = 11 seconds per segment
		}
		
		public void stop(){
			train.move(0);
		}
	}
}

@@ -58,87 +58,8 @@
 r;%0A%0A
-import java.util.Collections;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 impo
@@ -160,16 +160,64 @@
 roller;%0A
+import aQute.bnd.annotation.component.Activate;%0A
 import a
@@ -257,16 +257,66 @@
 ponent;%0A
+import aQute.bnd.annotation.component.Deactivate;%0A
 import a
@@ -430,16 +430,128 @@
 example%22
+,%0A%09immediate=true,%0A%09provide=Object.class,%0A%09properties=%7B%22osgi.command.scope=trains%22,%22osgi.command.function=move%22%7D
 )%0Apublic
@@ -588,37 +588,8 @@
  %7B%0A%0A
-%09@SuppressWarnings(%22unused%22)%0A
 %09pri
@@ -632,20 +632,16 @@
 private 
-Map%3C
 TrainCon
@@ -651,701 +651,423 @@
 ller
-, T
+ t
 rain
-Driver%3E trains = Collections.synchronizedMap(new HashMap%3CTrainController, TrainDriver%3E()); %0A%09%09%0A%0A%09@Reference%0A%09public void set
+;%0A%09%0A%09@Activate%0A%09public void activate()%7B%0A%09%09// TODO where does the TrainManager get his name from?%0A%09%09// notify 
 Track
+ 
 Manager
-(TrackForTrain t)%7B%0A%09%09this.trackManager = t;%0A%09%7D%0A%09%0A%09@Reference(type='*')%0A%09public void addTrainController(TrainController t)%7B%0A%09%09TrainDriver driver = new TrainDriver(t);%0A%09%09trains.put(t, driver);%0A%09%09driver.start();%0A%09%7D%0A%09%0A%09public void removeTrainController(TrainController t)%7B%0A%09%09TrainDriver driver = trains.get(t);%0A%09%09if(driver!=null)%7B%0A%09%09%09driver.stop();%0A%09%09%7D%0A%09%7D%0A%09%0A%09/**%0A%09 * Simple class that keeps on driving a train infinitely%0A%09 * without requesting segment access%0A%09 */%0A%09private class TrainDriver %7B%0A%0A%09%09private final TrainController train;%0A%09%09%0A%09%09public TrainDriv
+%0A%09%09trackManager.registerTrain(%22Train1%22, %22Train%22);%0A%0A%09%09// turn the train light on %0A%09%09train.light(true);%0A%09%09// start moving on activation%0A%09%09train.move(50);%0A%09%7D%0A%09%0A%09@Deactivate%0A%09public void deactivate()%7B%0A%09%09// stop when deactivated%0A%09%09train.move(0);%0A%09%7D%0A%09%0A%09@Reference%0A%09public void setTrainControll
 er(T
@@ -1083,25 +1083,24 @@
 oller t)%7B%0A%09%09
-%09
 this.train =
@@ -1103,24 +1103,32 @@
 in = t;%0A
-%09
 %09%7D%0A
-%09%09
 %0A%09
+@Reference%0A
 %09public 
@@ -1137,17 +1137,41 @@
 id s
-tart(
+etTrackManager(TrackForTrain t
 )%7B%0A
-%09
 %09%09th
@@ -1180,115 +1180,150 @@
 .tra
-in.move(50); // 5 cm per second = 11 seconds per segment%0A%09%09%7D%0A%09%09%0A%09%09public void stop()%7B%0A%09%09%09train.move(0);%0A%09%09%7D
+ckManager = t;%0A%09%7D%0A%0A%09// make train move from gogo shell command%0A%09public void move(int directionAndSpeed)%7B%0A%09%09this.train.move(directionAndSpeed);
 %0A%09%7D%0A

simplify TrainManager one on one mapping with a TrainController allow to control the train from Gogo shell command