using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarketData.Generator.TermStructure
{
  public class CurveData
  {
    public CurveData() { ;}
    public String CurveName{get;set;}
    public DateTime PricingDate { get; set; }
    public Tenors CurveTenors { get; set; }
    public String GetTenorValue(String tenorName)
    {
      try
      {
        int index=CurveTenors.FindIndex(x => x.Item.Equals(tenorName));
        if (-1 == index || index >= CurveTenors.Count) return null;
        return CurveTenors[index].Value;
      }
      catch (Exception /*exception*/)
      {
        return null;
      }
    }
    public void RemoveTenors(String strTenors)
    {
      string[] strTenorArray = strTenors.Split(',');
      foreach (String strTenor in strTenorArray)
      {
        CurveTenors.Remove(strTenor);
      }
    }
  }
// *************************************************************8
  public class CurveMappings : List<CurveMapping>
  {
    public CurveMappings()
    {
    }
  }
  public class CurveMapping
  {
    public CurveMapping()
    {
    }
    public String CurveName { get; set; }
    public String Currency { get; set; }
    public String BloombergMnemonic { get; set; }
    public String Field { get; set; }
    public String AttributeName { get; set; }
    public String CurveId { get; set; }
    public Tenors TenorMappings{get;set;}
    public DateTime PricingDate { get; set; }
    public void SetTenorValues(String strValues)
    {
      String[] strTenorValues = strValues.Split(',');
      if (strTenorValues.Length != TenorMappings.Count) throw new Exception("Tenor mapping count does not match tenor value count");
      for (int index = 0; index < TenorMappings.Count; index++)
      {
        TenorMappings[index].Value = strTenorValues[index];
      }
    }
  }
  public class Tenors : List<Tenor>
  {
    public Tenors()
    {
    }
    public void Remove(String strTenor)
    {
      for(int index=0;index<Count;index++)
      {
        if (this[index].Item.Equals(strTenor))
        {
          RemoveAt(index);
          return;
        }
      }
    }
    public static Tenors FromString(String strTenors)
    {
      Tenors tenors = new Tenors();
      String[] strTenorsArray = strTenors.Split(',');
      for (int index = 0; index < strTenorsArray.Length; index++)
      {
        tenors.Add(new Tenor(strTenorsArray[index]));
      }
      return tenors;
    }
    public String GetTenorValues()
    {
      StringBuilder sb = new StringBuilder();
      for (int index = 0; index < this.Count;index++)
      {
        String value = this[index].Value;
        if(null!=value)sb.Append(value);
        if (index < this.Count - 1) sb.Append(",");
      }
      return sb.ToString();
    }
  }
  public class Tenor
  {
    public Tenor()
    {
    }
    public Tenor(String tenor, String value)
    {
      Item = tenor;
      Value = value;
    }
    public Tenor(String tenor)
    {
      Item = tenor;
    }
    public String Item { get; set; }
    public String Value { get; set; }
    public double ValueNum{get{return double.Parse(Value);}}
    public int Days
    {
      get
      {
        return GetTenorDays(this.Item);
      }
    }
    public static int GetTenorDays(String strTenor)
    {
      int multiplier = 1;
      strTenor=strTenor.ToUpper();
      if (strTenor.EndsWith("D")) multiplier = 1;
      else if(strTenor.EndsWith("W")) multiplier = 7;
      else if(strTenor.EndsWith("M")) multiplier = 30;
      else if(strTenor.EndsWith("Y")) multiplier = 360;
      strTenor = strTenor.Substring(0, strTenor.Length - 1);
      return Int32.Parse(strTenor)*multiplier;
    }
  }
}
