using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using qdr.app.bundleboiler.business.Services.Interfaces;
using qdr.app.bundleboiler.data.Model;
using qdr.app.bundleboiler.ui.Configuration;
using qdr.app.bundleboiler.ui.Dialogs;
using Quadarax.Foundation.Core.Business.Injection;
using Quadarax.Foundation.Core.Logging;
using System.Windows.Forms;

namespace bundleboiler.ui
{
    public partial class MainForm : Form
    {
        #region *** Private fields ***
        ILog? _log;
        IDatabaseService _serviceDatabase;
        #endregion

        #region *** Constructor ***
        public MainForm(IDatabaseService? serviceDatabase, Quadarax.Foundation.Core.Logging.ILogger? logger)
        {
            InitializeComponent();
            _log = logger?.GetLogger(GetType().Name);
            _serviceDatabase = serviceDatabase ?? throw new ArgumentNullException(nameof(serviceDatabase));
            InitializeInternal();
        }
        #endregion

        #region *** Private operations ***
        #region **** Initialiation ****
        private void InitializeInternal()
        {
            var loggerFactory = new Quadarax.Foundation.Core.NLog.LoggerFactory();
            try
            {
                _serviceDatabase.EnsureDatabase();
            }
            catch (Exception e)
            {
                new ErrorMessageDialog(loggerFactory, "Cannot initialize application", e).ShowDialog();
            }
        }
        #endregion
        #region **** Logging ****
        private void LogDebug(string message)
        {
            _log?.Log(LogSeverityEnum.Debug, message);
        }
        #endregion
        #endregion
    }
}
