//+------------------------------------------------------------------+
//|                                           AlignmentValidator.mqh |
//|              Pure alignment rules for the bar aggregator         |
//+------------------------------------------------------------------+
#ifndef ALIGNMENTVALIDATOR_MQH
#define ALIGNMENTVALIDATOR_MQH

#include "MultiTFTypes.mqh"

//+------------------------------------------------------------------+
//| Implements the grid, Recency, and closed bar rules.             |
//| The class holds no state and makes no terminal calls.            |
//+------------------------------------------------------------------+
class CAlignmentValidator
  {
public:
                     CAlignmentValidator(void);
                    ~CAlignmentValidator(void);
   int               SupportedPeriodSeconds(const ENUM_TIMEFRAMES timeframe) const;
   bool              IsSupported(const ENUM_TIMEFRAMES timeframe) const;
   bool              IsPeriodMultiple(const int base_seconds,const int period_seconds) const;
   bool              IsOnGrid(const datetime open_time,const int period_seconds) const;
   datetime          ExpectedNewestOpen(const datetime forming_base_open,const int period_seconds) const;
   ENUM_MTF_STATUS   CheckRecency(const datetime newest_open,const datetime expected_open) const;
   datetime          SnapshotTime(const datetime closed_base_open,const int base_seconds) const;
   bool              ClosedBarEndOk(const datetime closed_open,const int period_seconds,
                                    const datetime snapshot_time) const;
  };

//+------------------------------------------------------------------+
//| Constructor: nothing to initialize.                              |
//+------------------------------------------------------------------+
CAlignmentValidator::CAlignmentValidator(void)
  {
  }

//+------------------------------------------------------------------+
//| Destructor: nothing to release.                                  |
//+------------------------------------------------------------------+
CAlignmentValidator::~CAlignmentValidator(void)
  {
  }

//+------------------------------------------------------------------+
//| Returns the fixed period in seconds of a supported timeframe.    |
//| Supported means M1 through D1. W1, MN1, and PERIOD_CURRENT return|
//| zero, because their open times do not sit on a fixed epoch grid. |
//+------------------------------------------------------------------+
int CAlignmentValidator::SupportedPeriodSeconds(const ENUM_TIMEFRAMES timeframe) const
  {
   switch(timeframe)
     {
      case PERIOD_M1:  return(60);
      case PERIOD_M2:  return(120);
      case PERIOD_M3:  return(180);
      case PERIOD_M4:  return(240);
      case PERIOD_M5:  return(300);
      case PERIOD_M6:  return(360);
      case PERIOD_M10: return(600);
      case PERIOD_M12: return(720);
      case PERIOD_M15: return(900);
      case PERIOD_M20: return(1200);
      case PERIOD_M30: return(1800);
      case PERIOD_H1:  return(3600);
      case PERIOD_H2:  return(7200);
      case PERIOD_H3:  return(10800);
      case PERIOD_H4:  return(14400);
      case PERIOD_H6:  return(21600);
      case PERIOD_H8:  return(28800);
      case PERIOD_H12: return(43200);
      case PERIOD_D1:  return(86400);
     }
   return(0);
  }

//+------------------------------------------------------------------+
//| True when the timeframe has a fixed period in this project.      |
//+------------------------------------------------------------------+
bool CAlignmentValidator::IsSupported(const ENUM_TIMEFRAMES timeframe) const
  {
   return(SupportedPeriodSeconds(timeframe)>0);
  }

//+------------------------------------------------------------------+
//| True when period_seconds is a positive integer multiple of       |
//| base_seconds. A period shorter than the base is not a multiple.  |
//+------------------------------------------------------------------+
bool CAlignmentValidator::IsPeriodMultiple(const int base_seconds,const int period_seconds) const
  {
   if(base_seconds<=0 || period_seconds<=0)
      return(false);
   if(period_seconds<base_seconds)
      return(false);
   return((period_seconds%base_seconds)==0);
  }

//+------------------------------------------------------------------+
//| Rule 1. True when the open time is positive and a multiple of    |
//| the period, counted in seconds from the epoch. No offset is      |
//| guessed or applied.                                              |
//+------------------------------------------------------------------+
bool CAlignmentValidator::IsOnGrid(const datetime open_time,const int period_seconds) const
  {
   if((long)open_time<=0 || period_seconds<=0)
      return(false);
   return(((long)open_time%(long)period_seconds)==0);
  }

//+------------------------------------------------------------------+
//| Rule 2 helper. Returns floor(F / p) * p, the open time the       |
//| newest bar of a series must have when F is the base forming bar. |
//| Returns zero when either input is not positive.                  |
//+------------------------------------------------------------------+
datetime CAlignmentValidator::ExpectedNewestOpen(const datetime forming_base_open,
                                                 const int period_seconds) const
  {
   if((long)forming_base_open<=0 || period_seconds<=0)
      return((datetime)0);
   long f=(long)forming_base_open;
   long p=(long)period_seconds;
   return((datetime)((f/p)*p));
  }

//+------------------------------------------------------------------+
//| Rule 2. Compares the newest open time with the expected one.     |
//| Returns MTF_OK when equal, MTF_ERR_STALE when smaller, and       |
//| MTF_ERR_AHEAD when larger. Zero or negative inputs return        |
//| MTF_ERR_INVALID_TIME.                                            |
//+------------------------------------------------------------------+
ENUM_MTF_STATUS CAlignmentValidator::CheckRecency(const datetime newest_open,
                                                   const datetime expected_open) const
  {
   if((long)newest_open<=0 || (long)expected_open<=0)
      return(MTF_ERR_INVALID_TIME);
   if(newest_open<expected_open)
      return(MTF_ERR_STALE);
   if(newest_open>expected_open)
      return(MTF_ERR_AHEAD);
   return(MTF_OK);
  }

//+------------------------------------------------------------------+
//| Returns T = B + p_base, the end time of the last closed base bar.|
//| Returns zero when either input is not positive.                  |
//+------------------------------------------------------------------+
datetime CAlignmentValidator::SnapshotTime(const datetime closed_base_open,
                                           const int base_seconds) const
  {
   if((long)closed_base_open<=0 || base_seconds<=0)
      return((datetime)0);
   return((datetime)((long)closed_base_open+(long)base_seconds));
  }

//+------------------------------------------------------------------+
//| Rule 3. True when open + period does not exceed the snapshot     |
//| time. Equality is allowed, because a bar ending exactly at T has |
//| fully closed. Any non-positive input returns false.              |
//+------------------------------------------------------------------+
bool CAlignmentValidator::ClosedBarEndOk(const datetime closed_open,const int period_seconds,
                                         const datetime snapshot_time) const
  {
   if((long)closed_open<=0 || period_seconds<=0 || (long)snapshot_time<=0)
      return(false);
   long end_time=(long)closed_open+(long)period_seconds;
   return(end_time<=(long)snapshot_time);
  }

#endif // ALIGNMENTVALIDATOR_MQH
