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

namespace MarketData.MarketDataModel
{
  public class Signals : List<Signal>
  {
    public Signals()
    {
    }
    public Signals(List<Signal> signals)
    {
      foreach(Signal signal in signals)Add(signal);
    }
    public Signals WeakBuySignals
    {
      get 
      {
        IEnumerable<Signal> list=(from Signal signal in this where signal.SignalIndicator.Equals(Signal.Indicator.WeakBuy) select signal);
        if(0==list.Count())return new Signals();
        return new Signals(list.ToList());
      }
    }
    public Signals WeakSellSignals
    {
      get 
      {
        IEnumerable<Signal> list=(from Signal signal in this where signal.SignalIndicator.Equals(Signal.Indicator.WeakSell) select signal);
        if(0==list.Count())return new Signals();
        return new Signals(list.ToList());
      }
    }
    public Signals StrongBuySignals
    {
      get 
      {
        IEnumerable<Signal> list=(from Signal signal in this where signal.SignalIndicator.Equals(Signal.Indicator.StrongBuy) select signal);
        if(0==list.Count())return new Signals();
        return new Signals(list.ToList());
      }
    }
    public Signals StrongSellSignals
    {
      get 
      {
        IEnumerable<Signal> list=(from Signal signal in this where signal.SignalIndicator.Equals(Signal.Indicator.StrongSell) select signal);
        if(0==list.Count())return new Signals();
        return new Signals(list.ToList());
      } 
    }
    public Signals CondenseSignals()
    {
      Signal currentSignal=null;
      Signals condensedSignals=new Signals();
      for(int index=0;index<Count;index++)
      {
        Signal signal=this[index];
        if(0==index)
        {
          currentSignal=signal;
          condensedSignals.Add(signal);
        }
        else
        {
          if(signal.SignalIndicator.Equals(currentSignal.SignalIndicator))continue;
          condensedSignals.Add(signal);
          currentSignal=signal;
        }
      }
      return condensedSignals;
    }
  }
// *****************************************************************************************************************************************************************
  public class SignalComparatorByDateDescending:IComparer<Signal>
  {
    public int Compare(Signal v1,Signal v2)
    {
      return v2.SignalDate.CompareTo(v1.SignalDate);
    }
  }
// *****************************************************************************************************************************************************************
  public class Signal
  {
    public enum Indicator { StrongBuy, WeakBuy, WeakSell, StrongSell,Neutral };
    private String ticker;
    private Indicator indicator;
    private DateTime signalDate;
    private String reason;

    public Signal()
    {
    }
    public Signal(String ticker, DateTime signalDate, Indicator indicator)
    {
      this.ticker = ticker;
      this.signalDate = signalDate;
      this.indicator = indicator;
    }
    public String Ticker
    {
      get { return ticker; }
      set { ticker = value; }
    }
    public Signal.Indicator SignalIndicator
    {
      get { return indicator; }
      set { indicator = value; }
    }
    public String SignalIndicatorString
    {
      get
      {
        if (indicator == Indicator.StrongBuy) return "SB";
        else if (indicator == Indicator.WeakBuy) return "WB";
        else if (indicator == Indicator.WeakSell) return "WS";
        else if (indicator == Indicator.StrongSell) return "SS";
        else if (indicator == Indicator.Neutral) return "NE";
        else return "??";
      }
    }
    public DateTime SignalDate
    {
      get { return signalDate; }
      set { signalDate = value; }
    }
    public bool IsWeakBuy()
    {
      return Indicator.WeakBuy == indicator;
    }
    public bool IsWeakSell()
    {
      return Indicator.WeakSell == indicator;
    }
    public bool IsStrongBuy()
    {
      return Indicator.StrongBuy == indicator;
    }
    public bool IsStrongSell()
    {
      return Indicator.StrongSell == indicator;
    }
    public bool IsSell()
    {
      if (IsStrongSell() || IsWeakSell()) return true;
      return false;
    }
    public bool IsBuy()
    {
      if (IsStrongBuy() || IsWeakBuy()) return true;
      return false;
    }
    public bool IsNeutral()
    {
      return Indicator.Neutral == indicator;
    }
    public String Reason
    {
      get { return reason; }
      set { reason = value; }
    }
    public override String ToString()
    {
      StringBuilder sb = new StringBuilder();
      sb.Append(ticker).Append(",").Append(Utility.DateTimeToStringMMSDDSYYYY(signalDate)).Append(",").Append(SignalIndicatorString);
      return sb.ToString();
    }
  }
}
