using System;
using System.Collections.Generic;
using System.Linq;
using MarketData.MarketDataModel;
using DataDisplay.DataSource;

namespace Navigator.MarketDataModel
{
  public class BollingerBandModel
  {
    private BollingerBandModel()
    {
    }
    public static CompositeDataSource SMAN(List<BollingerBandElement> bollingerBands)
    {
      if (null == bollingerBands) return null;
      CompositeDataSource compositeDataSource;
      var xData = new EnumerableDataSource<DateTime>(bollingerBands.Select(x => x.Date.Date));
      xData.SetXMapping(x => (x.Ticks / 10000000000.0));
      var yData = new EnumerableDataSource<double>(bollingerBands.Select(y => y.SMAN));
      yData.SetYMapping(y => y);
      compositeDataSource = xData.Join(yData);
      return compositeDataSource;
    }
    public static CompositeDataSource K(List<BollingerBandElement>  bollingerBands)
    {
      if (null == bollingerBands) return null;
      CompositeDataSource compositeDataSource;
      var xData = new EnumerableDataSource<DateTime>(bollingerBands.Select(x => x.Date.Date));
      xData.SetXMapping(x => (x.Ticks / 10000000000.0));
      var yData = new EnumerableDataSource<double>(bollingerBands.Select(y => y.K));
      yData.SetYMapping(y => y);
      compositeDataSource = xData.Join(yData);
      return compositeDataSource;
    }
    public static CompositeDataSource KL1(List<BollingerBandElement>  bollingerBands)
    {
      if (null == bollingerBands) return null;
      CompositeDataSource compositeDataSource;
      var xData = new EnumerableDataSource<DateTime>(bollingerBands.Select(x => x.Date.Date));
      xData.SetXMapping(x => (x.Ticks / 10000000000.0));
      var yData = new EnumerableDataSource<double>(bollingerBands.Select(y => y.KL1));
      yData.SetYMapping(y => y);
      compositeDataSource = xData.Join(yData);
      return compositeDataSource;
    }
    public static CompositeDataSource L(List<BollingerBandElement>  bollingerBands)
    {
      if (null == bollingerBands) return null;
      CompositeDataSource compositeDataSource;
      var xData = new EnumerableDataSource<DateTime>(bollingerBands.Select(x => x.Date.Date));
      xData.SetXMapping(x => (x.Ticks / 10000000000.0));
      var yData = new EnumerableDataSource<double>(bollingerBands.Select(y => y.L));
      yData.SetYMapping(y => y);
      compositeDataSource = xData.Join(yData);
      return compositeDataSource;
    }
    public static CompositeDataSource LP1(List<BollingerBandElement>  bollingerBands)
    {
      if (null == bollingerBands) return null;
      CompositeDataSource compositeDataSource;
      var xData = new EnumerableDataSource<DateTime>(bollingerBands.Select(x => x.Date.Date));
      xData.SetXMapping(x => (x.Ticks / 10000000000.0));
      var yData = new EnumerableDataSource<double>(bollingerBands.Select(y => y.LP1));
      yData.SetYMapping(y => y);
      compositeDataSource = xData.Join(yData);
      return compositeDataSource;
    }
    public static CompositeDataSource High(List<BollingerBandElement>  bollingerBands)
    {
      if (null == bollingerBands) return null;
      CompositeDataSource compositeDataSource;
      var xData = new EnumerableDataSource<DateTime>(bollingerBands.Select(x => x.Date.Date));
      xData.SetXMapping(x => (x.Ticks / 10000000000.0));
      var yData = new EnumerableDataSource<double>(bollingerBands.Select(y => y.High));
      yData.SetYMapping(y => y);
      compositeDataSource = xData.Join(yData);
      return compositeDataSource;
    }
    public static CompositeDataSource Low(List<BollingerBandElement>  bollingerBands)
    {
      if (null == bollingerBands) return null;
      CompositeDataSource compositeDataSource;
      var xData = new EnumerableDataSource<DateTime>(bollingerBands.Select(x => x.Date.Date));
      xData.SetXMapping(x => (x.Ticks / 10000000000.0));
      var yData = new EnumerableDataSource<double>(bollingerBands.Select(y => y.Low));
      yData.SetYMapping(y => y);
      compositeDataSource = xData.Join(yData);
      return compositeDataSource;
    }
    public static CompositeDataSource Close(List<BollingerBandElement>  bollingerBands)
    {
      if (null == bollingerBands) return null;
      CompositeDataSource compositeDataSource;

      var xData = new EnumerableDataSource<DateTime>(bollingerBands.Select(x => x.Date.Date));
      xData.SetXMapping(x => (x.Ticks / 10000000000.0));
      var yData = new EnumerableDataSource<double>(bollingerBands.Select(y => y.Close));
      yData.SetYMapping(y => y);
      compositeDataSource = xData.Join(yData);
      return compositeDataSource;
    }
   // public static CompositeDataSource LeastSquares(List<BollingerBandElement>  bollingerBands)
   // {
   //   if (null == bollingerBands) return null;
   //   CompositeDataSource compositeDataSource;
   //   var xData = new EnumerableDataSource<DateTime>(bollingerBands.Select(x => x.Date.Date));
   //   xData.SetXMapping(x => (x.Ticks / 10000000000.0));
			//LeastSquaresResult leastSquaresResult=bollingerBands.LeastSquaresFitClose();
   //   var yData = new EnumerableDataSource<double>(leastSquaresResult.LeastSquares);
   //   yData.SetYMapping(y => y);
   //   compositeDataSource = xData.Join(yData);
   //   return compositeDataSource;
   // }
   // public static CompositeDataSource LeastSquaresExtend(List<BollingerBandElement>  bollingerBands,DateTime extendToDate)
   // {
   //   if (null == bollingerBands) return null;
   //   CompositeDataSource compositeDataSource;

			//LeastSquaresResult leastSquaresResult=bollingerBands.LeastSquaresFitClose();       
   //   DateTime maxDateInBand=bollingerBands.Select(x=>x.Date).Max();
   //   if(maxDateInBand<extendToDate)
   //   {
   //     DateGenerator dateGenerator=new DateGenerator();
   //     List<DateTime> extendedDates=dateGenerator.GenerateHistoricalDates(dateGenerator.FindNextBusinessDay(maxDateInBand),extendToDate);
   //     var xData=new EnumerableDataSource<DateTime>(bollingerBands.Select(x => x.Date.Date).Union(extendedDates.Select(x=>x)).OrderByDescending(x=>x.Date));
   //     leastSquaresResult.Extend(extendedDates.Count,leastSquaresResult.Slope);
   //     xData.SetXMapping(x => (x.Ticks / 10000000000.0));
   //     var yData = new EnumerableDataSource<double>(leastSquaresResult.LeastSquares);
   //     yData.SetYMapping(y => y);
   //     compositeDataSource = xData.Join(yData);
   //   }
   //   else
   //   {
   //     var xData = new EnumerableDataSource<DateTime>(bollingerBands.Select(x => x.Date.Date));
   //     xData.SetXMapping(x => (x.Ticks / 10000000000.0));
   //     var yData = new EnumerableDataSource<double>(leastSquaresResult.LeastSquares);
   //     yData.SetYMapping(y => y);
   //     compositeDataSource = xData.Join(yData);
   //   }
   //   return compositeDataSource;
   // }
    public static CompositeDataSource Volume(BollingerBands bollingerBands)
    {
      if (null == bollingerBands) return null;
      CompositeDataSource compositeDataSource;
      var xData = new EnumerableDataSource<DateTime>(bollingerBands.Select(x => x.Date.Date));
      xData.SetXMapping(x => (x.Ticks / 10000000000.0));
      var yData = new EnumerableDataSource<long>(bollingerBands.Select(y => y.Volume));
      yData.SetYMapping(y => y);
      compositeDataSource = xData.Join(yData);
      return compositeDataSource;
    }
  }
}
