using System;
using System.Reflection;
using C4IT.FASD.Base;
using C4IT.Logging;
using static C4IT.Logging.cLogManager;

namespace FasdDesktopUi.Basics.Services.Models
{
    internal interface ITicketOverviewSettingsProvider
    {
        int GetPollingMinutes(TileScope scope);
    }

    internal sealed class TicketOverviewSettingsProvider : ITicketOverviewSettingsProvider
    {
        public int GetPollingMinutes(TileScope scope)
        {
            MethodBase CM = null;
            if (cLogManager.DefaultLogger.IsDebug)
            {
                CM = MethodBase.GetCurrentMethod();
                LogMethodBegin(CM);
            }

            int minutes = scope == TileScope.Role
                ? cF4sdTicketConfig.DefaultOverviewPollingRole
                : cF4sdTicketConfig.DefaultOverviewPollingPersonal;

            try
            {
                var ticketConfig = cFasdCockpitConfig.Instance?.Global?.TicketConfiguration;
                if (ticketConfig != null)
                {
                    minutes = scope == TileScope.Role
                        ? ticketConfig.OverviewPollingRole
                        : ticketConfig.OverviewPollingPersonal;
                }
            }
            catch (Exception ex)
            {
                LogException(ex);
            }
            finally
            {
                LogMethodEnd(CM);
            }

            if (minutes < 1)
            {
                minutes = 1;
            }

            return minutes;
        }
    }
}
