using Binance.Net.Clients;
using Binance.Net.Objects;
using Binance.TradeRobot.Model.Base;
using CryptoExchange.Net.Authentication;
using Microsoft.Extensions.Caching.Memory;
using SDKAdapter.APIClient;
using System;
using Yitter.IdGenerator;

namespace Binance.TradeRobot.Business
{
    public class BaseBusiness
    {
        protected IFreeSql fsql;
        protected NLogManager logManager;
        protected IIdGenerator idGenerator;
        protected IMemoryCache memoryCache;
        private TimeSpan expirationTimeSpan;


        public BaseBusiness(IFreeSql fsql, NLogManager logManager, IIdGenerator idGenerator, IMemoryCache memoryCache)
        {
            this.fsql = fsql;
            this.logManager = logManager;
            this.idGenerator = idGenerator;
            this.memoryCache = memoryCache;
            expirationTimeSpan = TimeSpan.FromDays(1);
        }

        protected BaseAPIClient GetBaseAPIClient(Enums.Exchange exchange, long accountId, string apiKey, string secret)
        {
            var cacheKey = exchange == Enums.Exchange.Binance ? accountId.ToString() : apiKey;
            if (!memoryCache.TryGetValue(cacheKey, out BaseAPIClient baseAPIClient))
            {
                baseAPIClient = BaseAPIClient.Create(exchange, accountId, apiKey, secret);
                memoryCache.Set(cacheKey, baseAPIClient, expirationTimeSpan);
            }
            return baseAPIClient;
        }
    }
}