using eSuite.WorkBench.Services;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace e_suiteRunner.Services
{
    public class DbService : IDbService
    {
        private string connectionString;
        public DbService()
        {
            connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        }

        public async Task DropDatabaseAsync()
        {
            SqlConnection con = new SqlConnection(connectionString);
            string query = "DROP DATABASE esuite";
            SqlCommand cmd = new SqlCommand(query, con);
            try
            {
                await con.OpenAsync();
                await cmd.ExecuteNonQueryAsync();
            }
            finally
            {
                await con.CloseAsync();
            }
        }

        public async Task BackupDatabaseAsync()
        {
            bool folderExists = Directory.Exists($"C:\\eSuiteBackups");
            if (!folderExists)
                Directory.CreateDirectory($"C:\\eSuiteBackups");
            var backupDirectory = $"C:\\eSuiteBackups\\{DateTime.UtcNow.Year}{DateTime.UtcNow.Month}{DateTime.UtcNow.Day}{DateTime.UtcNow.Ticks}-esuite-database-backup.bak";
            SqlConnection con = new SqlConnection(connectionString);
            string query = @$"BACKP DATABASE [esuite] TO  DISK = N'{backupDirectory}'
                             WITH NOFORMAT,
                             NOINIT,
                             NAME = N'esuite-Full Database Backup',
                             SKIP,
                             NOREWIND,
                             NOUNLOAD,
                             STATS = 10";
            SqlCommand cmd = new SqlCommand(query, con);
            try
            {
                await con.OpenAsync();
                await cmd.ExecuteNonQueryAsync();
            }
            finally
            {
                await con.CloseAsync();
            }
        }

        public IEnumerable<string> BackupsList()
        {
            bool folderExists = Directory.Exists($"C:\\eSuiteBackups");
            if (!folderExists)
                return new List<string>();

            var files = Directory.GetFiles("C:\\eSuiteBackups");
            return files.ToList();
        }

        public async Task RestoreDatabaseAsync(string backupName)
        {
            SqlConnection con = new SqlConnection(connectionString);
            string query = @$"USE [master]
                              RESTORE DATABASE [esuite] FROM  DISK = N'{backupName}' 
                              WITH  FILE = 1,  
                              NOUNLOAD,  
                              STATS = 5";
            SqlCommand cmd = new SqlCommand(query, con);
            try
            {
                await con.OpenAsync();
                cmd.ExecuteNonQuery();
            }
            finally
            {
                await con.CloseAsync();
            }
        }
    }
}
