using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using ZuluFeedsSampleApp.Configuration;
using ZuluFeedsSampleApp.Feeds;
using ZuluFeedsSampleApp.Model;
using ZuluFeedsSampleApp.Sender;
using ZuluFeedsSampleApp.Util;

namespace ZuluFeedsSampleApp
{
    class ZuluFeedsSampleApp
    {
        static void Main()
        {
            ZuluTradingServerConfig cfg = new ZuluTradingServerConfig("http://tradingserver.zulutrade.com/");
            ZuluTradeAuthenticationInfo ai = new ZuluTradeAuthenticationInfo("yourZuluAccountUsername", "yourZuluAccountPassword");

            IZuluFeedsGateway feedsGateway = new ZuluFeedsGatewayImpl(ai, cfg);

            try
            {
                WebResponse feedsResponse = feedsGateway.getStream();

                if (feedsResponse != null)
                {
                    Stream stream = feedsResponse.GetResponseStream();

                    StreamReader sr = new StreamReader(stream);
                    String line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        TickMessage tick = TickMessageUtil.getTick(line);
                        if (tick != null)
                        {
                            Console.Write("tick:");
                            Console.Write(" currency: " + tick.Currency);
                            Console.Write(", buy: " + tick.Buy);
                            Console.Write(", sell: " + tick.Sell);
                            Console.Write(", timeStampInUtc: " + tick.DateUTCTimeStamp);
                            Console.Write(", dateAsString: " + tick.DateAsString);
                            Console.WriteLine();
                        }
                    }

                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine("Exception requesting for feeds: " + e.Message);
            }

            Console.WriteLine();
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
    }
}
