using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using StockPortfolio.Data;
using System.Runtime.Serialization;

namespace StockPortfolio.Model
{
   
  
   public class StockDataItem:Common.BindableBase
    {
        public int Id { get; set; }
     
        public string Symbol { get; set; }


        private int _count;

        public int Count
        {
            get { return _count; }
            set {this.SetProperty(ref this._count, value);}
        }
        
        public string CompanyName { get; set; }
   
        public bool IsLoading { get; set; }

        public double AdjClose
        {
            get
            {
                if (this._historicalStock != null)
                {
                    return _historicalStock.Last<HistoricalStock>().AdjClose;
                }
                return -1;
            }
        }
        public double Volume
        {
            get
            {
                if(this._historicalStock != null)
                {
                    return _historicalStock.Last<HistoricalStock>().Volume;
                }
                return -1;
            }
        }

        

        #region Current Stock

        public async Task UpdateCurrentStockQuote()
        {
            this._currentStockQuote = await StockData.GetStockQuoteData(this.Symbol);
            //this.CurrentStockQuote = await StockData.GetStockQuoteData(this.Symbol);
          
        }

        

        private StockQuotes _currentStockQuote;
        public StockQuotes CurrentStockQuote
        {
            get 
            { 
                
                return this._currentStockQuote; 
            }
            set { this.SetProperty(ref this._currentStockQuote, value); }
        }

        //public async void GetCurrentStockQuote()
        //{
        //    this.CurrentStockQuote = await StockData.GetStockQuoteData(this.Symbol);
        //}

        #endregion

        #region Historical Stock

        private List<HistoricalStock> _historicalStock;
        public List<HistoricalStock> HistoricalStock
        {
            get { return this._historicalStock; }
            set { this.SetProperty(ref this._historicalStock, value); }
        }

        private List<string> _axisLabels;
        public List<string> AxisLabels
        {
            get { return this._axisLabels; }
            set { this.SetProperty(ref this._axisLabels, value); }
        }

        public async Task UpdateHistoricalStockData(DateTime startDate)
        {
            this.IsLoading = true;
                
            this._historicalStock = await StockData.GetStockHistoricalData(this.Symbol, startDate, DateTime.Now);
           
            this.IsLoading = false;
            UpdateAxisLabels(startDate);

            OnPropertyChanged("HistoricalStock");
        }

        private void UpdateAxisLabels(DateTime startDate)
        {
            // determine date format
            TimeSpan timeDifference = DateTime.Now - startDate;
            string format = "MMM d";
            if(timeDifference > new TimeSpan(300, 24, 60, 60))
            {
                format = " MMM yyyy ";
            }
            
            this._axisLabels = new List<string>();

            for(int i = 0; i < this.HistoricalStock.Count; i++)
            {
                this._axisLabels.Add(this.HistoricalStock[i].Date.ToString(format));
            }
            OnPropertyChanged("AxisLabels");
        }

        //public async void GetHistoricalStockData()
        //{
        //    this.HistoricalStock = await StockData.GetStockHistoricalData(this.Symbol, new DateTime(2013, 1, 1), DateTime.Now);
        //}
        #endregion

    }
}
