using System;
using System.Collections.Generic;
using System.Text;
using MarketData.Utils;

namespace MarketData.MarketDataModel
{
  public class TradeResult : IComparable
  {
    public TradeResult()
    {
    }
    public int CompareTo(Object obj)
    {
      if (obj == null || !obj.GetType().IsInstanceOfType(this)) throw new Exception("Expected TradeResult"); ;
      TradeResult otherTradeResult = (TradeResult)obj;
      return otherTradeResult.TotalGainLoss.CompareTo(TotalGainLoss);
//      return TotalGainLoss.CompareTo(otherTradeResult.TotalGainLoss);
    }
    public double InitialCash { get; set; }
    public MACDSetup MACDSetup { get; set; }
    public double StopLossThreshold { get; set; }
    public int StopLossDays { get; set; }
    public String Symbol { get; set; }
    public String Company { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime HistoricalDate { get; set; }
    public double AvailableCash { get; set; }
    public double TotalPortfolioValue { get; set; }
    public double TotalGainLoss { get; set; }
    public double ROI { get; set; }
    public double AverageGainLoss { get; set; }
    public int AverageHoldingDays { get; set; }
    public int MinimumHoldingDays { get; set; }
    public int MaximumHoldingDays { get; set; }
    public List<ModelTrade> Trades { get; set; }
    public override string ToString()
    {
      StringBuilder sb = new StringBuilder();
      sb.Append("Company:'" + Company + "'").Append("\n");
      sb.Append("Using " + Utility.AddQuotes(MACDSetup.ToString())).Append("\n");
      sb.Append("Using StopLoss:" + (StopLossThreshold * 100) + "%").Append("\n");
      sb.Append("Using StopLossDays:" + StopLossDays).Append("\n");
      sb.Append("Using initial cash " + String.Format("{0:C}", InitialCash)).Append("\n");
      sb.Append("Latest date for '" + Symbol + "' is " + Utility.DateTimeToStringMMSDDSYYYY(StartDate)).Append("\n");
      sb.Append("Historical date is " + Utility.DateTimeToStringMMSDDSYYYY(HistoricalDate)).Append("\n");
      sb.Append("Available Cash (Cash Available For Trading)=" + Utility.AddQuotes(String.Format("{0:C}", AvailableCash))).Append("\n");
      sb.Append("Total Portfolio Value (Cash+Stock)=" + Utility.AddQuotes(String.Format("{0:C}", TotalPortfolioValue))).Append("\n");
      sb.Append("Total Gain/Loss=" + Utility.AddQuotes(String.Format("{0:C}", TotalGainLoss))).Append("\n");
      sb.Append("Return On Investment:" + String.Format("{0:P}", ROI)).Append("\n");
      sb.Append("").Append("\n");
      sb.Append("********* T R A D I N G   D E T A I L  *********").Append("\n");
      sb.Append(ModelTrade.Header).Append("\n");
      for (int index = 0; index < Trades.Count; index++)
      {
        sb.Append(Trades[index].ToString()).Append("\n");
      }
      return sb.ToString();
    }
    public static String GetShortHeader()
    {
      return "Symbol,Company,MACD,Initial Cash,Start Date,Historical Date,Total Gain/Loss,Trade Count,Min Holding Days,Max Holding Days";
    }
    public string ToShortString()
    {
      StringBuilder sb = new StringBuilder();
      sb.Append(Symbol).Append(",");
      sb.Append(Utility.AddQuotes(Company)).Append(",");
      sb.Append(Utility.AddQuotes(MACDSetup.ToString())).Append(",");
      sb.Append(Utility.AddQuotes(String.Format("{0:C}", InitialCash))).Append(",");
      sb.Append(Utility.DateTimeToStringMMHDDHYYYY(StartDate)).Append(",");
      sb.Append(Utility.DateTimeToStringMMHDDHYYYY(HistoricalDate)).Append(",");
      sb.Append(Utility.AddQuotes(String.Format("{0:C}", TotalGainLoss))).Append(",");
      sb.Append(Trades.Count).Append(",");
      sb.Append(MinimumHoldingDays).Append(",");
      sb.Append(MaximumHoldingDays);
      return sb.ToString();
    }
  }
}
