using System;
using System.Data.SqlClient;
using System.Diagnostics;
using Valg.EVA.Skanning.Contracts;
using Valg.EVA.Skanning.Diagnostics;
using Valg.EVA.Skanning.Resources.Language;

namespace Valg.EVA.Skanning.UserInterfaceElements.ErrorHandling
{
    public class ErrorMessageBuilder
    {
        public static ErrorMessage GetErrorMessageAndLogIfNeeded(Exception exception)
        {
            var process = Process.GetCurrentProcess().ProcessName;

            var errorMessage = new ErrorMessage();
            if (exception is DatabaseConnectionException)
            {
                
                errorMessage.Title = LanguageString.Error_DatabaseConnection;
                errorMessage.PossibleSolutions = LanguageString.Error_DatabaseConnection_Solution;
            }
            else if (exception is SqlException)
            {
                EvaSkanningEventSource.Log.UnhandledSqlException(process, exception.ToString());
                errorMessage.Title = LanguageString.Error_GenericDatabaseError;
                errorMessage.PossibleSolutions = LanguageString.Error_GenericDatabaseErrorSolution;
            }
            else if (exception is DatabaseCompatibilityException)
            {
                
                errorMessage.Title = LanguageString.Error_DatabaseCompatibility;
                errorMessage.PossibleSolutions = LanguageString.Error_DatabaseCompatibility_Solution;
            }
            else
            {
                EvaSkanningEventSource.Log.UnhandledException(process, exception.ToString());
                errorMessage.Message = LanguageString.Error_UnhandledError + Environment.NewLine + exception.Message + Environment.NewLine + exception.StackTrace;
            }
            return errorMessage;
        }
    }
}
