using System;
using System.Collections.Generic;
//using MarketData.DataAccess;

namespace MarketData.MarketDataModel
{
  public class Portfolio
  {
    private List<ModelTrade> trades = new List<ModelTrade>();
    private bool openPosition = false;
    private double availableCash;
    private double initialCash;

    public Portfolio(double initialCash)
    {
      this.initialCash = this.availableCash = initialCash;
    }
    public List<ModelTrade> Trades
    {
      get { return trades; }
      set { trades = value; }
    }
    public double AvailableCash
    {
      get { return availableCash; }
      set { availableCash = value; }
    }
    public double GetPortfolioReturn(Price priceOpenPosition)
    {
      return (GetPortfolioValue(priceOpenPosition)-initialCash)/initialCash;
    }
  // pass in latest price so we can price any open position
    public double GetPortfolioValue(Price priceOpenPosition)
    {
        double cashValue = AvailableCash;
        if (0 != trades.Count)
        {
          ModelTrade lastTrade = trades[trades.Count - 1];
          if (lastTrade.Type.Equals(ModelTrade.TradeType.Buy)) cashValue += (lastTrade.Shares * priceOpenPosition.Close);
        }
        return cashValue;
    }
    public void Add(ModelTrade trade)
    {
      trades.Add(trade);
    }
    public ModelTrade GetLastTrade()
    {
      return trades[trades.Count - 1];
    }
    public bool HasOpenPosition
    {
      get { return openPosition; }
      set { openPosition = value; }
    }
    public int GetTradeCount()
    {
      return trades.Count;
    }
    public double GetAverageGainLoss()
    {
      double totalGainLoss = 0;
      for (int index = 0; index < trades.Count; index++)
      {
        totalGainLoss += trades[index].GainLoss;
      }
      return totalGainLoss / trades.Count;
    }
    public int GetAverageHoldingDays()
    {
      int totalHoldingDays = 0;
      int numberOfSellTrades = 0;
      for (int index = 0; index < trades.Count; index++)
      {
        ModelTrade trade = trades[index];
        if (trade.Type == ModelTrade.TradeType.Sell)
        {
          totalHoldingDays += trades[index].DaysHeld;
          numberOfSellTrades++;
        }
      }
      return (numberOfSellTrades>0?totalHoldingDays / numberOfSellTrades:0);
    }
    public int GetMinHoldingDays()
    {
      int minHoldingDays = Int16.MaxValue;
      for (int index = 0; index < trades.Count; index++)
      {
        ModelTrade trade = trades[index];
        int holdingDays = trade.DaysHeld;
        if (trade.Type == ModelTrade.TradeType.Sell && holdingDays < minHoldingDays) minHoldingDays = holdingDays;
      }
      return minHoldingDays;
    }
    public int GetMaxHoldingDays()
    {
      int maxHoldingDays =0;
      for (int index = 0; index < trades.Count; index++)
      {
        ModelTrade trade = trades[index];
        int holdingDays = trade.DaysHeld;
        if (trade.Type==ModelTrade.TradeType.Sell && holdingDays > maxHoldingDays) maxHoldingDays = holdingDays;
      }
      return maxHoldingDays;
    }
  }
}
