package com.bhrobotics.morlib;

import java.util.Hashtable;

// TODO: Test me.
public class ControlListener implements Listener {
    private Reactor reactor = Reactor.getInstance();
    private EventEmitter process = reactor.getProcess();
    
    private JoystickFilter joystickFilter = new JoystickFilter();
    protected DSInputFilter dsInputFilter = new DSInputFilter();
    
    private Hashtable eventHandlers = new Hashtable();
    
    public ControlListener() {
        eventHandlers.put("start", new StartHandler());
        eventHandlers.put("stop", new StopHandler());
        eventHandlers.put("startAutonomous", new StartAutonomousHandler());
        eventHandlers.put("stopAutonomous", new StopAutonomousHandler());
        eventHandlers.put("startOperatorControl", new StartOperatorControlHandler());
        eventHandlers.put("stopOperatorControl", new StopOperatorControlHandler());
    }
    
    public void handle(Event event) {
        String name = event.getName();
        
        EventHandler h = (EventHandler) eventHandlers.get(name);
        h.execute();
    }
    
    public void bound(EventEmitter emitter, String event) {}
    public void unbound(EventEmitter emitter, String event) {}
    
    public void start() {}
    public void stop() {}
    public void startAutonomous() {}
    public void stopAutonomous() {}
    public void startOperatorControl() {}
    public void stopOperatorControl() {}
    
    private interface EventHandler {
        public void execute();
    }
    
    private class StartHandler implements EventHandler {
        public void execute() {
            start();
        }
    }
    
    private class StopHandler implements EventHandler {
        public void execute() {
            stop();
        }
    }
    
    private class StartAutonomousHandler implements EventHandler {
        public void execute() {
            startAutonomous();
        }
    }
    
    private class StopAutonomousHandler implements EventHandler {
        public void execute() {
            stopAutonomous();
        }
    }
    
    private class StartOperatorControlHandler implements EventHandler {
        public void execute() {
            process.bind("newDataAvailable", joystickFilter);
            process.bind("newDataAvailable", dsInputFilter);
            startOperatorControl();
        }
    }
    
    private class StopOperatorControlHandler implements EventHandler {
        public void execute() {
            process.unbind("newDataAvailable", joystickFilter);
            process.unbind("newDataAvailable", dsInputFilter);
            stopOperatorControl();
        }
    }
}
@@ -29,37 +29,8 @@
 b;%0A%0A
-import java.util.Hashtable;%0A%0A
 // T
@@ -215,21 +215,23 @@
  %0A    pr
-ivate
+otected
  Joystic
@@ -356,1225 +356,174 @@
    p
-rivate Hashtable eventHandlers = new Hashtable();%0A    %0A    public ControlListener() %7B%0A        eventHandlers.put(%22start%22, new StartHandler());%0A        eventHandlers.put(%22stop%22, new StopHandler());%0A        eventHandlers.put(%22startAutonomous%22, new StartAutonomousHandler());%0A        eventHandlers.put(%22stopAutonomous%22, new StopAutonomousHandler());%0A        eventHandlers.put(%22startOperatorControl%22, new StartOperatorControlHandler());%0A        eventHandlers.put(%22stopOperatorControl%22, new StopOperatorControlHandler());%0A    %7D%0A    %0A    public void handle(Event event) %7B%0A        String name = event.getName();%0A        %0A        EventHandler h = (EventHandler) eventHandlers.get(name);%0A        h.execute();%0A    %7D%0A    %0A    public void bound(EventEmitter emitter, String event) %7B%7D%0A    public void unbound(EventEmitter emitter, String event) %7B%7D%0A    %0A    public void 
+ublic void handle(Event event) %7B%0A        String name = event.getName();%0A        %0A        if (name == %22
 start
-(
+%22
 ) %7B
-%7D
 %0A    
-public void stop() %7B%7D%0A    public void startAutonomous() %7B%7D%0A    public void stopAutonomous() %7B%7D%0A    public void startOperatorControl() %7B%7D%0A    public void stopOperatorControl() %7B%7D%0A    %0A    private interface EventHandler %7B%0A        public void execute();%0A    %7D%0A    %0A    private class StartHandler implements EventHandler %7B%0A        public void execute(
+        start();%0A        %7D else if (name == %22stop%22
 ) %7B%0A
@@ -528,35 +528,34 @@
 %7B%0A            st
-art
+op
 ();%0A        %7D%0A  
@@ -555,549 +555,204 @@
    %7D
-%0A    %7D%0A    %0A    private class StopHandler implements EventHandler %7B%0A        public void execute() %7B%0A            stop();%0A        %7D%0A    %7D%0A    %0A    private class StartAutonomousHandler implements EventHandler %7B%0A        public void execute() %7B%0A            startAutonomous();%0A        %7D%0A    %7D%0A    %0A    private class StopAutonomousHandler implements EventHandler %7B%0A        public void execute() %7B%0A            stopAutonomous();%0A        %7D%0A    %7D%0A    %0A    private class StartOperatorControlHandler implements EventHandler %7B%0A        public void execute(
+ else if (name == %22startAutonomous%22) %7B%0A            startAutonomous();%0A        %7D else if (name == %22stopAutonomous%22) %7B%0A            stopAutonomous();%0A        %7D else if (name == %22startOperatorControl%22
 ) %7B%0A
@@ -923,119 +923,47 @@
    %7D
-%0A    %7D%0A    %0A    private class StopOperatorControlHandler implements EventHandler %7B%0A        public void execute(
+ else if (name == %22stopOperatorControl%22
 ) %7B%0A
@@ -1140,9 +1140,352 @@
 %7D%0A    %7D%0A
+    %0A    public void bound(EventEmitter emitter, String event) %7B%7D%0A    public void unbound(EventEmitter emitter, String event) %7B%7D%0A    %0A    public void start() %7B%7D%0A    public void stop() %7B%7D%0A    public void startAutonomous() %7B%7D%0A    public void stopAutonomous() %7B%7D%0A    public void startOperatorControl() %7B%7D%0A    public void stopOperatorControl() %7B%7D%0A
 %7D

Refactor the ControlListener class.