using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using MarketData;
using MarketData.Utils;
using MarketData.MarketDataModel;
using MarketData.Generator;
using MarketData.DataAccess;
using TradeBlotter.DataAccess;
using TradeBlotter.Command;
using TradeBlotter.Model;
using Microsoft.Research.DynamicDataDisplay.DataSources;
using MarketData.Generator.TermStructure;
using Microsoft.Research.DynamicDataDisplay.PointMarkers;
using System.Threading;
using MarketData.Numerical;

namespace TradeBlotter.ViewModels
{
  public class YieldCurveViewModel : WorkspaceViewModel
  {
    private enum Tasks { SelectedItem};
    private Dictionary<Tasks, Semaphore> semaphorePool = new Dictionary<Tasks, Semaphore>();
    private List<Int32> dayCounts;
    private List<String> curves;
    private Int32 selectedDayCount;
    private String selectedCurve;
    private YieldCurve yieldCurve;
    private DMAValues yieldCurveMA5;
    private DMAValues yieldCurveMA21;
    private DMAValues yieldCurveMA55;

    private TermStructureInterpolatorByDate termStrucureInterpolatorByDate;
    private DateTime selectedTermStructureDate;
    private List<DateTime> termStructureDates;

    private CurveData termStructureTenorCurveData;
    private CompositeDataSource termStructureTenorPointsDataSource;
    private CompositeDataSource termStructureDataSource;

    private CurveData termStructureTenorCurveData90;
    private CompositeDataSource termStructureTenorPointsDataSource90;
    private CompositeDataSource termStructureDataSource90;

    private CurveData termStructureTenorCurveData180;
    private CompositeDataSource termStructureTenorPointsDataSource180;
    private CompositeDataSource termStructureDataSource180;

    private CompositeDataSource yieldCurveCompositeDataSource;
    private CompositeDataSource ma5CompositeDataSource;
    private CompositeDataSource ma21CompositeDataSource;
    private CompositeDataSource ma55CompositeDataSource;

    private double dataSlope=double.NaN;     // This is the slope of whatever data is presently being displayed.  If TermStructure then it respresents the slope of the most recent curve

    private bool isLegendVisible = true;
    private bool busyIndicator = false;

    public YieldCurveViewModel()
    {
      semaphorePool.Add(Tasks.SelectedItem, new Semaphore(1, 1));
      base.DisplayName = "YieldCurveView";
      curves = new List<String>();
      curves.Add("1-Month");
      curves.Add("3-Month");
      curves.Add("6-Month");
      curves.Add("1-Year");
      curves.Add("2-Year");
      curves.Add("3-Year");
      curves.Add("5-Year");
      curves.Add("7-Year");
      curves.Add("10-Year");
      curves.Add("20-Year");
      curves.Add("30-Year");
      curves.Add("Term Structure");
      dayCounts = new List<Int32>();
      dayCounts.Add(60);
      dayCounts.Add(90);
      dayCounts.Add(180);
      dayCounts.Add(360);
      dayCounts.Add(720);
      dayCounts.Add(1440);
      dayCounts.Add(3600);
      termStructureDates=YieldCurveDA.GetYieldCurveDates();
      PropertyChanged += OnYieldCurveViewModelPropertyChanged;
      selectedDayCount = dayCounts[1];
      selectedCurve = curves[curves.Count-1];
      base.OnPropertyChanged("SelectedDayCount");
      SelectedTermStructureDate=termStructureDates[0];
    }
    public override SaveParameters GetSaveParameters()
    {
      return null;
    }
    public override void SetSaveParameters(SaveParameters saveParameters)
    {
    }
    public override bool CanPersist()
    {
      return false;
    }
    public bool BusyIndicator
    {
      get { return busyIndicator; }
      set { busyIndicator = value; base.OnPropertyChanged("BusyIndicator"); }
    }
    private void OnYieldCurveViewModelPropertyChanged(object sender, PropertyChangedEventArgs eventArgs)
    {
      if (eventArgs.PropertyName.Equals("SelectedDayCount") || eventArgs.PropertyName.Equals("SelectedCurve")||eventArgs.PropertyName.Equals("SelectedTermStructureDate"))
      {
        BusyIndicator = true;
        Task workerTask = Task.Factory.StartNew(() =>
        {
          if (null == selectedCurve) return;
          semaphorePool[Tasks.SelectedItem].WaitOne();
          InitializeDataSource();
          UpdateProperties();
          yieldCurve = YieldCurveDA.GetYieldCurve(selectedDayCount * 2);
          if (selectedCurve.Equals("1-Month"))
          {
            termStrucureInterpolatorByDate=null;
            yieldCurveMA5 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Mo1, 5);
            yieldCurveMA21 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Mo1, 21);
            yieldCurveMA55 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Mo1, 55);

            yieldCurveCompositeDataSource=YieldCurveModel.Value(yieldCurveMA5);
            ma5CompositeDataSource=YieldCurveModel.MA(yieldCurveMA5);
            ma21CompositeDataSource=YieldCurveModel.MA(yieldCurveMA21);
            ma55CompositeDataSource=YieldCurveModel.MA(yieldCurveMA55);;

            double[] values=yieldCurve.GetYieldCurveDataOrderByDateAscending(YieldCurve.CurveType.Mo1);
            CalculateSlope(values);
          }
          else if (selectedCurve.Equals("3-Month"))
          {
            termStrucureInterpolatorByDate=null;
            yieldCurveMA5 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Mo3, 5);
            yieldCurveMA21 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Mo3, 21);
            yieldCurveMA55 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Mo3, 55);

            yieldCurveCompositeDataSource=YieldCurveModel.Value(yieldCurveMA5);
            ma5CompositeDataSource=YieldCurveModel.MA(yieldCurveMA5);
            ma21CompositeDataSource=YieldCurveModel.MA(yieldCurveMA21);
            ma55CompositeDataSource=YieldCurveModel.MA(yieldCurveMA55);;
            
            double[] values=yieldCurve.GetYieldCurveDataOrderByDateAscending(YieldCurve.CurveType.Mo3);
            CalculateSlope(values);
          }
          else if (selectedCurve.Equals("6-Month"))
          {
            termStrucureInterpolatorByDate=null;
            yieldCurveMA5 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Mo6, 5);
            yieldCurveMA21 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Mo6, 21);
            yieldCurveMA55 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Mo6, 55);

            yieldCurveCompositeDataSource=YieldCurveModel.Value(yieldCurveMA5);
            ma5CompositeDataSource=YieldCurveModel.MA(yieldCurveMA5);
            ma21CompositeDataSource=YieldCurveModel.MA(yieldCurveMA21);
            ma55CompositeDataSource=YieldCurveModel.MA(yieldCurveMA55);;
            
            double[] values=yieldCurve.GetYieldCurveDataOrderByDateAscending(YieldCurve.CurveType.Mo6);
            CalculateSlope(values);
          }
          else if (selectedCurve.Equals("1-Year"))
          {
            termStrucureInterpolatorByDate=null;
            yieldCurveMA5 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Yr1, 5);
            yieldCurveMA21 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Yr1, 21);
            yieldCurveMA55 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Yr1, 55);

            yieldCurveCompositeDataSource=YieldCurveModel.Value(yieldCurveMA5);
            ma5CompositeDataSource=YieldCurveModel.MA(yieldCurveMA5);
            ma21CompositeDataSource=YieldCurveModel.MA(yieldCurveMA21);
            ma55CompositeDataSource=YieldCurveModel.MA(yieldCurveMA55);;
            
            double[] values=yieldCurve.GetYieldCurveDataOrderByDateAscending(YieldCurve.CurveType.Yr1);
            CalculateSlope(values);
          }
          else if (selectedCurve.Equals("2-Year"))
          {
            termStrucureInterpolatorByDate=null;
            yieldCurveMA5 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Yr2, 5);
            yieldCurveMA21 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Yr2, 21);
            yieldCurveMA55 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Yr2, 55);

            yieldCurveCompositeDataSource=YieldCurveModel.Value(yieldCurveMA5);
            ma5CompositeDataSource=YieldCurveModel.MA(yieldCurveMA5);
            ma21CompositeDataSource=YieldCurveModel.MA(yieldCurveMA21);
            ma55CompositeDataSource=YieldCurveModel.MA(yieldCurveMA55);;
            
            double[] values=yieldCurve.GetYieldCurveDataOrderByDateAscending(YieldCurve.CurveType.Yr2);
            CalculateSlope(values);
          }
          else if (selectedCurve.Equals("3-Year"))
          {
            termStrucureInterpolatorByDate=null;
            yieldCurveMA5 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Yr3, 5);
            yieldCurveMA21 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Yr3, 21);
            yieldCurveMA55 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Yr3, 55);

            yieldCurveCompositeDataSource=YieldCurveModel.Value(yieldCurveMA5);
            ma5CompositeDataSource=YieldCurveModel.MA(yieldCurveMA5);
            ma21CompositeDataSource=YieldCurveModel.MA(yieldCurveMA21);
            ma55CompositeDataSource=YieldCurveModel.MA(yieldCurveMA55);;
            
            double[] values=yieldCurve.GetYieldCurveDataOrderByDateAscending(YieldCurve.CurveType.Yr3);
            CalculateSlope(values);
          }
          else if (selectedCurve.Equals("5-Year"))
          {
            termStrucureInterpolatorByDate=null;
            yieldCurveMA5 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Yr5, 5);
            yieldCurveMA21 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Yr5, 21);
            yieldCurveMA55 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Yr5, 55);

            yieldCurveCompositeDataSource=YieldCurveModel.Value(yieldCurveMA5);
            ma5CompositeDataSource=YieldCurveModel.MA(yieldCurveMA5);
            ma21CompositeDataSource=YieldCurveModel.MA(yieldCurveMA21);
            ma55CompositeDataSource=YieldCurveModel.MA(yieldCurveMA55);;
            
            double[] values=yieldCurve.GetYieldCurveDataOrderByDateAscending(YieldCurve.CurveType.Yr5);
            CalculateSlope(values);
          }
          else if (selectedCurve.Equals("7-Year"))
          {
            termStrucureInterpolatorByDate=null;
            yieldCurveMA5 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Yr7, 5);
            yieldCurveMA21 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Yr7, 21);
            yieldCurveMA55 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Yr7, 55);

            yieldCurveCompositeDataSource=YieldCurveModel.Value(yieldCurveMA5);
            ma5CompositeDataSource=YieldCurveModel.MA(yieldCurveMA5);
            ma21CompositeDataSource=YieldCurveModel.MA(yieldCurveMA21);
            ma55CompositeDataSource=YieldCurveModel.MA(yieldCurveMA55);;
            
            double[] values=yieldCurve.GetYieldCurveDataOrderByDateAscending(YieldCurve.CurveType.Yr7);
            CalculateSlope(values);
          }
          else if (selectedCurve.Equals("10-Year"))
          {
            termStrucureInterpolatorByDate=null;
            yieldCurveMA5 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Yr10, 5);
            yieldCurveMA21 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Yr10, 21);
            yieldCurveMA55 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Yr10, 55);

            yieldCurveCompositeDataSource=YieldCurveModel.Value(yieldCurveMA5);
            ma5CompositeDataSource=YieldCurveModel.MA(yieldCurveMA5);
            ma21CompositeDataSource=YieldCurveModel.MA(yieldCurveMA21);
            ma55CompositeDataSource=YieldCurveModel.MA(yieldCurveMA55);;
            
            double[] values=yieldCurve.GetYieldCurveDataOrderByDateAscending(YieldCurve.CurveType.Yr10);
            CalculateSlope(values);
          }
          else if (selectedCurve.Equals("20-Year"))
          {
            termStrucureInterpolatorByDate=null;
            yieldCurveMA5 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Yr20, 5);
            yieldCurveMA21 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Yr20, 21);
            yieldCurveMA55 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Yr20, 55);

            yieldCurveCompositeDataSource=YieldCurveModel.Value(yieldCurveMA5);
            ma5CompositeDataSource=YieldCurveModel.MA(yieldCurveMA5);
            ma21CompositeDataSource=YieldCurveModel.MA(yieldCurveMA21);
            ma55CompositeDataSource=YieldCurveModel.MA(yieldCurveMA55);;
            
            double[] values=yieldCurve.GetYieldCurveDataOrderByDateAscending(YieldCurve.CurveType.Yr20);
            CalculateSlope(values);
          }
          else if (selectedCurve.Equals("30-Year"))
          {
            termStrucureInterpolatorByDate=null;
            yieldCurveMA5 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Yr30, 5);
            yieldCurveMA21 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Yr30, 21);
            yieldCurveMA55 = yieldCurve.GetMovingAverage(YieldCurve.CurveType.Yr30, 55);

            yieldCurveCompositeDataSource=YieldCurveModel.Value(yieldCurveMA5);
            ma5CompositeDataSource=YieldCurveModel.MA(yieldCurveMA5);
            ma21CompositeDataSource=YieldCurveModel.MA(yieldCurveMA21);
            ma55CompositeDataSource=YieldCurveModel.MA(yieldCurveMA55);

            double[] values=yieldCurve.GetYieldCurveDataOrderByDateAscending(YieldCurve.CurveType.Yr30);
            CalculateSlope(values);
          }
          else if(selectedCurve.Equals("Term Structure"))
          {
            termStrucureInterpolatorByDate=new TermStructureInterpolatorByDate();

            DateGenerator dateGenerator=new DateGenerator();
            DateTime termStructureDate90=dateGenerator.GenerateHistoricalDate(selectedTermStructureDate,90);
            DateTime termStructureDate180=dateGenerator.GenerateHistoricalDate(selectedTermStructureDate,180);

            termStrucureInterpolatorByDate.Add(selectedTermStructureDate,new TermStructureInterpolator(selectedTermStructureDate));
            termStrucureInterpolatorByDate.Add(termStructureDate90,new TermStructureInterpolator(termStructureDate90));
            termStrucureInterpolatorByDate.Add(termStructureDate180,new TermStructureInterpolator(termStructureDate180));

            termStructureTenorCurveData=TermStructureModel.GetTenorCurve(termStrucureInterpolatorByDate,selectedTermStructureDate);
            termStructureDataSource=TermStructureModel.GetInterpolatedRates(termStrucureInterpolatorByDate,selectedTermStructureDate);
            termStructureTenorPointsDataSource=TermStructureModel.GetTenorRates(termStrucureInterpolatorByDate,selectedTermStructureDate);

            termStructureTenorCurveData90=TermStructureModel.GetTenorCurve(termStrucureInterpolatorByDate,termStructureDate90);
            termStructureDataSource90=TermStructureModel.GetInterpolatedRates(termStrucureInterpolatorByDate,termStructureDate90);
            termStructureTenorPointsDataSource90=TermStructureModel.GetTenorRates(termStrucureInterpolatorByDate,termStructureDate90);

            termStructureTenorCurveData180=TermStructureModel.GetTenorCurve(termStrucureInterpolatorByDate,termStructureDate180);
            termStructureDataSource180=TermStructureModel.GetInterpolatedRates(termStrucureInterpolatorByDate,termStructureDate180);
            termStructureTenorPointsDataSource180=TermStructureModel.GetTenorRates(termStrucureInterpolatorByDate,termStructureDate180);

            double[] values=(from Tenor tenor in termStructureTenorCurveData.CurveTenors select tenor.ValueNum).ToArray();
            CalculateSlope(values);

            yieldCurve = null;
            yieldCurveMA5 = null;
            yieldCurveMA21 = null;
            yieldCurveMA55 = null;

          }
        });
        workerTask.ContinueWith((continuation) =>
        {
          BusyIndicator = false;
          UpdateProperties();
          semaphorePool[Tasks.SelectedItem].Release();
        });
      }
    }
    public void CalculateSlope(double[] observations)
    {
      if(observations==null)return;
      LeastSquaresResultWithR2 leastSquaresResult=LeastSquaresHelper.CalculateLeastSquaresWithR2(observations);
      dataSlope=leastSquaresResult.Slope;
    }
    public void InitializeDataSource()
    {
      termStrucureInterpolatorByDate=null;
      if(null!=yieldCurveCompositeDataSource)yieldCurveCompositeDataSource.Clear();
      
      if(null!=termStructureTenorPointsDataSource)termStructureTenorPointsDataSource.Clear();
      if(null!=termStructureDataSource)termStructureDataSource.Clear();

      if(null!=termStructureTenorPointsDataSource90)termStructureTenorPointsDataSource90.Clear();
      if(null!=termStructureDataSource90)termStructureDataSource90.Clear();

      if(null!=termStructureTenorPointsDataSource180)termStructureTenorPointsDataSource180.Clear();
      if(null!=termStructureDataSource180)termStructureDataSource180.Clear();

      if(null!=ma5CompositeDataSource)ma5CompositeDataSource.Clear();
      if(null!=ma21CompositeDataSource)ma21CompositeDataSource.Clear();
      if(null!=ma55CompositeDataSource)ma55CompositeDataSource.Clear();
   }
    private void UpdateProperties()
    {
      base.OnPropertyChanged("Values");                    
      base.OnPropertyChanged("MA5");                       
      base.OnPropertyChanged("MA21");                      
      base.OnPropertyChanged("MA55");                      

      base.OnPropertyChanged("TermStructure");              
      base.OnPropertyChanged("TermStructure90");            
      base.OnPropertyChanged("TermStructure180");           

      base.OnPropertyChanged("TermStructureTenors");        
      base.OnPropertyChanged("TermStructureTenors90");      
      base.OnPropertyChanged("TermStructureTenors180");     

      base.OnPropertyChanged("TermStructureTenorMarkers");    
      base.OnPropertyChanged("TermStructureTenorMarkers90");
      base.OnPropertyChanged("TermStructureTenorMarkers180");

      base.OnPropertyChanged("DayCountsEnabled");
      base.OnPropertyChanged("TermStructureDatesEnabled");

      base.OnPropertyChanged("DataSlope");
      base.OnPropertyChanged("DataSlopeDescription");

      base.OnPropertyChanged("Title");
    }
// **************************************************** C O M P O S I T E  D A T A  S O U R C E *************************************************
// **********************************************************************************************************************************************
    public CompositeDataSource Values
    {
      get
      {
        if (null == selectedCurve||selectedCurve.Equals("Term Structure")) return null;
        return yieldCurveCompositeDataSource;
      }
    }
    public CompositeDataSource MA5
    {
      get
      {
        if (null == selectedCurve||selectedCurve.Equals("Term Structure")) return null;
        return ma5CompositeDataSource;
      }
    }
    public CompositeDataSource MA21
    {
      get
      {
        if (null == selectedCurve||selectedCurve.Equals("Term Structure")) return null;
        return ma21CompositeDataSource;
      }
    }
    public CompositeDataSource MA55
    {
      get
      {
        if (null == selectedCurve||selectedCurve.Equals("Term Structure")) return null;
        return ma55CompositeDataSource;
//        return YieldCurveModel.MA(yieldCurveMA55);
      }
    }
// ****************************************************************************************************************************************************
// ****************************************************************** T E R M   S T R U C T U R E  ****************************************************
// ****************************************************************************************************************************************************
    public CompositeDataSource TermStructure
    {
      get
      {
        if(null==termStrucureInterpolatorByDate||null==termStructureDataSource||!selectedCurve.Equals("Term Structure"))return null;
        return termStructureDataSource;
      }
    }
    public CompositeDataSource TermStructureTenors
    {
      get
      {
        if(null==termStrucureInterpolatorByDate||null==termStructureTenorPointsDataSource||!selectedCurve.Equals("Term Structure"))return null;
        return termStructureTenorPointsDataSource;
      }
    }
    public CenteredTextMarker[] TermStructureTenorMarkers
    {
      get
      {
        if(null==termStrucureInterpolatorByDate||null==termStructureDataSource||!selectedCurve.Equals("Term Structure"))return null;
        List<CenteredTextMarker> centeredTextMarkers=new List<CenteredTextMarker>();
        if(null==termStructureTenorCurveData)return null;
        CenteredTextMarker centeredTextMarker=null;
        StringBuilder sb=null;

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData.GetTenorValue("1D")).Append("(").Append("1D").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData.GetTenorValue("1M")).Append("(").Append("1M").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData.GetTenorValue("3M")).Append("(").Append("3M").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData.GetTenorValue("6M")).Append("(").Append("6M").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData.GetTenorValue("1Y")).Append("(").Append("1Y").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData.GetTenorValue("2Y")).Append("(").Append("2Y").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData.GetTenorValue("3Y")).Append("(").Append("3Y").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData.GetTenorValue("5Y")).Append("(").Append("5Y").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData.GetTenorValue("7Y")).Append("(").Append("7Y").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData.GetTenorValue("10Y")).Append("(").Append("10Y").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData.GetTenorValue("20Y")).Append("(").Append("20Y").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData.GetTenorValue("30Y")).Append("(").Append("30Y").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarker.VerticalShift="45";
        centeredTextMarkers.Add(centeredTextMarker);

        return centeredTextMarkers.ToArray();
      }
    }
    public CompositeDataSource TermStructure90
    {
      get
      {
        if(null==termStrucureInterpolatorByDate||null==termStructureDataSource90||!selectedCurve.Equals("Term Structure"))return null;
        return termStructureDataSource90;
      }
    }
    public CompositeDataSource TermStructureTenors90
    {
      get
      {
        if(null==termStrucureInterpolatorByDate||null==termStructureTenorPointsDataSource90||!selectedCurve.Equals("Term Structure"))return null;
        return termStructureTenorPointsDataSource90;
      }
    }
    public CenteredTextMarker[] TermStructureTenorMarkers90
    {
      get
      {
        if(null==termStrucureInterpolatorByDate||null==termStructureDataSource90||!selectedCurve.Equals("Term Structure"))return null;
        List<CenteredTextMarker> centeredTextMarkers=new List<CenteredTextMarker>();
        if(null==termStructureTenorCurveData90)return null;
        CenteredTextMarker centeredTextMarker=null;
        StringBuilder sb=null;

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData90.GetTenorValue("1D")).Append("(").Append("1D").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData90.GetTenorValue("1M")).Append("(").Append("1M").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData90.GetTenorValue("3M")).Append("(").Append("3M").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData90.GetTenorValue("6M")).Append("(").Append("6M").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData90.GetTenorValue("1Y")).Append("(").Append("1Y").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData90.GetTenorValue("2Y")).Append("(").Append("2Y").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData90.GetTenorValue("3Y")).Append("(").Append("3Y").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData90.GetTenorValue("5Y")).Append("(").Append("5Y").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData90.GetTenorValue("7Y")).Append("(").Append("7Y").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData90.GetTenorValue("10Y")).Append("(").Append("10Y").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData90.GetTenorValue("20Y")).Append("(").Append("20Y").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData90.GetTenorValue("30Y")).Append("(").Append("30Y").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarker.VerticalShift="45";
        centeredTextMarkers.Add(centeredTextMarker);

        return centeredTextMarkers.ToArray();
      }
    }
    public CompositeDataSource TermStructure180
    {
      get
      {
        if(null==termStrucureInterpolatorByDate||null==termStructureDataSource180||!selectedCurve.Equals("Term Structure"))return null;
        return termStructureDataSource180;
      }
    }
    public CompositeDataSource TermStructureTenors180
    {
      get
      {
        if(null==termStrucureInterpolatorByDate||null==termStructureTenorPointsDataSource180||!selectedCurve.Equals("Term Structure"))return null;
        return termStructureTenorPointsDataSource180;
      }
    }
    public CenteredTextMarker[] TermStructureTenorMarkers180
    {
      get
      {
        if(null==termStrucureInterpolatorByDate||null==termStructureDataSource180||!selectedCurve.Equals("Term Structure"))return null;
        List<CenteredTextMarker> centeredTextMarkers=new List<CenteredTextMarker>();
        if(null==termStructureTenorCurveData180)return null;
        CenteredTextMarker centeredTextMarker=null;
        StringBuilder sb=null;

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData180.GetTenorValue("1D")).Append("(").Append("1D").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData180.GetTenorValue("1M")).Append("(").Append("1M").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData180.GetTenorValue("3M")).Append("(").Append("3M").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData180.GetTenorValue("6M")).Append("(").Append("6M").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData180.GetTenorValue("1Y")).Append("(").Append("1Y").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData180.GetTenorValue("2Y")).Append("(").Append("2Y").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData180.GetTenorValue("3Y")).Append("(").Append("3Y").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData180.GetTenorValue("5Y")).Append("(").Append("5Y").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData180.GetTenorValue("7Y")).Append("(").Append("7Y").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData180.GetTenorValue("10Y")).Append("(").Append("10Y").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData180.GetTenorValue("20Y")).Append("(").Append("20Y").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarkers.Add(centeredTextMarker);

        sb=new StringBuilder();
        centeredTextMarker=new CenteredTextMarker();
        sb.Append(termStructureTenorCurveData180.GetTenorValue("30Y")).Append("(").Append("30Y").Append(")");
        centeredTextMarker.Text=sb.ToString();
        centeredTextMarker.VerticalShift="45";
        centeredTextMarkers.Add(centeredTextMarker);

        return centeredTextMarkers.ToArray();
      }
    }
// *********************************************************************************************************************************************************
    public Boolean CheckBoxLegendVisible
    {
      get
      {
        return isLegendVisible;
      }
      set
      {
        isLegendVisible = value;
        base.OnPropertyChanged("CheckBoxLegendVisible");
        base.OnPropertyChanged("LegendVisible");
      }
    }
    public String LegendVisible
    {
      get
      {
        if (isLegendVisible) return "true";
        return "false";
      }
      set
      {
        isLegendVisible = Boolean.Parse(value);
        base.OnPropertyChanged("LegendVisible");
      }
    }
    public override String Title
    {
      get
      {
        if(selectedCurve.Equals("Term Structure"))
        {
          return "Treasury Yield Curve - Interest Rates "+"("+selectedCurve+") Curve Date ("+Utility.DateTimeToStringMMSDDSYYYY(selectedTermStructureDate)+")";
        }
        else
        {
          if(null==yieldCurve||0==yieldCurve.Count)return "";
          return "Treasury Yield Curve - Interest Rates "+"("+selectedCurve+") Thru "+Utility.DateTimeToStringMMSDDSYYYY(yieldCurve[0].Date);
        }
      }
    }
    public bool TermStructureDatesEnabled
    {
      get{return selectedCurve.Equals("Term Structure")?true:false;}
    }

    public Boolean DayCountsEnabled
    {
      get{return selectedCurve.Equals("Term Structure")?false:true;}
    }
    public DateTime SelectedTermStructureDate
    {
      get{return selectedTermStructureDate;}
      set{selectedTermStructureDate=value;base.OnPropertyChanged("SelectedTermStructureDate");}
    }
    public List<DateTime> TermStructureDates
    {
      get{return termStructureDates;}
    }
    public List<Int32> DayCounts
    {
      get { return dayCounts; }
    }
    public Int32 SelectedDayCount
    {
      get { return selectedDayCount; }
      set { selectedDayCount = value; base.OnPropertyChanged("SelectedDayCount"); }
    }
    public List<String> Curves
    {
      get { return curves; }
    }
    public String SelectedCurve
    {
      get { return selectedCurve; }
      set { selectedCurve = value; base.OnPropertyChanged("SelectedCurve"); }
    }
    public String DataSlope
    {
      get
      {
        if(dataSlope.Equals(double.NaN))return Utility.FormatNumber(0,3);
        return Utility.FormatNumber(dataSlope,3);
      }
    }
// ***********************************************************************************************************************************************************************************
// ********************************************************************************* T O O L T I P   C A L L O U T S  ****************************************************************
// ***********************************************************************************************************************************************************************************
    public String DataSlopeDescription
    {
      get
      {
        StringBuilder sb=new StringBuilder();
        if(selectedCurve.Equals("Term Structure"))
        {
          sb.Append("Term Structure: A positive slope implies the bond market expects the economy to do well,").Append("\n");
          sb.Append("and a negative slope implies the bond market expects the economy to do badly.").Append("\n");
          sb.Append("A negatively sloped – inverted – yield curve implies that investors expect interest").Append("\n");
          sb.Append(" rates to be lower in the future.");
        }
        else
        {
          sb.Append("Slope of selected data tenor.");
        }
        return sb.ToString();
      }
    }
  }
}
