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

namespace MarketData.DividendRiskParity
{
  public class DividendRiskParityPosition
  {
    public enum PositionType { Position, Proforma };
    public String Symbol { get; set; }
    public DateTime TradeDate { get; set; }
    public double PurchasePrice { get; set; }
    public double Shares { get; set; }
    public double Exposure { get; set; }
    public double WeightedDividendPercent { get; set; }
    public double DividendPayment { get; set; }
    public double Weight { get; set; }
    public double GainLoss { get; set; }
    public double GainLossPercent{get;set;}
    public double CurrentPrice{get;set;}
    public PositionType TypeOfPosition { get; set; }
    public String TypeOfPositionAsString
    { 
      get 
      {
        switch (TypeOfPosition)
        {
          case PositionType.Position :
            return "Position";
          default :
            return "Proforma";
        }
      } 
    }
    public override String ToString()
    {
      StringBuilder sb = new StringBuilder();
      sb.Append(Symbol).Append("|");
      sb.Append(TradeDate.ToShortDateString()).Append("|");
      sb.Append(PurchasePrice).Append("|");
      sb.Append(Shares).Append("|");
      sb.Append(Exposure).Append("|");
      sb.Append(WeightedDividendPercent).Append("|");
      sb.Append(DividendPayment).Append("|");
      sb.Append(Weight).Append("|");
      sb.Append(GainLoss).Append("|");
      sb.Append(GainLossPercent).Append("|");
      sb.Append(CurrentPrice).Append("|");
      sb.Append(TypeOfPositionAsString);
      return sb.ToString();
    }
    public static DividendRiskParityPosition FromString(String strString)
    {
      DividendRiskParityPosition dividendRiskParityPosition = new DividendRiskParityPosition();
      string[] items = strString.Split('|');
      if (10 != items.Length) return null;
      dividendRiskParityPosition.Symbol = items[0];
      dividendRiskParityPosition.TradeDate = DateTime.Parse(items[1]);
      dividendRiskParityPosition.PurchasePrice = double.Parse(items[2]);
      dividendRiskParityPosition.Shares = double.Parse(items[3]);
      dividendRiskParityPosition.Exposure = double.Parse(items[4]);
      dividendRiskParityPosition.WeightedDividendPercent = double.Parse(items[5]);
      dividendRiskParityPosition.DividendPayment = double.Parse(items[6]);
      dividendRiskParityPosition.Weight = double.Parse(items[7]);
      dividendRiskParityPosition.GainLoss = double.Parse(items[8]);
      dividendRiskParityPosition.GainLossPercent = double.Parse(items[9]);
      dividendRiskParityPosition.PurchasePrice = double.Parse(items[10]);
      if ("Position".Equals(items[11])) dividendRiskParityPosition.TypeOfPosition = PositionType.Position;
      else dividendRiskParityPosition.TypeOfPosition = PositionType.Proforma; 
      return dividendRiskParityPosition; 
    }
  }
  public class DividendRiskParityPositionCollection : List<DividendRiskParityPosition>
  {
    public DividendRiskParityPositionCollection()
    {
    }
    public DividendRiskParityPositionCollection(List<DividendRiskParityPosition> dividendRiskParityPositions)
    {
      foreach (DividendRiskParityPosition dividendRiskParityPosition in dividendRiskParityPositions) Add(dividendRiskParityPosition);
    }
    public DividendRiskParityPositionCollection(PortfolioTrades portfolioTrades)
    {
      for (int index = 0; index < portfolioTrades.Count; index++)
      {
        PortfolioTrade portfolioTrade = portfolioTrades[index];
        DividendRiskParityPosition dividendRiskParityPosition = new DividendRiskParityPosition
        {
          TypeOfPosition = DividendRiskParityPosition.PositionType.Position,
          Symbol = portfolioTrade.Symbol,
          TradeDate = portfolioTrade.TradeDate,
          PurchasePrice = portfolioTrade.Price,
          Shares = portfolioTrade.Shares,
          Exposure = portfolioTrade.Exposure()
        };
        Add(dividendRiskParityPosition);
      }
    }
  }
}
