using Autofac;
using GreenTree.Nachtragsmanagement.Core;
using GreenTree.Nachtragsmanagement.Core.Domain.Misc;
using GreenTree.Nachtragsmanagement.Data;
using GreenTree.Nachtragsmanagement.Services.Logging;
using GreenTree.Nachtragsmanagement.Services.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace GreenTree.Nachtragsmanagement.Web.Update
{
    public class UpdateProcess
    {
        /// <summary>
        /// Run all actions required for the current version
        /// </summary>
        public void RunUpdateProcess()
        {
            RunUpdateActionsForVersion(AppendixVersion.CurrentVersion);
        }

        /// <summary>
        /// Run all actions required for a specific update version
        /// </summary>
        /// <param name="version">Update version.</param>
        public void RunUpdateProcess(string version)
        {
            RunUpdateActionsForVersion(version);
        }

        /// <summary>
        /// Run all actions for a specific update version
        /// </summary>
        /// <param name="version">Update version.</param>
        private void RunUpdateActionsForVersion(string version)
        {
            if (String.IsNullOrEmpty(version)) return;

            if (CheckExistingDbContextSpecForVersion(version)) return;

            switch (version)
            {
                case "0.9.1.0":
                    {
                        var logger = Singleton<IContainer>.Instance.Resolve<IServiceLogger>();

                        logger.Information("UpdateProcess Test - " + version);
                    }
                    break;
                default:
                    return;
            }

            CreateDbContextSpecForVersion(version);
        }

        /// <summary>
        /// Checks the specified update version for an existing DbContextSpec entity to check wether the actions has already been done
        /// </summary>
        /// <param name="version">Update version.</param>
        /// <returns>True when action already done otherwise False.</returns>
        private bool CheckExistingDbContextSpecForVersion(string version)
        {
            if (String.IsNullOrEmpty(version)) return true;

            var dbContext = Singleton<IContainer>.Instance.Resolve<IDbContext>();

            if (dbContext == null) return true;

            var versionSpecName = String.Format("UpdateRun-{0}", version);

            var versionSpec = dbContext.Get<DbContextSpec>()
                .FirstOrDefault(d => d.Name == versionSpecName);

            if (versionSpec != null) return true;

            return false;
        }

        /// <summary>
        /// Sets a new DbContextSpec entity to determine wether the actions has already been done
        /// </summary>
        /// <param name="version">Update version.</param>
        private void CreateDbContextSpecForVersion(string version)
        {
            if (String.IsNullOrEmpty(version)) return;

            if (CheckExistingDbContextSpecForVersion(version)) return;

            var dbContext = Singleton<IContainer>.Instance.Resolve<IDbContext>();

            if (dbContext == null) return;

            var versionSpec = new DbContextSpec
            {
                Name = String.Format("UpdateRun-{0}", version),
                Value = "True"
            };

            dbContext.Get<DbContextSpec>().Add(versionSpec);
            dbContext.SaveChanges();
        }
    }
}