// --------------------------------------------------------------------------------------------------------------------
// <copyright company="MirGames" file="DatabaseInitializer.cs">
// Copyright 2014 Bulat Aykaev
// This file is part of MirGames.
// MirGames is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
// MirGames is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with MirGames. If not, see http://www.gnu.org/licenses/.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace MirGames.Infrastructure
{
    using System.Collections.Generic;

    internal sealed class DatabaseInitializer : IDatabaseInitializer
    {
        private readonly IEnumerable<IEntityMapper> mappers;

        /// <summary>
        /// Initializes a new instance of the <see cref="DatabaseInitializer"/> class.
        /// </summary>
        /// <param name="mappers">The mappers.</param>
        public DatabaseInitializer(IEnumerable<IEntityMapper> mappers)
        {
            this.mappers = mappers;
        }

        /// <summary>
        /// Creates the database.
        /// </summary>
        /// <param name="connectionString">The connection string.</param>
        public void CreateDatabase(string connectionString)
        {
            var dataContext = new DataContext(this.mappers, connectionString);
            dataContext.Database.Create();
        }

        /// <summary>
        /// Deletes the database.
        /// </summary>
        /// <param name="connectionString">The connection string.</param>
        public void DeleteDatabase(string connectionString)
        {
            var dataContext = new DataContext(this.mappers, connectionString);
            dataContext.Database.Delete();
        }

        /// <summary>
        /// Determines whether database exists.
        /// </summary>
        /// <param name="connectionString">The connection string.</param>
        /// <returns>True whether database exists.</returns>
        public bool Exists(string connectionString)
        {
            var dataContext = new DataContext(this.mappers, connectionString);
            return dataContext.Database.Exists();
        }
    }
}