#include "TMHistogrammingCErrorMessages.h"
#include "SPDMood.h"
#include "AliITSRawStreamSPD.h"
#include <TStyle.h>
#include <TGText.h>
#include <TGTextView.h>

//_____________________________________________________________________
TMHistogrammingCErrorMessages::TMHistogrammingCErrorMessages() :
  TMHistogramming(), fNrLines(0), fDumpToTerminal(kTRUE)
{}
//_____________________________________________________________________
TMHistogrammingCErrorMessages::~TMHistogrammingCErrorMessages() {
}
//_____________________________________________________________________
void TMHistogrammingCErrorMessages::EndEvent(AliITSRawStreamSPD *str, UInt_t globalEventNumber) {
  if (fEventAccept) {
    AliITSRawStreamSPDErrorLog *advLogger = str->GetAdvancedErrorLog();

    if (advLogger->GetNrErrorsAllEq(0)>0) {
      TString newEv = Form("__________________________ ERRORS EVENT %d (PHYSICS EVENT)__________________________",globalEventNumber);
      if (!DumpThis(newEv.Data())) {
	return;
      }
    }

    TGText* genText = advLogger->GetTextGeneralThisEvent();
    for (Long_t line=0; line<genText->RowCount()-1; line++) {
      if (line==0) {
	DumpThis("General Errors:");
      }
      DumpThis(genText->GetLine(TGLongPosition(0,line),(ULong_t)(genText->GetLineLength(line))));
    }

    for (UInt_t eq=0; eq<20; eq++) {
      TGText* eqText = advLogger->GetTextThisEvent(eq);
      for (Long_t line=0; line<eqText->RowCount()-1; line++) {
	if (line==0) {
	  TString msg = Form("Eq %d:",eq);
	  DumpThis(msg.Data());
	}
	DumpThis(eqText->GetLine(TGLongPosition(0,line),(ULong_t)(eqText->GetLineLength(line))));
      }
    }


  }
}
//_____________________________________________________________________
void TMHistogrammingCErrorMessages::Dump(TGTextView *textview) {
  if (fDumpToTerminal) {
    DumpBuffer();
  }
  else {
    textview->Clear();
    LoadBuffer(textview);
  }
  fNeedRedump=kFALSE;
}
//_____________________________________________________________________
void TMHistogrammingCErrorMessages::DumpBuffer() {
  for (UInt_t i=0; i<fNrLines; i++) {
    printf("%s\n",fTextLines[i].Data());
  }
  if (fNrLines>0) printf("\n");
}
//_____________________________________________________________________
void TMHistogrammingCErrorMessages::LoadBuffer(TGTextView *textview) {
  for (UInt_t i=0; i<fNrLines; i++) {
    textview->AddLineFast(fTextLines[i].Data());
  }
  textview->AddLineFast(" ");
  textview->AddLineFast(" ");
  textview->Update();
}
//_____________________________________________________________________
void TMHistogrammingCErrorMessages::Reset() {
  fNrLines=0;

  fNeedRedump=kTRUE;

  TMHistogramming::Reset();
}
//_____________________________________________________________________
Bool_t TMHistogrammingCErrorMessages::DumpThis(const Char_t *msg) {
  fNeedRedump=kTRUE;
  if (fNrLines>=gMAXLINESmess-1) {
    if (fNrLines==gMAXLINESmess-1) {
      fTextLines[fNrLines] = Form("MAXIMUM NUMBER OF LINES (%d) REACHED...REST IS SKIPPED",gMAXLINESmess);
      fNrLines++;
    }
    return kFALSE;
  }
  fTextLines[fNrLines] = msg;
  fNrLines++;
  return kTRUE;
}
//_____________________________________________________________________
Bool_t TMHistogrammingCErrorMessages::DumpThis(const Char_t *msg, Int_t value, Char_t *status, Int_t binaryLength) {
  // status = hex / dec / bin
  fNeedRedump=kTRUE;
  if (fNrLines>=gMAXLINESmess-1) {
    if (fNrLines==gMAXLINESmess-1) {
      fTextLines[fNrLines] = Form("MAXIMUM NUMBER OF LINES (%d) REACHED...REST IS SKIPPED",gMAXLINESmess);
      fNrLines++;
    }
    return kFALSE;
  }

  Char_t m[400];
  if ("hex" == status) sprintf(m, "%s : 0x%08x  (hex)", msg, value);
  if ("dec" == status) sprintf(m, "%s : %10d  (dec)", msg, value);
  if ("tme" == status) {
    time_t time;
    time=(time_t)value;
    Char_t *s;
    s=ctime(&time);
    s[strlen(s)-1]=0;
    sprintf(m, "%s : %s (tme)", msg, s);
  }
  if ("bin" == status) {
    sprintf(m, "%s : 0x%08x  (hex)   %02d bits dump : ", msg, value, binaryLength);
    for (Int_t i=binaryLength-1 ; i>=0 ; i--) {
      if ( 1 == ((value>>i)&(0x00000001)) ) {
	strcat(m, "1");
      } 
      else {
	strcat(m, "0");
      }
    }
  }

  //  TString messSTR = Form("%s",m);

  fTextLines[fNrLines] = m;
  fNrLines++;
  return kTRUE;
}
