using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebSocket.Clients;
using WebSocket.Interfaces;
using Knoks.CryptoExchanges.Entities;

namespace Knoks.CryptoExchanges.ExchangeClients
{
    class BittrexExchange: IExchangeContainer
    {
        #region Members
        private BittrexWebsocket4Net socket;
        private BittrexWebClient client;
        private int exchangeId;
        public IDictionary<string, string> ReplaceSymbols { get; set; }
        #endregion

        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="exchangeId"></param>
        public BittrexExchange( int exchangeId) {
            this.exchangeId = exchangeId;
        }

        public async Task InitClient(UpdateData updateData) {

            socket = new BittrexWebsocket4Net();
            socket.Create("https://beta.bittrex.com/signalr");
            socket.Sign((s) => new ConsoleWebClient(
                updateData, 
                this.exchangeId, 
                (ss) =>
                {
                    return client = new BittrexWebClient(ss);
                }, s));
            await socket.Connect();
        }

        public async Task Reconnect(){
            await Task.CompletedTask;
        }

        public async Task RequestPairsData(List<Tuple<string, string>> pairs) {
            await socket.Connect(pairs.Select(x=>$"{x.Item2}-{x.Item1}").ToArray()); //new string[] { "BTC-ADA", "BTC-TRX" }
        }

        public async Task CloseConnection() {
            await socket.Close();
        }
    }
}
