﻿namespace Vert.Db.StockSample
{
    using System;

    public class Program
    {
        private static void Main(string[] args)
        {
            if (args == null || args.Length < 1)
            {
                DisplayUsage();
                return;
            }
            if (args[0] == "gen")
            {
                Console.WriteLine(@"Stock price generator started. Commands:\n
<O> - open market\n
<C> - close market\n
<R> - market reset\n
<ESC> - exit");
                ProcessGeneratorKeys();
            }
            else if (args[0] == "view")
            {
                Console.WriteLine(@"Stock price viewer started. Commands:\n
<R> - manual refresh\n
<A> - auto refresh\n
<S> - stop auto refresh\n
<1> - MSFT history\n
<2> - APPLE history\n
<3> - GOOGLE history\n
<F1> - MSFT prices\n
<F2> - APPLE prices\n
<F3> - GOOGLE prices\n
<ESC> - exit");
                ProcessViewerKeys();
            }
            else
            {
                DisplayUsage();
            }
        }

        private static void DisplayUsage()
        {
            Console.WriteLine("Usage args:");
            Console.WriteLine("gen - stock price generation mode.");
            Console.WriteLine("view - stock price view mode.");
        }

        private static void ProcessGeneratorKeys()
        {
            var cki = new ConsoleKeyInfo();
            do
            {
                cki = Console.ReadKey(true);

                if (cki.Key == ConsoleKey.O)
                {
                    Console.WriteLine("Opening market...");
                    StockTicker.Instance.OpenMarket();
                }

                if (cki.Key == ConsoleKey.C)
                {
                    Console.WriteLine("Closing market...");
                    StockTicker.Instance.CloseMarket();
                }

                if (cki.Key == ConsoleKey.R)
                {
                    Console.WriteLine("Resetting market...");
                    StockTicker.Instance.Reset();
                }

            } while (cki.Key != ConsoleKey.Escape);
        }

        private static void ProcessViewerKeys()
        {
            using (var viewer = new StockViewer())
            {
                var cki = new ConsoleKeyInfo();
                do
                {
                    cki = Console.ReadKey(true);

                    if (cki.Key == ConsoleKey.R)
                    {
                        Console.WriteLine("Refreshing stock prices...");
                        viewer.ManualRefresh();
                    }
                    if (cki.Key == ConsoleKey.A)
                    {
                        Console.WriteLine("Starting Auto-Refresh...");
                        viewer.StartAutoRefresh();
                    }
                    if (cki.Key == ConsoleKey.S)
                    {
                        Console.WriteLine("Stopping Auto-Refresh...");
                        viewer.StopAutoRefresh();
                    }
                    if (cki.Key == ConsoleKey.NumPad1)
                    {
                        viewer.ViewStockHistory(Symbols.Microsoft);
                    }
                    if (cki.Key == ConsoleKey.NumPad2)
                    {
                        viewer.ViewStockHistory(Symbols.Apple);
                    }
                    if (cki.Key == ConsoleKey.NumPad3)
                    {
                        viewer.ViewStockHistory(Symbols.Google);
                    }
                    if (cki.Key == ConsoleKey.F1)
                    {
                        viewer.ViewPriceHistory(Symbols.Microsoft);
                    }
                    if (cki.Key == ConsoleKey.F2)
                    {
                        viewer.ViewPriceHistory(Symbols.Apple);
                    }
                    if (cki.Key == ConsoleKey.F3)
                    {
                        viewer.ViewPriceHistory(Symbols.Google);
                    }

                } while (cki.Key != ConsoleKey.Escape);
            }

        }
    }
}
