/**
 * Copyright (c) 2014 Etherios, a Division of Digi International, Inc.
 * All rights not expressly granted are reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Etherios 11001 Bren Road East, Minnetonka, MN 55343
 * =======================================================================
 */
package com.etherios.connector.tank;

import com.etherios.connector.core.DataPoint;
import com.etherios.connector.core.events.ConnectionEvent;
import com.etherios.connector.core.events.DataPointEvent;
import com.etherios.connector.core.events.DataPushEvent;
import com.etherios.connector.core.events.EventsListener;
import com.etherios.connector.core.events.FileSystemEvent;
import com.etherios.connector.core.events.FirmwareEvent;
import com.etherios.connector.core.events.RCIEvent;
import com.etherios.connector.core.log.ConnectorLogger;
import com.etherios.connector.core.log.Logger;
import java.util.List;

/**
 * Class to manage the Device Cloud events.
 *
 * <p>It listens to the Cloud Connector events, by implementing the
 * {@link EventsListener} interface.</p>
 */
public class ConnectorEventsListener implements EventsListener {

	/* Variables */

	// Tank Monitor.
	private final TankMonitor tankMonitor;

	/**
	 * Class constructor. Instances a new <code>ConnectorEventsListener</code> 
	 * to manage the Cloud Connector events.
	 *
	 * @param tankMonitor The Tank Monitor.
	 */
	public ConnectorEventsListener(TankMonitor tankMonitor) {
		this.tankMonitor = tankMonitor;
	}

	/**
	 * Returns the Cloud Connector logger.
	 * 
	 * @return the Cloud Connector logger.
	 */
	private ConnectorLogger getLogger() {
		return tankMonitor.getCloudConnector().getLogger();
	}

	@Override
	public void notifyConnectionEvent(ConnectionEvent event) {
		switch(event.getConnectionEventType()) {
			case ConnectionEvent.CONNECTION_STARTING:
				System.out.println("Cloud Connector is connecting...");
				break;
			case ConnectionEvent.CONNECTION_CONNECTED:
				System.out.println("Cloud Connector is CONNECTED!");
				break;
			case ConnectionEvent.CONNECTION_DISCONNECTED:
				System.out.println("Cloud Connector is DISCONNECTED!");
				break;
			case ConnectionEvent.CONNECTION_ERROR:
				ConnectionEvent.ConnectionErrorEvent errorEvent = (ConnectionEvent.ConnectionErrorEvent) event;
				Logger.logSystemError(getLogger(), 
						TankMonitor.LOGGER_PREFIX + errorEvent.getEventLogString() 
						+ " (Error: " + errorEvent.getConnectionErrorType() + ")");
				break;
			default:
				Logger.logSystemError(getLogger(), 
					TankMonitor.LOGGER_PREFIX + "Unknown connection event type: " 
					+ event.getConnectionEventType() + ".");
				break;
		}
	}

	@Override
	public void notifyDataPointEvent(DataPointEvent event) {
		List<DataPoint> dataPoints = tankMonitor.getLastSentDataPoints();
		switch (event.getStatus()) {
			case DataPointEvent.STATUS_SUCCESS:
				System.out.println("Data points successfully sent: " + event.getStatusMessage());
				break;
			case DataPointEvent.STATUS_BAD_REQUEST:
			case DataPointEvent.STATUS_UNAVAILABLE:
			case DataPointEvent.STATUS_SERVER_ERROR:
				// Add the list of data points to the queue to retry later.
				tankMonitor.retryToSendLater(dataPoints);
				System.out.println("Error sending data points: " + event.getStatusMessage());
				break;
		}
	}

	@Override
	public void notifyDataPushEvent(DataPushEvent event) {
	}
	
	@Override
	public void notifyFileSystemEvent(FileSystemEvent event) {
	}

	@Override
	public void notifyRCIEvent(RCIEvent event) {
	}

	@Override
	public void notifyFirmwareEvent(FirmwareEvent event) {
	}
}
