//------------------------------------------------------------------------------
// <copyright file="SensorControllerExceptions.cpp" company="LiquidRobotics">
//
// Copyright (c) 2014 Liquid Robotics, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// This software is associated with a release.  Liquid Robotics makes no
// representation that the design, interfaces or component distribution will
// remain unchanged in future releases.
//
// </copyright>
//
// <summary>
//	 Implementation of SensorControllerExceptions, a class-based namespace that
//	 contains exception classes and translation methods.
// </summary>
//
// <owner>Mike Cookson</owner>
//------------------------------------------------------------------------------

#include "SensorControllerExceptions.h"
#include <stdexcept>
#include <typeinfo>

using namespace std;

namespace LiquidRobotics {
namespace EventLib {

IMPLEMENT_INNER_EXCEPTION(SensorControllerExceptions, Aborted, std::runtime_error);
IMPLEMENT_INNER_EXCEPTION(SensorControllerExceptions, CommFailure, std::runtime_error);
IMPLEMENT_INNER_EXCEPTION(SensorControllerExceptions, NotReady, std::runtime_error);
IMPLEMENT_INNER_EXCEPTION(SensorControllerExceptions, ReturnsAsynchronously, std::runtime_error);
IMPLEMENT_INNER_EXCEPTION(SensorControllerExceptions, UnavailableInPowerOffState, std::runtime_error);

enum ExceptionType
{
	EX_NONE,
	EX_ABORTED,
	EX_COMM_FAILURE,
	EX_INVALID_ARG,
	EX_LOGIC,
	EX_NOT_READY,
	EX_POWER_OFF,
	EX_RETURNS_ASYNCHRONOUSLY,
	EX_RUNTIME,
	EX_UNKNOWN = -1
};

const int SensorControllerExceptions::NoException = EX_NONE;

int SensorControllerExceptions::ExceptionToNumber(std::exception& ex)
{
	const type_info& tip = typeid(ex);
	int result = EX_UNKNOWN;

	if (tip == typeid(Aborted))
		result =  EX_ABORTED;
	else if (tip == typeid(CommFailure))
		result = EX_COMM_FAILURE;
	else if (tip == typeid(invalid_argument))
		result = EX_INVALID_ARG;
	else if (tip == typeid(NotReady))
		result = EX_NOT_READY;
	else if (tip == typeid(UnavailableInPowerOffState))
		result = EX_POWER_OFF;
	else if (tip == typeid(ReturnsAsynchronously))
		result = EX_RETURNS_ASYNCHRONOUSLY;
	else
	{
		try
		{
			dynamic_cast<runtime_error&>(ex);
			result = EX_RUNTIME;
		}
		catch(bad_typeid& btid) { }

		if (result == EX_UNKNOWN)
			try
			{
				dynamic_cast<logic_error&>(ex);
				result = EX_LOGIC;
			}
			catch(bad_typeid& btid) { }
	}

	return result;
}

void SensorControllerExceptions::ThrowException(int exceptionNum, const string& text)
{
	ThrowException(exceptionNum, "", text);
}

void SensorControllerExceptions::ThrowException(int exceptionNum, const string& text, const string& text2)
{
	switch(exceptionNum)
	{
	case EX_ABORTED:				throw Aborted(text2);
	case EX_COMM_FAILURE:			throw CommFailure(text2);
	case EX_INVALID_ARG:			throw invalid_argument(text2);
	case EX_LOGIC:					throw logic_error(text2);
	case EX_NONE:					break;
	case EX_NOT_READY:				throw NotReady(text2);
	case EX_POWER_OFF:				throw UnavailableInPowerOffState(text2);
	case EX_RETURNS_ASYNCHRONOUSLY:	throw ReturnsAsynchronously(text2);
	case EX_RUNTIME:				throw runtime_error(text2);
	default:						throw runtime_error(text2);
	}
}

}
}
