using Knoks.Core.Data.Interfaces;
using Knoks.Core.Entities;
using Knoks.Core.Entities.Args;
using Knoks.Core.Logic.Interfaces;
using Knoks.Framework.DataAccess;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Knoks.Core.Data.Dao
{
    public class BackofficeDao : IBackofficeDao
    {
        private readonly IProcExecutor _executor;
        private readonly ILogger<BackofficeDao> _logger;
        private readonly ISystemConfigurationManager _systemConfigurationManager;

        public BackofficeDao(IProcExecutor executor, ILogger<BackofficeDao> logger, ISystemConfigurationManager systemConfigurationManager)
        {
            _logger = logger;
            _executor = executor;
            _systemConfigurationManager = systemConfigurationManager;
        }


        public async Task<AccountTransaction> AddAccountTransaction(AddAccountTransactionArgs args, string transactionType)
        {
            _logger.LogDebug($"The operator id: {args.OperatorId} added \"{transactionType}\" account transaction for user id: {args.UserId}");

            var transType = await GetAccountTransactionType(transactionType);
            args.AccountTransactionTypeId = transType.AccountTransactionTypeID;

            var result = await _executor.Go("TRN_AccountTransactions_Add", args);
            return new AccountTransaction() { AccountTransactionId = (result.OutputParams["TransId"] as long?).Value };
        }

        public async Task<AccountTransactionType> GetAccountTransactionType(string transactionType)
        {
            var result = await _executor.Go("TRN_GetAccountTransactionType", new { AcountTransactionTypeName = transactionType });
            return result.Тables[0][0].To<AccountTransactionType>();
        }
    }
}
