#include "TMConfig.h"
#include "SPDMood.h"
#include <fstream>

Bool_t TMConfig::instanceFlag = kFALSE;
TMConfig* TMConfig::single = NULL;

extern TString gVERSION;

//___________________________________________________________________________________
TMConfig* TMConfig::Instance() {
 if(!instanceFlag) {
   instanceFlag = kTRUE;
   single = new TMConfig();
   return single;
 }
 else {
   return single;
 }
}
//___________________________________________________________________________________
TMConfig::TMConfig() {
  SetDefaultConfig();
}
//___________________________________________________________________________________
TMConfig::~TMConfig() {
  instanceFlag=kFALSE;
}
//___________________________________________________________________________________
void TMConfig::SetDefaultConfig() {
  aa_nbInRun_           = 0;
  aa_runNb_             = 0;  
  moodVersionFlag	= 0x300D0401;
  handleEventMin        = 0;
  handleEventMax        = 100000000;
  uploadCommand2        = ".";
  eventType             = 6;
  monitoringPolicy      = 0;
  monitorOffLine        = kTRUE;
  declareMonitor        = kTRUE;
  swap16                = kFALSE;
  swap32                = kFALSE;
  setNowait             = kFALSE;
  dataSourceFullPath        = "@aldaqpc033:";
  dataSourceFullPathOnline  = "@aldaqpc033:";
  dataSourceFullPathOffline = "/local/data/run28111.000@aldaqpc056";
  nameOfMp              = "SPD-MOOD";
  multipleFiles         = kFALSE;
  dumpInconsistent      = kFALSE;
  alerBeforeDumping     = kFALSE;
  dumpNr                = 1;
  deadLimit             = 0;
  noisyLimit            = 100;
  uploadCommand         = ".";
  updateTime            = 0.;
  movieName             = "./movieTest";
  movieTotal            = 10;
  movieRate             = 1;
  movieDelay            = 1;
  for (UInt_t eq=0; eq<20; eq++) eqActive[eq]=kTRUE;
  errorSuppr            = 0;
  rootMode              = kFALSE;
  advEvCntDumpAll       = kFALSE;
  globalOverlap         = kFALSE;
  multipleFileType      = kFALSE;
  dataSourceOfflineList = kFALSE;
  dataSourceFullPathOfflineList = "/local/home/spd/fileList.txt";
  safetyHoldTime        = 2.;
  safetyContTime        = 2.;
  stopRunEventNumber    = 0;
  triggerMask0          = 0xffffffff;
  triggerMask1          = 0xffffffff;
}
//___________________________________________________________________________________
void TMConfig::SaveConfig(const Char_t *fileName) {
  TString confFileName = fileName;
  if (confFileName.Length()==0) {
    confFileName = Form("%s/.spdmood%s.conf", getenv("HOME"), gVERSION.Data());
  }
  ofstream cfFile;
  cfFile.open(confFileName.Data());
  if (!cfFile) {
    printf("Could not save configuration to file '%s'.\n",confFileName.Data());
    return;
  }

  if (uploadCommand2.Length()==0) uploadCommand2 = ".";
  if (dataSourceFullPath.Length()==0) dataSourceFullPath = ".";
  if (dataSourceFullPathOnline.Length()==0) dataSourceFullPathOnline = ".";
  if (dataSourceFullPathOffline.Length()==0) dataSourceFullPathOffline = ".";
  if (nameOfMp.Length()==0) nameOfMp = ".";
  if (uploadCommand.Length()==0) uploadCommand = ".";
  if (movieName.Length()==0) movieName = ".";

  cfFile << aa_nbInRun_                                                                  << "\n" // 0
	 << aa_runNb_									 << "\n" // 1
	 << moodVersionFlag								 << "\n" // 2
	 << handleEventMin								 << "\n" // 3
	 << handleEventMax								 << "\n" // 4
	 << uploadCommand2.Data()							 << "\n" // 5
	 << eventType									 << "\n" // 6
	 << monitoringPolicy								 << "\n" // 7
	 << monitorOffLine								 << "\n" // 8
	 << declareMonitor								 << "\n" // 9
	 << swap16									 << "\n" // 10
	 << swap32									 << "\n" // 11
	 << setNowait                                                                    << "\n" // 12
	 << dataSourceFullPath.Data()							 << "\n" // 13
	 << dataSourceFullPathOnline.Data()						 << "\n" // 14
	 << dataSourceFullPathOffline.Data()						 << "\n" // 15
	 << dataSourceOnline								 << "\n" // 16
	 << nameOfMp.Data()								 << "\n" // 17
	 << multipleFiles								 << "\n" // 18
	 << dumpInconsistent								 << "\n" // 19
	 << alerBeforeDumping								 << "\n" // 20
	 << dumpNr									 << "\n" // 21
	 << deadLimit									 << "\n" // 22
	 << noisyLimit									 << "\n" // 23
	 << uploadCommand.Data()							 << "\n" // 24
	 << updateTime									 << "\n" // 25
	 << movieName.Data()								 << "\n" // 26
	 << movieTotal									 << "\n" // 27
	 << movieRate									 << "\n" // 28
	 << movieDelay									 << "\n" // 29
	 << eqActive[0] << "\n" << eqActive[1] << "\n" << eqActive[2] << "\n" 
	 << eqActive[3] << "\n" << eqActive[4] << "\n" << eqActive[5] << "\n" 
	 << eqActive[6] << "\n" << eqActive[7] << "\n" << eqActive[8] << "\n"
	 << eqActive[9] << "\n" << eqActive[10] << "\n" << eqActive[11] << "\n"
	 << eqActive[12] << "\n" << eqActive[13] << "\n" << eqActive[14] << "\n"
	 << eqActive[15] << "\n" << eqActive[16] << "\n" << eqActive[17] << "\n"
	 << eqActive[18] << "\n" << eqActive[19]                                         << "\n" // 30-49
	 << errorSuppr                                                                   << "\n" // 50
	 << rootMode                                                                     << "\n" // 51
	 << advEvCntDumpAll                                                              << "\n" // 52
	 << globalOverlap                                                                << "\n" // 53
	 << multipleFileType                                                             << "\n" // 54
	 << dataSourceOfflineList                                                        << "\n" // 55
	 << dataSourceFullPathOfflineList                                                << "\n" // 56
	 << safetyHoldTime                                                               << "\n" // 57
	 << safetyContTime                                                               << "\n" // 58
	 << stopRunEventNumber                                                           << "\n" // 59
	 << triggerMask0                                                                 << "\n" // 60
	 << triggerMask1                                                                 << "\n";// 61

  cfFile.close();
}
//___________________________________________________________________________________
void TMConfig::LoadConfig(const Char_t *fileName) {
  TString confFileName = fileName;
  if (confFileName.Length()==0) {
    confFileName = Form("%s/.spdmood%s.conf", getenv("HOME"), gVERSION.Data());
  }
  ifstream cfFile;
  cfFile.open(confFileName.Data(),ifstream::in);
  if (cfFile.fail()) {
    printf("No configuration file found. Taking default values.\n");
    return SetDefaultConfig();
  }

  UInt_t index=0;
  while (1) {
    Char_t tmpVal[200];
    cfFile >> tmpVal;
    if (cfFile.eof()) break;
    if (index==0) aa_nbInRun_=atoi(tmpVal);
    else if (index==1) aa_runNb_=atoi(tmpVal);
    else if (index==2) moodVersionFlag=atoi(tmpVal);
    else if (index==3) handleEventMin=atoi(tmpVal);
    else if (index==4) handleEventMax=atoi(tmpVal);
    else if (index==5) uploadCommand2=tmpVal;
    else if (index==6) eventType=atoi(tmpVal);
    else if (index==7) monitoringPolicy=atoi(tmpVal);
    else if (index==8) monitorOffLine=atoi(tmpVal);
    else if (index==9) declareMonitor=atoi(tmpVal);
    else if (index==10) swap16=atoi(tmpVal);
    else if (index==11) swap32=atoi(tmpVal);
    else if (index==12) setNowait=atoi(tmpVal);
    else if (index==13) dataSourceFullPath=tmpVal;
    else if (index==14) dataSourceFullPathOnline=tmpVal;
    else if (index==15) dataSourceFullPathOffline=tmpVal;
    else if (index==16) dataSourceOnline=atoi(tmpVal);
    else if (index==17) nameOfMp=tmpVal;
    else if (index==18) multipleFiles=atoi(tmpVal);
    else if (index==19) dumpInconsistent=atoi(tmpVal);
    else if (index==20) alerBeforeDumping=atoi(tmpVal);
    else if (index==21) dumpNr=atoi(tmpVal);
    else if (index==22) deadLimit=atoi(tmpVal);
    else if (index==23) noisyLimit=atoi(tmpVal);
    else if (index==24) uploadCommand=tmpVal;
    else if (index==25) updateTime=atoi(tmpVal);
    else if (index==26) movieName=tmpVal;
    else if (index==27) movieTotal=atoi(tmpVal);
    else if (index==28) movieRate=atoi(tmpVal);
    else if (index==29) movieDelay=atoi(tmpVal);
    else if (index>=30 && index<50) eqActive[index-30]=atoi(tmpVal);
    else if (index==50) errorSuppr=atoi(tmpVal);
    else if (index==51) rootMode=atoi(tmpVal);
    else if (index==52) advEvCntDumpAll=atoi(tmpVal);
    else if (index==53) globalOverlap=atoi(tmpVal);
    else if (index==54) multipleFileType=atoi(tmpVal);
    else if (index==55) dataSourceOfflineList=atoi(tmpVal);
    else if (index==56) dataSourceFullPathOfflineList=tmpVal;
    else if (index==57) safetyHoldTime=atof(tmpVal);
    else if (index==58) safetyContTime=atof(tmpVal);
    else if (index==59) stopRunEventNumber=atoi(tmpVal);
    else if (index==60) triggerMask0=strtoul(tmpVal,NULL,0);
    else if (index==61) triggerMask1=strtoul(tmpVal,NULL,0);
    index++;
  }

  cfFile.close();
}
