//+------------------------------------------------------------------+
//|                                                    BarSource.mqh |
//|        Abstract bar source and the terminal implementation       |
//+------------------------------------------------------------------+
#ifndef BARSOURCE_MQH
#define BARSOURCE_MQH

#include "MultiTFTypes.mqh"

//+------------------------------------------------------------------+
//| Abstract source of bars. The aggregator talks only to this class,|
//| so tests can replace the terminal with scripted data.            |
//+------------------------------------------------------------------+
class CBarSourceBase
  {
public:
                     CBarSourceBase(void);
   virtual          ~CBarSourceBase(void);
   virtual string    SymbolName(void)=0;
   virtual bool      IsSynchronized(const ENUM_TIMEFRAMES timeframe)=0;
   virtual int       BarCount(const ENUM_TIMEFRAMES timeframe)=0;
   virtual bool      ReadBar(const ENUM_TIMEFRAMES timeframe,const int shift,
                             CTimeframeBar &bar,int &error_code)=0;
  };

//+------------------------------------------------------------------+
//| Constructor: nothing to initialize.                              |
//+------------------------------------------------------------------+
CBarSourceBase::CBarSourceBase(void)
  {
  }

//+------------------------------------------------------------------+
//| Virtual destructor, so derived objects delete correctly through  |
//| a base pointer.                                                  |
//+------------------------------------------------------------------+
CBarSourceBase::~CBarSourceBase(void)
  {
  }

//+------------------------------------------------------------------+
//| The single impure class of the project. It reads real terminal   |
//| data for one symbol.                                             |
//+------------------------------------------------------------------+
class CTerminalBarSource : public CBarSourceBase
  {
private:
   string            m_symbol;

public:
                     CTerminalBarSource(const string symbol);
   virtual          ~CTerminalBarSource(void);
   virtual string    SymbolName(void);
   virtual bool      IsSynchronized(const ENUM_TIMEFRAMES timeframe);
   virtual int       BarCount(const ENUM_TIMEFRAMES timeframe);
   virtual bool      ReadBar(const ENUM_TIMEFRAMES timeframe,const int shift,
                             CTimeframeBar &bar,int &error_code);
  };

//+------------------------------------------------------------------+
//| Constructor: remembers the symbol name. The symbol is not        |
//| checked here, because every read reports its own failure.        |
//+------------------------------------------------------------------+
CTerminalBarSource::CTerminalBarSource(const string symbol)
  {
   m_symbol=symbol;
  }

//+------------------------------------------------------------------+
//| Destructor: nothing to release.                                  |
//+------------------------------------------------------------------+
CTerminalBarSource::~CTerminalBarSource(void)
  {
  }

//+------------------------------------------------------------------+
//| Returns the symbol this source reads.                            |
//+------------------------------------------------------------------+
string CTerminalBarSource::SymbolName(void)
  {
   return(m_symbol);
  }

//+------------------------------------------------------------------+
//| Reports whether the terminal considers the series synchronized.  |
//| Returns false when the property cannot be read.                  |
//+------------------------------------------------------------------+
bool CTerminalBarSource::IsSynchronized(const ENUM_TIMEFRAMES timeframe)
  {
   return(::SeriesInfoInteger(m_symbol,timeframe,SERIES_SYNCHRONIZED)!=0);
  }

//+------------------------------------------------------------------+
//| Returns the number of bars the terminal holds for the series.    |
//| The value is diagnostic context only. Zero means no data.        |
//+------------------------------------------------------------------+
int CTerminalBarSource::BarCount(const ENUM_TIMEFRAMES timeframe)
  {
   return(::Bars(m_symbol,timeframe));
  }

//+------------------------------------------------------------------+
//| Reads exactly one bar at a shift (0 is the newest bar).          |
//| On success the bar holds time, prices, and tick volume, and its  |
//| valid flag stays false, because only the aggregator grants it.   |
//| On failure the bar is reset, error_code holds GetLastError() or  |
//| MTF_CODE_UNKNOWN, and the result is false.                       |
//+------------------------------------------------------------------+
bool CTerminalBarSource::ReadBar(const ENUM_TIMEFRAMES timeframe,const int shift,
                                 CTimeframeBar &bar,int &error_code)
  {
   bar.Reset();
   error_code=MTF_CODE_NONE;
//--- a negative shift is a caller error
   if(shift<0)
     {
      error_code=MTF_CODE_UNKNOWN;
      return(false);
     }
//--- copy exactly one row
   MqlRates rates[];
   ::ResetLastError();
   int copied=::CopyRates(m_symbol,timeframe,shift,1,rates);
   if(copied!=1 || ::ArraySize(rates)<1)
     {
      error_code=::GetLastError();
      if(error_code==MTF_CODE_NONE)
         error_code=MTF_CODE_UNKNOWN;
      return(false);
     }
//--- translate the row into the project structure
   bar.timeframe=timeframe;
   bar.open_time=rates[0].time;
   bar.open_price=rates[0].open;
   bar.high_price=rates[0].high;
   bar.low_price=rates[0].low;
   bar.close_price=rates[0].close;
   bar.tick_volume=rates[0].tick_volume;
   return(true);
  }

#endif // BARSOURCE_MQH
