using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MarketData.MarketDataModel;
using Microsoft.Research.DynamicDataDisplay.DataSources;
using MarketData.Generator.TermStructure;
using MarketData.Utils;

namespace TradeBlotter.Model
{
  public class TermStructureModel
  {
    private TermStructureModel()
    {
    } 
    public static CurveData GetTenorCurve(TermStructureInterpolatorByDate termStrucureInterpolatorByDate,DateTime analysisDate)
    {
      if(null==termStrucureInterpolatorByDate)return null;
      TermStructureInterpolator termStructureInterpolator = termStrucureInterpolatorByDate[analysisDate];
      return termStructureInterpolator.CurveData;
    }
    public static CompositeDataSource GetTenorRates(TermStructureInterpolatorByDate termStrucureInterpolatorByDate,DateTime analysisDate)
    {
      if (null == termStrucureInterpolatorByDate||!termStrucureInterpolatorByDate.ContainsKey(analysisDate)) return null;
      TermStructureInterpolator termStructureInterpolator = termStrucureInterpolatorByDate[analysisDate];
      if(null==termStructureInterpolator.CurveData)return null;
      CompositeDataSource compositeDataSource;
      DateGenerator dateGenerator=new DateGenerator();
      List<DateTime> dates = new List<DateTime>();
      //DateTime today = DateTime.Now;
      dates.Add(analysisDate);
      dates.Add(dateGenerator.GenerateFutureDate(analysisDate,30));
      dates.Add(dateGenerator.GenerateFutureDate(analysisDate,30*3));
      dates.Add(dateGenerator.GenerateFutureDate(analysisDate,30*6));
      dates.Add(dateGenerator.GenerateFutureDate(analysisDate,360*1));
      dates.Add(dateGenerator.GenerateFutureDate(analysisDate,360*2));
      dates.Add(dateGenerator.GenerateFutureDate(analysisDate,360*3));
      dates.Add(dateGenerator.GenerateFutureDate(analysisDate,360*5));
      dates.Add(dateGenerator.GenerateFutureDate(analysisDate,360*7));
      dates.Add(dateGenerator.GenerateFutureDate(analysisDate,360*10));
      dates.Add(dateGenerator.GenerateFutureDate(analysisDate,360*20));
      dates.Add(dateGenerator.GenerateFutureDate(analysisDate,360*30));
      
      double[] rates = new double[]
      {
        double.Parse(termStructureInterpolator.CurveData.GetTenorValue("1D")),
        double.Parse(termStructureInterpolator.CurveData.GetTenorValue("1M")),
        double.Parse(termStructureInterpolator.CurveData.GetTenorValue("3M")),
        double.Parse(termStructureInterpolator.CurveData.GetTenorValue("6M")),
        double.Parse(termStructureInterpolator.CurveData.GetTenorValue("1Y")),
        double.Parse(termStructureInterpolator.CurveData.GetTenorValue("2Y")),
        double.Parse(termStructureInterpolator.CurveData.GetTenorValue("3Y")), 
        double.Parse(termStructureInterpolator.CurveData.GetTenorValue("5Y")),
        double.Parse(termStructureInterpolator.CurveData.GetTenorValue("7Y")), 
        double.Parse(termStructureInterpolator.CurveData.GetTenorValue("10Y")), 
        double.Parse(termStructureInterpolator.CurveData.GetTenorValue("20Y")), 
        double.Parse(termStructureInterpolator.CurveData.GetTenorValue("30Y")) 
      };
      var xData = new EnumerableDataSource<DateTime>(dates);
      xData.SetXMapping(x => (x.Ticks / 10000000000.0));
      var yData = new EnumerableDataSource<double>(rates);
      yData.SetYMapping(y => y);
      compositeDataSource = xData.Join(yData);
      return compositeDataSource;
    }
    public static CompositeDataSource GetInterpolatedRates(TermStructureInterpolatorByDate termStrucureInterpolatorByDate,DateTime analysisDate)
    {
      if(null==termStrucureInterpolatorByDate)return null;
//      if(null==compositeDataSource)compositeDataSource=new CompositeDataSource();
      TermStructureInterpolator termStructureInterpolator = termStrucureInterpolatorByDate[analysisDate];
      CompositeDataSource compositeDataSource=new CompositeDataSource();
      DateGenerator dateGenerator=new DateGenerator();
      List<DateTime> dates = new List<DateTime>();
      List<double> rates = new List<double>();

      analysisDate=analysisDate.Date;
      List<int> keys = new List<int>(termStructureInterpolator.SplineRates.Keys);
      keys.Sort();
      foreach (int key in keys)
      {
        dates.Add(analysisDate);
        rates.Add(termStructureInterpolator.SplineRates[key]);
        analysisDate=dateGenerator.GetNextDay(analysisDate);
      }
      double[] ratesArray = rates.ToArray<double>();
      var xData = new EnumerableDataSource<DateTime>(dates);
      xData.SetXMapping(x => (x.Ticks / 10000000000.0));
      var yData = new EnumerableDataSource<double>(rates);
      yData.SetYMapping(y => y);
//      compositeDataSource = xData.Join(yData);
      compositeDataSource.AddDataPart(xData.Join(yData));
      return compositeDataSource;
    }
  }
}
