using Microsoft.EntityFrameworkCore;
using StockFin.Models;
using YahooFinanceApi;

namespace StockFin
{
    public class StocksCache
    {
        public Dictionary<string, double?> actuals = new Dictionary<string, double?>();
        public Dictionary<string, double?> months = new Dictionary<string, double?>();
        FinancesContext context;
        
        public StocksCache() {
            context = new FinancesContext();
            var stocks = context.Stocks.Include(s => s.TimeValues)
                .OrderBy(s => s.Title)
                .ToList();

            foreach (var stock in stocks)
            {
                //var quote2 = await _finnhub.GetQuoteAsync(stock.Ticker);
                //var quote2 =
                //    await _twelveData.GetPriceAsync("AAPL");
                if (!string.IsNullOrWhiteSpace(stock.Ticker))
                {
                    var securities = Yahoo.Symbols(stock.Ticker).Fields(Field.Symbol, Field.RegularMarketPrice, Field.FiftyTwoWeekHigh, Field.RegularMarketPreviousClose,Field.Currency).QueryAsync().GetAwaiter().GetResult();
                    if (securities.Count > 0)
                    {
                        var aapl = securities[stock.Ticker];
                        var price = aapl[Field.RegularMarketPrice];
                        if (price != null)
                        {
                            if (aapl[Field.Currency] == "EUR")
                            {
                                actuals[stock.Ticker] = price;
                            }
                            else
                            { 
                                var rate = FrankfurterCurrencyConverter.GetRateAsync(aapl[Field.Currency], "EUR").GetAwaiter().GetResult();
                                if (rate!=null)
                                {
                                    actuals[stock.Ticker] = price * rate;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (stock.TimeValues.Count > 0)
                        {
                            actuals[stock.Ticker] = stock.TimeValues
                                    .OrderBy(s => s.Date)
                                    .LastOrDefault()?.Value;
                        }
                    }
                }
                else
                {
                    
                }
                
    //            var candles = Yahoo.GetHistoricalAsync(
    //"SPY",
    //DateTime.Today.AddDays(-30),
    //DateTime.Today,
    //Period.Daily).GetAwaiter().GetResult() ;
    //            var history = Yahoo.GetHistoricalAsync(stock.Ticker, new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1), new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(2), Period.Daily).GetAwaiter().GetResult();

    //            foreach (var candle in history)
    //            {
    //                months[stock.Ticker] = (double?)candle.Close;
    //                Console.WriteLine($"DateTime: {candle.DateTime}, Open: {candle.Open}, High: {candle.High}, Low: {candle.Low}, Close: {candle.Close}, Volume: {candle.Volume}, AdjustedClose: {candle.AdjustedClose}");
    //            }

            }
        }
    }
}
