using System.Globalization;
using FKGees.Services;

namespace FKGees;

internal class App
{
    private readonly DecretsService _decretsService;
    private readonly PcmService _pcmService;
    private readonly Context _context;
    private ILogger<App> _logger;
    private readonly RegistersService _registersService;

    public App(Context context, ILogger<App> logger, RegistersService registersService, DecretsService decretsService, PcmService pcmService)
    {
        _context = context;
        _logger = logger;
        _registersService = registersService;
        _decretsService = decretsService;
        _pcmService = pcmService;
    }

    public async Task Run(string[] args)
    {
        if (args.Length == 0)
        {
            Console.WriteLine("Nie podano argumentów - wykonuję eksport 'Za wczoraj'");
            await Task.Delay(2000);
        }
        else if (args.Length != 2)
        {
            Console.WriteLine("Wymagane są dwa argumenty");
            Console.WriteLine("DataOd DataDo");
            Console.WriteLine("Data w formacie: yyyy-MM-dd np: 2020-12-25");
        }
        else
        {
            if (!DateTime.TryParseExact(args[0], Constants.DefaultdateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var startDate))
            {
                Console.WriteLine("Niewłaściwy format DataOd");
                await Task.Delay(4000);
                Environment.Exit(1);
            }

            if (!DateTime.TryParseExact(args[1], Constants.DefaultdateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var endDate))
            {
                Console.WriteLine("Niewłaściwy format DataOd");
                await Task.Delay(4000);
                Environment.Exit(1);
            }
            endDate = endDate.AddDays(1) - TimeSpan.FromSeconds(1);
            _context.StatDate = startDate;
            _context.EndDate = endDate;
        }

        _context.PaymentForms = await _pcmService.FormyPlatnosci();

        await _registersService.Process();
        await _decretsService.Process();
    }
}