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

/// <summary>
/// This StopLimit class is used by models and the UI
/// </summary>

namespace MarketData.Generator.Model
{
  public class StopLimits:List<StopLimit>
  {
    public StopLimits()
    {
    }
    public StopLimits(List<StopLimit> stopLimits)
    {
      foreach(StopLimit stopLimit in stopLimits)Add(stopLimit);
    }
    public NVPCollections ToNVPCollections()
    {
      NVPCollections nvpCollections=new NVPCollections();
      foreach(StopLimit stopLimit in this)
      {
        nvpCollections.Add(stopLimit.ToNVPCollection());
      }
      return nvpCollections;
    }
    public static StopLimits FromNVPCollections(NVPCollections nvpCollections)
    {
      StopLimits stopLimits=new StopLimits();
      foreach(NVPCollection nvpCollection in nvpCollections)
      {
        stopLimits.Add(StopLimit.FromNVPCollection(nvpCollection));
      }
      return stopLimits;
    }
    public void AddFromNVPCollection(NVPCollection nvpCollection)
    {
      Add(StopLimit.FromNVPCollection(nvpCollection));
    }
  }
  public class StopLimit
  {
    public StopLimit()
    {
    }
    public StopLimit(StopLimit stopLimit)
    {
      this.Symbol=stopLimit.Symbol;
      this.AnalysisDate=stopLimit.AnalysisDate;
      this.PreviousStop=stopLimit.PreviousStop;
      this.NewStop=stopLimit.NewStop;
      this.CurrentPriceLow=stopLimit.CurrentPriceLow;
      this.CurrentPriceClose=stopLimit.CurrentPriceClose;
      this.PriceTrendIndicatorSlope=stopLimit.PriceTrendIndicatorSlope;
      this.StopLimitId=stopLimit.StopLimitId;
    }
    public StopLimit(String symbol,DateTime analysisDate,double previousStop,double newStop,double currentPriceLow,double currentPriceClose,double priceTrendIndicatorSlope)
    {
      this.Symbol=symbol;
      this.AnalysisDate=analysisDate;
      this.PreviousStop=previousStop;
      this.NewStop=newStop;
      this.CurrentPriceLow=currentPriceLow;
      this.CurrentPriceClose=currentPriceClose;
      this.PriceTrendIndicatorSlope=priceTrendIndicatorSlope;
    }
    public String Symbol { get; set; }
    public DateTime AnalysisDate{get;set;}
    public double PreviousStop{get;set;}
    public double NewStop{get;set;}
    public double CurrentPriceLow{get;set;}
    public double CurrentPriceClose{get;set;}
    public double PriceTrendIndicatorSlope{get;set;}
    public String StopLimitId {get;set;}
    public static String Header()
    {
      StringBuilder sb=new StringBuilder();
      sb.Append("Id,Symbol,AnalysisDate,PreviousStop,NewStop,CurrentPriceLow,CurrentPriceClose,PriceTrendIndicatorSlope");
      return sb.ToString(); 
    }
    public override String ToString()
    {
      StringBuilder sb=new StringBuilder();
      sb.Append(null==StopLimitId?"":StopLimitId).Append(",");
      sb.Append(Symbol).Append(",");
      sb.Append(AnalysisDate.ToShortDateString()).Append(",");
      sb.Append(Utility.FormatCurrency(PreviousStop)).Append(",");
      sb.Append(Utility.FormatCurrency(NewStop)).Append(",");
      sb.Append(Utility.FormatCurrency(CurrentPriceLow)).Append(",");
      sb.Append(Utility.FormatCurrency(CurrentPriceClose)).Append(",");
      sb.Append(Utility.FormatNumber(PriceTrendIndicatorSlope,4,false));
      return sb.ToString();
    }
    public virtual NVPCollection ToNVPCollection()
    {
      NVPCollection nvpCollection=new NVPCollection();
      nvpCollection.Add(new NVP("Symbol",Symbol.ToString()));
      nvpCollection.Add(new NVP("AnalysisDate",AnalysisDate.ToString()));
      nvpCollection.Add(new NVP("PreviousStop",PreviousStop.ToString()));
      nvpCollection.Add(new NVP("NewStop",NewStop.ToString()));
      nvpCollection.Add(new NVP("CurrentPriceLow",CurrentPriceLow.ToString()));
      nvpCollection.Add(new NVP("CurrentPriceClose",CurrentPriceClose.ToString()));
      nvpCollection.Add(new NVP("PriceTrendIndicatorSlope",PriceTrendIndicatorSlope.ToString()));
      nvpCollection.Add(new NVP("StopLimitId",StopLimitId==null?"":StopLimitId));
      return nvpCollection;
    }
    public static StopLimit FromNVPCollection(NVPCollection nvpCollection)
    {
      StopLimit stopLimit=new StopLimit();

      NVPDictionary nvpDictionary=nvpCollection.ToDictionary();
      stopLimit.Symbol=nvpDictionary["Symbol"].Get<String>();
      stopLimit.AnalysisDate=nvpDictionary["AnalysisDate"].Get<DateTime>();
      stopLimit.PreviousStop=nvpDictionary["PreviousStop"].Get<double>();
      stopLimit.NewStop=nvpDictionary["NewStop"].Get<double>();
      stopLimit.CurrentPriceLow=nvpDictionary["CurrentPriceLow"].Get<double>();
      stopLimit.CurrentPriceClose=nvpDictionary["CurrentPriceClose"].Get<double>();
      stopLimit.PriceTrendIndicatorSlope=nvpDictionary["PriceTrendIndicatorSlope"].Get<double>();
      if(nvpDictionary.ContainsKey("StopLimitId"))stopLimit.StopLimitId=nvpDictionary["StopLimitId"].Get<String>();
      return stopLimit;
    }
  }
}
