using System;
using System.Text;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
using MarketData.DataAccess;
using MarketData.Numerical;

namespace MarketData.MarketDataModel
{
  public class StickerPrice
  {
    private double marginOfSafety = double.NaN;
    private double marginOfSafety80Pcnt = double.NaN;
    private double lowPE = double.NaN;
    private double trailingPE = double.NaN;
    private double averageLowTrailing = double.NaN;
    private double currentStockEstimatePrice = double.NaN;
    private double priceEstimate10y = double.NaN;
    private double todaysPriceForRequiredReturn = double.NaN;
    private double averageBVPSGrowth = double.NaN;
    private ReturnItems bvpsItems = null;

//    public StickerPrice(String symbol, int growthYears = -1)
    public StickerPrice(String symbol,double bvpsGrowthOverride)
    {
      try
      {
        double requiredReturn = 15.00 / 100.00;
//        TimeSeriesCollection bookValuePerShareCollection = HistoricalDA.GetTimeSeries(symbol, TimeSeriesElement.ElementType.BVPS);
        Fundamental fundamental = FundamentalDA.GetFundamental(symbol);
  //      bvpsItems = new ReturnItems();
//        averageBVPSGrowth = Numerics.AverageReturnWithSpline(bookValuePerShareCollection,bvpsItems);
        averageBVPSGrowth=bvpsGrowthOverride;
        lowPE = fundamental.EPS * 2.00;
        trailingPE = fundamental.TrailingPE;
        averageLowTrailing = (lowPE + trailingPE) / 2.00;
        currentStockEstimatePrice = fundamental.EPS * averageLowTrailing;
        priceEstimate10y = currentStockEstimatePrice * Math.Pow((1.00 + averageBVPSGrowth), 10.00);
        todaysPriceForRequiredReturn = priceEstimate10y / Math.Pow((1.00 + requiredReturn), 10.00);
        marginOfSafety = todaysPriceForRequiredReturn * .50;
        marginOfSafety80Pcnt = todaysPriceForRequiredReturn * .80;
      }
      catch (Exception)
      {
      }
    }
    public StickerPrice(String symbol)
    {
      try
      {
        double requiredReturn = 15.00 / 100.00;
        TimeSeriesCollection bookValuePerShareCollection = HistoricalDA.GetTimeSeries(symbol, TimeSeriesElement.ElementType.BVPS);
        Fundamental fundamental = FundamentalDA.GetFundamental(symbol);
        bvpsItems = new ReturnItems();
        averageBVPSGrowth = Numerics.AverageReturnWithSpline(bookValuePerShareCollection,bvpsItems);
        //if (-1 == growthYears) averageGrowth = Numerics.AverageReturn(ref bookValuesPerShareSeries);
        //else averageGrowth = Numerics.AverageReturnTop(ref bookValuesPerShareSeries, growthYears);
        lowPE = fundamental.EPS * 2.00;
        trailingPE = fundamental.TrailingPE;
        averageLowTrailing = (lowPE + trailingPE) / 2.00;
        currentStockEstimatePrice = fundamental.EPS * averageLowTrailing;
        priceEstimate10y = currentStockEstimatePrice * Math.Pow((1.00 + averageBVPSGrowth), 10.00);
        todaysPriceForRequiredReturn = priceEstimate10y / Math.Pow((1.00 + requiredReturn), 10.00);
        marginOfSafety = todaysPriceForRequiredReturn * .50;
        marginOfSafety80Pcnt = todaysPriceForRequiredReturn * .80;
      }
      catch (Exception)
      {
      }
    }
    public ReturnItems BVPSItems
    {
      get
      {
        return bvpsItems;
      }
    }
    public double AverageBVPSGrowth
    {
      get { return averageBVPSGrowth; }
    }
    public double MarginOfSafety
    {
      get { return marginOfSafety; }
    }
    public double MarginOfSafety80Pcnt
    {
      get { return marginOfSafety80Pcnt; }
    }
    public double LowPE
    {
      get { return lowPE; }
    }
    public double TrailingPE
    {
      get { return trailingPE; }
    }
    public double AverageLowTrailing
    {
      get { return averageLowTrailing; }
    }
    public double CurrentStockEstimatePrice
    {
      get { return currentStockEstimatePrice; }
    }
    public double PriceEstimate10y
    {
      get { return priceEstimate10y; }
    }
    public double TodaysPriceForRequiredReturn
    {
      get { return todaysPriceForRequiredReturn; }
    }
  }
}
