#include "TMAbout.h"
#include "TMConfig.h"
#include <TGTextView.h>

ClassImp(TMAbout);


TMAbout::TMAbout(const TGWindow *p, const TGWindow *main, const Char_t *aboutWhat)
  :TGTransientFrame(gClient->GetRoot(), main, 500, 500, kVerticalFrame) {
  // Constructor.

  fL = new TGLayoutHints( kLHintsLeft | kLHintsCenterY | kLHintsCenterX, 0, 0, 0, 0);
   
  fTextView = new TGTextView(this, 750, 640);
  AddFrame(fTextView, fL);
  Char_t theTextFile[300];
    
  if (!strcmp(aboutWhat, "NODOC")) {
    sprintf(theTextFile, "%s/config/hints/nodoc.txt", getenv("SPDMOOD_SITE"));
    SetWindowName("No Help Available");
  }
  else if (!strcmp(aboutWhat, "README")) {
    sprintf(theTextFile, "%s/README", getenv("SPDMOOD_SITE"));
    SetWindowName("Notes for the user : README");
  } 
  else if (!strcmp(aboutWhat, "REMOTESOURCE")) {
    sprintf(theTextFile, "%s/config/hints/remotesource.txt", getenv("SPDMOOD_SITE"));
    SetWindowName("Syntax of defining a remote source");
  }
  else if (!strcmp(aboutWhat, "LOG")) {
    sprintf(theTextFile, "%s", TMConfig::Instance()->GETuploadCommand2().Data());
    SetWindowName("Log File");
  } 
  else if (!strcmp(aboutWhat, "SHOWGLOBALS")) {
    Char_t command[500];
    sprintf(command, "bash -c \"echo List of SPD-MOOD Globals : > $SPDMOOD_SITE/config/showGlobals.txt\"");
    system(command);
    sprintf(command, "bash -c \"date >> $SPDMOOD_SITE/config/showGlobals.txt\"");
    system(command);
    sprintf(command, "bash -c \"echo ---------------------------------------------------------- >> $SPDMOOD_SITE/config/showGlobals.txt\"");
    system(command);
    sprintf(command, "bash -c \"echo moodVersionFlag...............: %08X >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETmoodVersionFlag());
    system(command);
    sprintf(command, "bash -c \"echo handleEventMin................: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GEThandleEventMin());
    system(command);
    sprintf(command, "bash -c \"echo handleEventMax................: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GEThandleEventMax());
    system(command);
    sprintf(command, "bash -c \"echo uploadCommand2................: %s >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETuploadCommand2().Data());
    system(command);
    sprintf(command, "bash -c \"echo eventType.....................: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETeventType());
    system(command);
    sprintf(command, "bash -c \"echo monitoringPolicy..............: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETmonitoringPolicy());
    system(command);
    sprintf(command, "bash -c \"echo monitorOffLine................: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETmonitorOffLine());
    system(command);
    sprintf(command, "bash -c \"echo declareMonitor................: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETdeclareMonitor());
    system(command);
    sprintf(command, "bash -c \"echo swap16........................: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETswap16());
    system(command);
    sprintf(command, "bash -c \"echo swap32........................: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETswap32());
    system(command);
    sprintf(command, "bash -c \"echo setNowait.....................: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETsetNowait());
    system(command);
    sprintf(command, "bash -c \"echo dataSourceFull................: %s >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETdataSourceFullPath().Data());
    system(command);
    sprintf(command, "bash -c \"echo dataSourceFullPathOnline......: %s >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETdataSourceFullPathOnline().Data());
    system(command);
    sprintf(command, "bash -c \"echo dataSourceFullPathOffline.....: %s >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETdataSourceFullPathOffline().Data());
    system(command);
    sprintf(command, "bash -c \"echo dataSourceOnline..............: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETdataSourceOnline());
    system(command);
    sprintf(command, "bash -c \"echo nameOfMp......................: %s >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETnameOfMp().Data());
    system(command);
    sprintf(command, "bash -c \"echo multipleFiles.................: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETmultipleFiles());
    system(command);
    sprintf(command, "bash -c \"echo dumpInconsistent..............: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETdumpInconsistent());
    system(command);
    sprintf(command, "bash -c \"echo alerBeforeDumping.............: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETalerBeforeDumping());
    system(command);
    sprintf(command, "bash -c \"echo aa_runNb_.....................: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETaa_runNb_());
    system(command);
    sprintf(command, "bash -c \"echo aa_nbInRun_...................: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETaa_nbInRun_());
    system(command);
    sprintf(command, "bash -c \"echo dumpNr........................: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETdumpNr());
    system(command);
    sprintf(command, "bash -c \"echo deadLimit.....................: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETdeadLimit());
    system(command);
    sprintf(command, "bash -c \"echo noisyLimit....................: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETnoisyLimit());
    system(command);
    sprintf(command, "bash -c \"echo uploadCommand.................: %s >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETuploadCommand().Data());
    system(command);
    sprintf(command, "bash -c \"echo updateTime....................: %f >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETupdateTime());
    system(command);
    sprintf(command, "bash -c \"echo movieName.....................: %s >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETmovieName().Data());
    system(command);
    sprintf(command, "bash -c \"echo movieTotal....................: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETmovieTotal());
    system(command);
    sprintf(command, "bash -c \"echo movieRate.....................: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETmovieRate());
    system(command);
    sprintf(command, "bash -c \"echo movieDelay....................: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETmovieDelay());
    system(command);
    TString eqAct = "";
    for (UInt_t eq=0; eq<20; eq++) {
      eqAct.Append(Form("%d",TMConfig::Instance()->GETeqActive(eq)));
    }
    sprintf(command, "bash -c \"echo eqActive......................: %s >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    eqAct.Data());
    system(command);
    sprintf(command, "bash -c \"echo errorSuppr....................: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETerrorSuppr());
    system(command);
    sprintf(command, "bash -c \"echo rootMode......................: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETrootMode());
    system(command);
    sprintf(command, "bash -c \"echo advEvCntDumpAll...............: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETadvEvCntDumpAll());
    system(command);
    sprintf(command, "bash -c \"echo globalOverlap.................: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETglobalOverlap());
    system(command);
    sprintf(command, "bash -c \"echo multipleFileType..............: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETmultipleFileType());
    system(command);
    sprintf(command, "bash -c \"echo dataSourceOfflineList.........: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETdataSourceOfflineList());
    system(command);
    sprintf(command, "bash -c \"echo dataSourceFullPathOfflineList.: %s >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETdataSourceFullPathOfflineList().Data());
    system(command);
    sprintf(command, "bash -c \"echo safetyHoldTime................: %f >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETsafetyHoldTime());
    system(command);
    sprintf(command, "bash -c \"echo safetyContTime................: %f >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETsafetyContTime());
    system(command);
    sprintf(command, "bash -c \"echo stopRunEventNumber............: %d >> $SPDMOOD_SITE/config/showGlobals.txt\"", 
	    TMConfig::Instance()->GETstopRunEventNumber());
    system(command);

    sprintf(theTextFile, "%s/config/showGlobals.txt", getenv("SPDMOOD_SITE"));
    SetWindowName("List of SPD-MOOD Globals");
  } 
  else {
    printf("You should not see this text.\n");
  }
    
  fTextView->LoadFile(theTextFile);
    
  Char_t picture[300];
  sprintf(picture, "%s/src/images/button7long.xpm", getenv("SPDMOOD_SITE"));
  fPictureButton = new TGPictureButton(this, picture, BUTTON_CLOSE);
  fPictureButton->SetToolTipText("Close.", 0);
  fPictureButton->Associate(this);
  AddFrame(fPictureButton, fL);

  fLetUserCloseWindow = kFALSE;
    
  MapSubwindows();
  Resize();
  MapWindow();
    
  SetWMSizeHints(750, 670, 750, 670, 0, 0);
}

TMAbout::~TMAbout() {
  // Destructor.
  delete   fGroup;
  delete   fTextView;
  delete   fPictureButton;
  delete   fL;
}

void TMAbout::CloseWindow() {
  // Prevents window manager close window function.
  if (fLetUserCloseWindow == kFALSE)
    DontCallClose();
  else
    UnmapWindow();
}

Bool_t TMAbout::ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2) {
  // Event handler.

  switch(GET_MSG(msg)) {
  case kC_COMMAND :
    switch (GET_SUBMSG(msg)) {
    case kCM_BUTTON :
      switch (parm1) {
      case BUTTON_CLOSE :
	{
	  fLetUserCloseWindow = kTRUE;
	  CloseWindow();
	  break;
	}
      }
    }
  }
  return kTRUE;    
}
