using System;
using System.Collections.Generic;
using System.Linq;
using MarketData.MarketDataModel;
using Microsoft.Research.DynamicDataDisplay.DataSources;

namespace TradeBlotter.Model
{
  public class MovingAverageModel
  {
    private MovingAverageModel()
    {
    }
    public static CompositeDataSource Close(MovingAverages movingAverages)
    {
      if (null == movingAverages) return null;
      CompositeDataSource compositeDataSource;
      var xData = new EnumerableDataSource<DateTime>(movingAverages.Select(x => x.Date.Date));
      xData.SetXMapping(x => (x.Ticks / 10000000000.0));
      var yData = new EnumerableDataSource<double>(movingAverages.Select(y => y.Close));
      yData.SetYMapping(y => y);
      compositeDataSource = xData.Join(yData);
      return compositeDataSource;
    }
    public static CompositeDataSource High(MovingAverages movingAverages)
    {
      if (null == movingAverages) return null;
      CompositeDataSource compositeDataSource;
      var xData = new EnumerableDataSource<DateTime>(movingAverages.Select(x => x.Date.Date));
      xData.SetXMapping(x => (x.Ticks / 10000000000.0));
      var yData = new EnumerableDataSource<double>(movingAverages.Select(y => y.High));
      yData.SetYMapping(y => y);
      compositeDataSource = xData.Join(yData);
      return compositeDataSource;
    }
    public static CompositeDataSource Low(MovingAverages movingAverages)
    {
      if (null == movingAverages) return null;
      CompositeDataSource compositeDataSource;
      var xData = new EnumerableDataSource<DateTime>(movingAverages.Select(x => x.Date.Date));
      xData.SetXMapping(x => (x.Ticks / 10000000000.0));
      var yData = new EnumerableDataSource<double>(movingAverages.Select(y => y.Low));
      yData.SetYMapping(y => y);
      compositeDataSource = xData.Join(yData);
      return compositeDataSource;
    }
    public static CompositeDataSource MA200(MovingAverages movingAverages)
    {
      if (null == movingAverages) return null;
      movingAverages=new MovingAverages((from MovingAverageElement element in movingAverages where !double.IsNaN(element.MA200) select element).ToList());
      CompositeDataSource compositeDataSource;
      var xData = new EnumerableDataSource<DateTime>(movingAverages.Select(x => x.Date.Date));
      xData.SetXMapping(x => (x.Ticks / 10000000000.0));
      var yData = new EnumerableDataSource<double>(movingAverages.Select(y => y.MA200));
      yData.SetYMapping(y => y);
      compositeDataSource = xData.Join(yData);
      return compositeDataSource;
    }
    public static CompositeDataSource MA100(MovingAverages movingAverages)
    {
      if (null == movingAverages) return null;
      movingAverages = new MovingAverages((from MovingAverageElement element in movingAverages where !double.IsNaN(element.MA100) select element).ToList());
      CompositeDataSource compositeDataSource;
      var xData = new EnumerableDataSource<DateTime>(movingAverages.Select(x => x.Date.Date));
      xData.SetXMapping(x => (x.Ticks / 10000000000.0));
      var yData = new EnumerableDataSource<double>(movingAverages.Select(y => y.MA100));
      yData.SetYMapping(y => y);
      compositeDataSource = xData.Join(yData);
      return compositeDataSource;
    }
    public static CompositeDataSource MA55(MovingAverages movingAverages)
    {
      if (null == movingAverages) return null;
      movingAverages=new MovingAverages((from MovingAverageElement element in movingAverages where !double.IsNaN(element.MA55) select element).ToList());
      CompositeDataSource compositeDataSource;
      var xData = new EnumerableDataSource<DateTime>(movingAverages.Select(x => x.Date.Date));
      xData.SetXMapping(x => (x.Ticks / 10000000000.0));
      var yData = new EnumerableDataSource<double>(movingAverages.Select(y => y.MA55));
      yData.SetYMapping(y => y);
      compositeDataSource = xData.Join(yData);
      return compositeDataSource;
    }
    public static CompositeDataSource MA21(MovingAverages movingAverages)
    {
      if (null == movingAverages) return null;
      movingAverages=new MovingAverages((from MovingAverageElement element in movingAverages where !double.IsNaN(element.MA21) select element).ToList());
      CompositeDataSource compositeDataSource;
      var xData = new EnumerableDataSource<DateTime>(movingAverages.Select(x => x.Date.Date));
      xData.SetXMapping(x => (x.Ticks / 10000000000.0));
      var yData = new EnumerableDataSource<double>(movingAverages.Select(y => y.MA21));
      yData.SetYMapping(y => y);
      compositeDataSource = xData.Join(yData);
      return compositeDataSource;
    }
    public static CompositeDataSource MA5(MovingAverages movingAverages)
    {
      if (null == movingAverages) return null;
      movingAverages=new MovingAverages((from MovingAverageElement element in movingAverages where !double.IsNaN(element.MA5) select element).ToList());
      CompositeDataSource compositeDataSource;
      var xData = new EnumerableDataSource<DateTime>(movingAverages.Select(x => x.Date.Date));
      xData.SetXMapping(x => (x.Ticks / 10000000000.0));
      var yData = new EnumerableDataSource<double>(movingAverages.Select(y => y.MA5));
      yData.SetYMapping(y => y);
      compositeDataSource = xData.Join(yData);
      return compositeDataSource;
    }
    public static CompositeDataSource CreateCompositeDataSource(DMAValues dmaValues)
    {
      if (null == dmaValues) return null;
      CompositeDataSource compositeDataSource;
      var xData = new EnumerableDataSource<DateTime>(dmaValues.Select(x => x.Date));
      xData.SetXMapping(x => (x.Ticks / 10000000000.0));
      var yData = new EnumerableDataSource<double>(dmaValues.Select(y => y.MAValue));
      yData.SetYMapping(y => y);
      compositeDataSource = xData.Join(yData);
      return compositeDataSource;
    }
  }
}
