using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Knoks.Core.Data.Dao;
using Knoks.Core.Entities;
using Knoks.Core.Entities.Args.AccountTransactions;
using Knoks.Core.Logic.Managers;
using Knoks.Framework.DataAccess;
using Knoks.Framework.Extentions;
using Knoks.Framework.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace ConsoleTestApp
{
    class Program
    {
        private static ILoggerFactory _loggerFactory = new LoggerFactory();
        private static ProcExecutor _procExecutor;
        private static IConfigurationRoot _configuration;

        static async Task Main(string[] args)
        {
            try
            {
                _configuration = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                    .Build();

                var loggingSection = _configuration.GetSection("Logging");
                _loggerFactory.AddConsole(loggingSection);
                _loggerFactory.AddDebug();

                _procExecutor = new ProcExecutor(_configuration.Load<Dictionary<string, string>>("DbConnections"), _loggerFactory.CreateLogger<ProcExecutor>());

                long a = long.MaxValue;
                int b = (int) a;
                //await TransactionsDaoTest();
                //await UserDaoTest();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            Console.WriteLine("{0}Press Enter to close..{0}", Environment.NewLine);
            Console.ReadLine();
        }

        private static async Task UserDaoTest()
        {
            var dao = new UserDao(_loggerFactory.CreateLogger<UserDao>(), _procExecutor);
            UserType? userType = null;
            var platformUser = await dao.GetUsers(userTypeId: (byte?)userType);
        }

        private static async Task TransactionsDaoTest()
        {
            var userDao = new UserDao(_loggerFactory.CreateLogger<UserDao>(), _procExecutor);
            var accountDao = new AccountDao(_loggerFactory.CreateLogger<AccountDao>(), _procExecutor);

            var userManager = new UserManager(_loggerFactory.CreateLogger<UserManager>(), userDao, null, null, null, null, accountDao, null, _configuration.Load<AccountTransactionsSettings>());
            userManager.Initialize();

            var manager = new AccountTransactionManager(userManager, new AccountTransactionDao(_loggerFactory.CreateLogger<AccountTransactionDao>(), _procExecutor));


            var user = (await userDao.GetUsers(userId: 1)).First();

            var account = (await accountDao.GetUserAccounts(1)).Accounts.First();

            //await manager.ActivateFreeCredit(user, account);
            //await manager.ExecutePublishKnokTransaction(user, account, 1, 25);
        }

        //private static async Task KnokerWinsKnokTest()
        //{

        //}
    }
}
