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

namespace Knoks.Core.Data.Dao
{
    public class AccountDao : IAccountDao
    {
        private readonly ILogger<AccountDao> _logger;
        private readonly IProcExecutor _executor;

        public AccountDao(ILogger<AccountDao> logger, IProcExecutor executor)
        {
            _logger = logger;
            _executor = executor;
        }

        public async Task<UserAccounts> GetUserAccounts(long userId)
        {
            _logger.LogDebug($"Get User Accounts for user id: {userId}");

            var result = await _executor.Go("API_GetUserAccounts", new { userId });
            
            var user = result.Тables[0][0].To<User>();
            var accounts = result.Тables[1].Rows.Select(r => r.To<Account>());

            return new UserAccounts
            {
                User = user,
                Accounts = accounts
            };
        }

        public async Task<IEnumerable<int>> GetFavorites(long? userId)
        {
            return (await _executor.Go("BO_GetUserFavoriteKnoks", new { userId })).Тables[0].ToPrimitiveList<int>();
        }
        public async Task<IEnumerable<int>> AddFavorite(long? userId, int knokId)
        {
            return (await _executor.Go("BO_AddUserFavoriteKnok", new { userId, knokId })).Тables[0].ToPrimitiveList<int>();
        }
        public async Task<IEnumerable<int>> RemoveFavorite(long? userId, int knokId)
        {
            return (await _executor.Go("BO_RemoveUserFavoriteKnok", new { userId, knokId })).Тables[0].ToPrimitiveList<int>();
        }
        public async Task<IEnumerable<long>> GetFollows(long? userId)
        {
            return (await _executor.Go("BO_GetUserFollows", new { userId })).Тables[0].ToPrimitiveList<long>();
        }
        public async Task<IEnumerable<long>> AddFollow(long? userId, long followUserId)
        {
            return (await _executor.Go("BO_AddUserFollow", new { userId, followUserId })).Тables[0].ToPrimitiveList<long>();
        }
        public async Task<IEnumerable<long>> RemoveFollow(long? userId, long followUserId)
        {
            return (await _executor.Go("BO_RemoveUserFollow", new { userId, followUserId })).Тables[0].ToPrimitiveList<long>();
        }
    }
}
