/**
 * 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.emulator;

import com.etherios.connector.AbstractPlatformConnector;
import com.etherios.connector.core.log.ConnectorLogger;
import com.etherios.connector.emulator.firmware.IGeneralFirmwareImplementation;
import com.etherios.connector.emulator.firmware.GeneralFirmwareManager;
import com.etherios.connector.emulator.rci.IGeneralRCIExtension;
import com.etherios.connector.emulator.rci.GeneralRCIManager;
import com.etherios.connector.log.DefaultConnectorLogger;

/**
 * Main Cloud Connector class for Emulator devices.
 * 
 * <p>This class is the entry point to start working with Device Cloud in your
 * application.</p>
 */
public class CloudConnector extends AbstractPlatformConnector {

	// Constants
	/** Cloud Connector version. */
	public final static String VERSION = "0.1.0.0";
	
	/**
	 * Class constructor. Instances a new Cloud Connector object.
	 */
	public CloudConnector() {
		this(new PlatformCustomization(), null, null);
	}
	
	/**
	 * Class constructor. Instances a new Cloud Connector object.
	 * 
	 * @param configFile Path of the Cloud Connector configuration file.
	 */
	public CloudConnector(String configFile) {
		this(new PlatformCustomization(configFile), null, null);
	}
	
	/**
	 * Class constructor. Instances a new Cloud Connector object with
	 * the given firmware implementation.
	 * 
	 * @param generalFwImpl The general firmware implementation.
	 */
	public CloudConnector(IGeneralFirmwareImplementation generalFwImpl) {
		this(new PlatformCustomization(), null, generalFwImpl);
	}
	
	/**
	 * Class constructor. Instances a new Cloud Connector object with
	 * the given firmware implementation.
	 * 
	 * @param generalFwImpl The general firmware implementation.
	 * @param configFile Path of the Cloud Connector configuration file.
	 */
	public CloudConnector(IGeneralFirmwareImplementation generalFwImpl, String configFile) {
		this(new PlatformCustomization(configFile), null, generalFwImpl);
	}
	
	/**
	 * Class constructor. Instances a new Cloud Connector object with
	 * the given General RCI extension.
	 * 
	 * @param generalRCIExtension The general RCI extension.
	 */
	public CloudConnector(IGeneralRCIExtension generalRCIExtension) {
		this(new PlatformCustomization(), generalRCIExtension, null);
	}
	
	/**
	 * Class constructor. Instances a new Cloud Connector object with
	 * the given General RCI extension.
	 * 
	 * @param generalRCIExtension The general RCI extension.
	 * @param configFile Path of the Cloud Connector configuration file.
	 */
	public CloudConnector(IGeneralRCIExtension generalRCIExtension, String configFile) {
		this(new PlatformCustomization(configFile), generalRCIExtension, null);
	}
	
	/**
	 * Class constructor. Instances a new Cloud Connector object with
	 * the given firmware and RCI implementations.
	 * 
	 * @param generalRCIExtension The general RCI extension.
	 * @param generalFwImpl The general firmware implementation.
	 */
	public CloudConnector(IGeneralRCIExtension generalRCIExtension, IGeneralFirmwareImplementation generalFwImpl) {
		this(new PlatformCustomization(), generalRCIExtension, generalFwImpl);
	}
	
	/**
	 * Class constructor. Instances a new Cloud Connector object with
	 * the given firmware and RCI implementations.
	 * 
	 * @param generalRCIExtension The general RCI extension.
	 * @param generalFwImpl The general firmware implementation.
	 * @param configFile Path of the Cloud Connector configuration file.
	 */
	public CloudConnector(IGeneralRCIExtension generalRCIExtension, IGeneralFirmwareImplementation generalFwImpl, String configFile) {
		this(new PlatformCustomization(configFile), generalRCIExtension, generalFwImpl);
	}
	
	/**
	 * Class constructor. Instances a new Cloud Connector object with
	 * the given platform customization, firmware and RCI implementations.
	 * 
	 * @param platformCustomization Platform implementation used to retrieve  
	 *                              several connector parameters and execute 
	 *                              some actions.
	 * @param generalRCIExtension The general RCI extension.
	 * @param generalFwImpl The general firmware implementation.
	 */
	private CloudConnector(PlatformCustomization platformCustomization, 
			IGeneralRCIExtension generalRCIExtension, IGeneralFirmwareImplementation generalFwImpl) {
		super(platformCustomization, 
				new GeneralRCIManager(platformCustomization, generalRCIExtension), 
				new GeneralFirmwareManager(platformCustomization, generalFwImpl));
	}
	
	@Override
	public String getConnectorVersion() {
		return VERSION;
	}
	
	/**
	 * Returns the log file path.
	 * 
	 * @return the log file path or {@code null} if not logging to a file.
	 * @see DefaultConnectorLogger
	 */
	public String getLoggerFilePath() {
		DefaultConnectorLogger logger = getDefaultConnectorLogger();
		if (logger == null || !logger.isLoggingToFile())
			return null;
		return logger.getLogFilePath();
	}
	
	/**
	 * Returns {@code true} if logging to file, {@code false} otherwise.
	 * 
	 * @return {@code true} if logging to file, {@code false} otherwise.
	 */
	public boolean isLoggingToFile() {
		DefaultConnectorLogger logger = getDefaultConnectorLogger();
		if (logger == null)
			return false;
		return logger.isLoggingToFile();
	}
	
	/**
	 * Returns {@code true} if logging to console, {@code false} otherwise.
	 * 
	 * @return {@code true} if logging to console, {@code false} otherwise.
	 */
	public boolean isLoggingToConsole() {
		DefaultConnectorLogger logger = getDefaultConnectorLogger();
		if (logger == null)
			return false;
		return logger.isLoggingToConsole();
	}
	
	/**
	 * Retrieves the {@code DefaultConnectorLogger} used to log messages from 
	 * Cloud Connector.
	 * 
	 * @return the {@code DefaultConnectorLogger} used to log messages from 
	 *         Cloud Connector.
	 * @see DefaultConnectorLogger
	 */
	protected DefaultConnectorLogger getDefaultConnectorLogger() {
		ConnectorLogger logger = getLogger();
		if (logger == null || !(logger instanceof DefaultConnectorLogger))
			return null;
		return (DefaultConnectorLogger)logger;
	}
}
