using System;
using System.Configuration;
using Valg.EVA.Skanning.Contracts;

namespace Valg.EVA.Skanning.UserInterfaceElements.ErrorHandling
{
    public class ExceptionConverter : IExceptionConverter
    {
        public IException Convert(Exception exception)
        {
            var exceptionAsIException = exception as IException;
            if (exceptionAsIException != null)
            {
                return exceptionAsIException;
            }

            if (exception is ConfigurationErrorsException)
            {
                return new ConfigurationPresentationException(exception);
            }

            if (exception is DatabaseConnectionException)
            {
                return new DatabaseConnectionPresentationException();
            }

            if (exception is DatabaseCompatibilityException)
            {
                var exc = (DatabaseCompatibilityException) exception;
                return new DatabaseCompatibilityPresentationException(exc.CurrentDatabaseVersion, exc.ExpectedDatabaseVersion);
            }
            
            return new UnhandledPresentationException(exception);
        }
    }
}

