using Knoks.Core.Data.Interfaces;
using Knoks.Core.Entities;
using Knoks.Core.Entities.Args;
using Knoks.Core.Logic.Interfaces;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;

namespace Knoks.Core.Logic.Managers
{
    public class SystemConfigurationManager : ISystemConfigurationManager
    {
        private readonly ILogger<SystemConfigurationManager> _logger;
        private readonly ISystemConfigurationDao _systemConfigurationDao;
        private SystemConfiguration _systemConfiguration;

        public SystemConfigurationManager(ILogger<SystemConfigurationManager> logger, ISystemConfigurationDao systemConfigurationDao)
        {
            _logger = logger;
            _systemConfigurationDao = systemConfigurationDao;
        }

        public DateTime SystemDateTime
        {
            get
            {
                if (_systemConfiguration != null && _systemConfiguration.IsTestMode)
                    return _systemConfiguration.SystemDateTime;
                else
                    return DateTime.UtcNow;
            }
        }

        private async Task<SystemConfiguration> GetSystemConfiguration()
        {
            _systemConfiguration =  await _systemConfigurationDao.GetSystemConfiguration();

            return _systemConfiguration;
        }

        public async Task<SystemConfiguration> UpdateSystemConfiguration(SystemConfigurationArgs args)
        {
            _systemConfiguration = await _systemConfigurationDao.UpdateSystemConfiguration(args);

            return _systemConfiguration;
        }
    }
}
