package de.toman.milight;

import java.awt.Color;

import de.toman.milight.events.ChangeBrightnessEvent;
import de.toman.milight.events.ChangeColorEvent;
import de.toman.milight.events.LightEvent;
import de.toman.milight.events.LightListener;

/**
 * This class stores the current state of a given group of lights and adjusts
 * this state each time it changes. It listens to the LightEvent instances fired
 * and is therefore only notified if commands are send to the group of lights
 * via this API and not via remote control or other controllers.
 * 
 * @author Stefan Toman (toman@tum.de)
 */
public class LightObserver {
	private Lights lights;

	private LightState currentState;

	private static final MilightColor INITIAL_COLOR = new MilightColor(
			Color.WHITE);
	private static final float INITIAL_BRIGHTNESS = 1f;
	private static final boolean INITIAL_WHITE_MODE = true;
	private static final boolean INITIAL_ON = false;

	public LightObserver(Lights lights) {
		// call super constructor
		super();

		// set attributes
		this.lights = lights;
		currentState = new LightState(INITIAL_COLOR, INITIAL_BRIGHTNESS,
				INITIAL_WHITE_MODE, INITIAL_ON);

		// add as listener
		lights.addLightListener(new LightListener() {
			public void lightsChanged(LightEvent event) {
				// ChangeColorEvent
				switch (event.getClass().getSimpleName()) {
				case "ChangeColorEvent":
					setCurrentState(new LightState(((ChangeColorEvent) event)
							.getColor(), currentState.getBrightness(),
							currentState.isWhiteMode(), currentState.isOn()));
					break;
				// ChangeBrightnessEvent
				case "ChangeBrightnessEvent":
					setCurrentState(new LightState(currentState.getColor(),
							((ChangeBrightnessEvent) event).getBrightness(),
							currentState.isWhiteMode(), currentState.isOn()));
					break;
				// TODO brightness should be handled differently for
				// white/colored
				// mode
				// ColoredModeEvent
				case "ColoredModeEvent":
					setCurrentState(new LightState(currentState.getColor(),
							currentState.getBrightness(), false, currentState
									.isOn()));
					break;
				// WhiteModeEvent
				case "WhiteModeEvent":
					setCurrentState(new LightState(currentState.getColor(),
							currentState.getBrightness(), true, currentState
									.isOn()));
					break;
				// SwitchOnEvent
				case "SwitchOnEvent":
					setCurrentState(new LightState(currentState.getColor(),
							currentState.getBrightness(), currentState
									.isWhiteMode(), true));
					break;
				// SwitchOffEvent
				case "SwitchOffEvent":
					setCurrentState(new LightState(currentState.getColor(),
							currentState.getBrightness(), currentState
									.isWhiteMode(), false));
					break;
				}
			}
		});
	}

	/**
	 * @return the currentState
	 */
	public LightState getCurrentState() {
		return currentState;
	}

	/**
	 * @param currentState
	 *            the currentState to set
	 */
	private void setCurrentState(LightState currentState) {
		this.currentState = currentState;
	}

	/**
	 * @return the lights
	 */
	public Lights getLights() {
		return lights;
	}
}

@@ -1611,32 +1611,92 @@
 ightnessEvent%22:%0A
+%09%09%09%09%09if (currentState.isWhiteMode()) %7B%0A%09%09%09%09%09%09// white mode%0A%09
 %09%09%09%09%09setCurrentS
@@ -1707,32 +1707,41 @@
 (new LightState(
+%0A%09%09%09%09%09%09%09%09
 currentState.get
@@ -1745,24 +1745,25 @@
 getColor(),%0A
+%09
 %09%09%09%09%09%09%09((Cha
@@ -1805,32 +1805,33 @@
 tness(),%0A%09%09%09%09%09%09%09
+%09
 currentState.isW
@@ -1874,104 +1874,337 @@
 %09%09%09%09
-break;%0A%09%09%09%09// TODO brightness should be handled differently for%0A%09%09%09%09// white/
+%7D else %7B%0A%09%09%09%09%09%09// colored mode%0A%09%09%09%09%09%09MilightColor color = currentState.getColor();%0A%09%09%09%09%09%09color.setBrightness(((ChangeBrightnessEvent) event)%0A%09%09%09%09%09%09%09%09.getBrightness());%0A%09%09%09%09%09%09setCurrentState(new LightState(
 color
-ed%0A%09%09%09%09// mode
+, currentState%0A%09%09%09%09%09%09%09%09.getBrightness(), currentState.isWhiteMode(),%0A%09%09%09%09%09%09%09%09currentState.isOn()));%0A%0A%09%09%09%09%09%7D%0A%09%09%09%09%09break;
 %0A%09%09%09

handle ChangeBrightnessEvents correctly in LightObserver