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

namespace PortfolioManager.Renderers
{
  public class TextMarkerManager : List<MarkerItem>
  {
    private DateGenerator dateGenerator = new DateGenerator();
    private const double DATE_SPREAD_PCNT = 10.0;    // PERCENT
    private const double PRICE_SPREAD_PCNT = 10.0;   // PERCENT
    
    public Coordinates GetBestMarkerLocation(double markerDate, double markerPrice,OffsetDictionary offsetDictionary,double verticalAdjustmentFactor)
    {
      if(!IsOverlapped(markerDate,markerPrice,offsetDictionary))
      {
        Add(new MarkerItem(markerDate, markerPrice));
        return new Coordinates(){X=markerDate,Y=markerPrice};
      }
      Add(new MarkerItem(markerDate, markerPrice-verticalAdjustmentFactor));
      return new Coordinates(){X=markerDate,Y=markerPrice-verticalAdjustmentFactor};
    }
    
    private bool IsOverlapped(double markerDate, double markerPrice,OffsetDictionary offsetDictionary)
    {
      foreach(MarkerItem markerItem in this)
      {
        double markerItemDate = markerItem.MarkerDate;
        double markerItemPrice = markerItem.MarkerPrice;
        double minDate = markerItemDate - offsetDictionary.HorizontalSpread*(DATE_SPREAD_PCNT/100.0);
        double maxDate = markerItemDate + offsetDictionary.HorizontalSpread*(DATE_SPREAD_PCNT/100.0);
        double minPrice = markerItemPrice - offsetDictionary.VerticalSpread*(PRICE_SPREAD_PCNT/100.0);  
        double maxPrice = markerItemPrice + offsetDictionary.VerticalSpread*(PRICE_SPREAD_PCNT/100.0);            
          
        if(markerDate>=minDate && markerDate<=maxDate && markerPrice >=minPrice && markerPrice<=maxPrice)
        {
          return true;
        }
      }
      return false;
    }
  }
}
