//+------------------------------------------------------------------+
//|                                                MultiTFReport.mqh |
//|                  Pure log line formatting for bundles and errors |
//+------------------------------------------------------------------+
#ifndef MULTITFREPORT_MQH
#define MULTITFREPORT_MQH

#include "MultiTFTypes.mqh"

//+------------------------------------------------------------------+
//| Formats results into fixed, documented log lines.                |
//|                                                                  |
//| Header line:                                                     |
//| MTF bundle | state=VALID | symbol=S | base=TF | snapshot=T |     |
//| timeframes=N                                                     |
//| Bar line:                                                        |
//| MTF bar | tf=TF | open=T | end=T | bars=N                        |
//| Error line:                                                      |
//| MTF error | status=NAME | tf=TF | expected=T | actual=T |        |
//| code=N | text=SENTENCE                                           |
//|                                                                  |
//| Times use TimeToString() with date and minutes. A time of zero   |
//| or less prints as "n/a". Each line is one string, with no breaks.|
//+------------------------------------------------------------------+
class CMtfReport
  {
public:
                     CMtfReport(void);
                    ~CMtfReport(void);
   string            FormatTime(const datetime value) const;
   string            BundleHeaderLine(const CAlignedBundle &bundle) const;
   string            BarLine(const CAlignedBundle &bundle,const int index) const;
   string            ErrorLine(const CMtfError &error) const;
   int               FormatBundle(const CAlignedBundle &bundle,string &lines[]);
  };

//+------------------------------------------------------------------+
//| Constructor: nothing to initialize.                              |
//+------------------------------------------------------------------+
CMtfReport::CMtfReport(void)
  {
  }

//+------------------------------------------------------------------+
//| Destructor: nothing to release.                                  |
//+------------------------------------------------------------------+
CMtfReport::~CMtfReport(void)
  {
  }

//+------------------------------------------------------------------+
//| Formats a time as "yyyy.mm.dd hh:mi", or "n/a" when the value is |
//| zero or negative.                                                |
//+------------------------------------------------------------------+
string CMtfReport::FormatTime(const datetime value) const
  {
   if((long)value<=0)
      return("n/a");
   return(::TimeToString(value,TIME_DATE|TIME_MINUTES));
  }

//+------------------------------------------------------------------+
//| Builds the header line. An invalid bundle prints state=INVALID   |
//| and zero timeframes, so it never looks usable.                   |
//+------------------------------------------------------------------+
string CMtfReport::BundleHeaderLine(const CAlignedBundle &bundle) const
  {
   string state="INVALID";
   int count=0;
   if(bundle.IsValid())
     {
      state="VALID";
      count=bundle.Count();
     }
   return("MTF bundle | state="+state+
          " | symbol="+bundle.SymbolName()+
          " | base="+MtfTimeframeName(bundle.BaseTimeframe())+
          " | snapshot="+FormatTime(bundle.SnapshotTime())+
          " | timeframes="+::IntegerToString(count));
  }

//+------------------------------------------------------------------+
//| Builds the line for one bundle slot. Returns an empty string     |
//| when the index does not hold a bar.                              |
//+------------------------------------------------------------------+
string CMtfReport::BarLine(const CAlignedBundle &bundle,const int index) const
  {
   CTimeframeBar bar;
   if(index<0 || index>=bundle.Count())
      return("");
   if(!bundle.GetBar(index,bar))
      return("");
   return("MTF bar | tf="+MtfTimeframeName(bar.timeframe)+
          " | open="+FormatTime(bar.open_time)+
          " | end="+FormatTime(bar.end_time)+
          " | bars="+::IntegerToString(bundle.BarCountAt(index)));
  }

//+------------------------------------------------------------------+
//| Builds the error line with the offending timestamps.             |
//+------------------------------------------------------------------+
string CMtfReport::ErrorLine(const CMtfError &error) const
  {
   return("MTF error | status="+MtfStatusName(error.status)+
          " | tf="+MtfTimeframeName(error.timeframe)+
          " | expected="+FormatTime(error.expected_time)+
          " | actual="+FormatTime(error.actual_time)+
          " | code="+::IntegerToString(error.error_code)+
          " | text="+error.text);
  }

//+------------------------------------------------------------------+
//| Writes the header line and one bar line per slot into lines.     |
//| Returns the number of lines, or -1 when the array cannot be      |
//| resized. An invalid bundle yields the header line only.          |
//+------------------------------------------------------------------+
int CMtfReport::FormatBundle(const CAlignedBundle &bundle,string &lines[])
  {
   int total=1;
   if(bundle.IsValid())
      total=1+bundle.Count();
//--- size the output array and check the result
   if(::ArrayResize(lines,total)!=total)
      return(-1);
//--- fill the header and the bar lines
   lines[0]=BundleHeaderLine(bundle);
   for(int i=1;i<total;i++)
      lines[i]=BarLine(bundle,i-1);
   return(total);
  }

#endif // MULTITFREPORT_MQH
//+------------------------------------------------------------------+