using Valg.EVA.Skanning.Entity.ReadModel;
using Valg.EVA.Skanning.Entity.SqlConnection;
using System;

namespace Valg.EVA.Skanning.Repositories.System
{
    public class SystemRepository : ISystemRepository
    {
        private readonly ISqlConnectionFactory connectionFactory;

        public SystemRepository(ISqlConnectionFactory connectionFactory)
        {
            this.connectionFactory = connectionFactory;
        }

        public string GetDatabaseVersion()
        {
            return connectionFactory.SP_Execute<string>("System_GetDatabaseVersion");
        }

        public void ExecuteEmptyCommand()
        {
            using (var connection = connectionFactory.CreateSqlConnection())
            {
                using (var command = connectionFactory.CreateSqlCommand(string.Empty, connection))
                {
                    command.Connection.Open();
                }
            }
        }

        public IsDatabaseAliveResult IsDatabaseAlive()
        {
            try
            {
                return connectionFactory.SP_Execute<IsDatabaseAliveResult>("System_IsDatabaseAlive", 5);
            }
            catch (Exception e)
            {
                return new IsDatabaseAliveResult
                {
                    IsAlive = false,
                    DatabaseExeception = e
                };
            }
        }
    }
}
