using System;
using System.Collections.Generic;
using System.Globalization;
using WpfApplication1.Models;

namespace WpfApplication1.ViewModels
{
    public class DowJonesViewModel : DataSourceViewModelBase
    {
        private List<DowJonesData> data;

        public List<DowJonesData> Data
        {
            get { return this.data; }
            set
            {
                if (this.data != value)
                {
                    this.data = value;
                    this.OnPropertyChanged(() => this.Data);
                }
            }
        }

        public DowJonesViewModel()
        {
            this.GetData("DowJonesData.csv");
        }

        protected override void GetDataCompleted(System.Collections.IEnumerable data)
        {
            this.Data = data as List<DowJonesData>;
        }

        protected override System.Collections.IEnumerable ParseData(System.IO.TextReader dataReader)
        {            
            string line;
            List<DowJonesData> chartData = new List<DowJonesData>();

            while ((line = dataReader.ReadLine()) != null)
            {
                if (string.IsNullOrEmpty(line))
                    continue;

                string[] data = line.Split(',');

                DowJonesData dataItem = new DowJonesData(); 
                dataItem.Date = DateTime.Parse(data[0], CultureInfo.InvariantCulture);
                dataItem.Open = double.Parse(data[1], CultureInfo.InvariantCulture);
                dataItem.High = double.Parse(data[2], CultureInfo.InvariantCulture);
                dataItem.Low = double.Parse(data[3], CultureInfo.InvariantCulture);
                dataItem.Close = double.Parse(data[4], CultureInfo.InvariantCulture);

                chartData.Add(dataItem);
            }

            return chartData;
        }
    }
}
