#include "TMConfigureDateSimple.h"
#include "TMConfig.h"
#include "SPDMood.h"
#include "TMAbout.h"
#include <TGFileDialog.h>

ClassImp(TMConfigureDateSimple);

//___________________________________________________________________________________
TMConfigureDateSimple::TMConfigureDateSimple(const TGWindow *p, const TGWindow *main) : TGTransientFrame(gClient->GetRoot(), main, 670, 500, kVerticalFrame) {
  // Constructor.
  fSPD = main;

  fLayout  = new TGLayoutHints(kLHintsCenterY | kLHintsExpandX,1,1,1,1);
  fLayout2 = new TGLayoutHints(kLHintsBottom | kLHintsExpandX,1,1,1,1);
  fLayout3 = new TGLayoutHints(kLHintsCenterY,1,1,1,1);
  fLayout4 = new TGLayoutHints(kLHintsBottom | kLHintsExpandX,10,10,10,10);
  fLayout5 = new TGLayoutHints(kLHintsTop | kLHintsExpandX,10,10,10,10);
  fLayout6 = new TGLayoutHints(kLHintsCenterY,1,1,1,1);
  fLayout7 = new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 0,0,0,0);
  fLayout8 = new TGLayoutHints(kLHintsRight | kLHintsExpandY, 0,0,0,0);
  fLayout9 = new TGLayoutHints(kLHintsCenterY, 0,0,0,0);

  UInt_t textLength = 400;

  fTop = new TGGroupFrame(this, "Data Source", kVerticalFrame);
  AddFrame(fTop,fLayout5);

  fOnlineSL = new TGCompositeFrame(this, 10,10, kHorizontalFrame);
  fOnlineSLabel = new TGLabel(fOnlineSL, "Online Path:  [  ex:  @aldaqpc018:  or  @aldaqpc018-LDC:  ]");
  fOnlineSL->AddFrame(fOnlineSLabel, fLayout7);
  fTop->AddFrame(fOnlineSL, fLayout5);

  fOnlineS = new TGCompositeFrame(this, 10,10, kHorizontalFrame);
  fRadioFrame[0] = new TGCompositeFrame(fOnlineS, 130, 10, kHorizontalFrame | kFixedWidth);
  fRadio[0]=new TGRadioButton(fRadioFrame[0], "Online", CONF_ONLINE);
  fRadioFrame[0]->AddFrame(fRadio[0],fLayout5);
  fRadio[0]->Associate(this); 
  fOnlineS->AddFrame(fRadioFrame[0], fLayout9);
  fButtonFOnline = new TGPictureButton(fOnlineS, fClient->GetPicture("folder_t.xpm"), BUTTON_FETCHONLINE);
  fButtonFOnline->SetToolTipText("Data file full path for ON-LINE.", 0);
  fButtonFOnline->Associate(this);
  fOnlineS->AddFrame(fButtonFOnline, fLayout3);
  fTextSOnline = new TGTextEntry(fOnlineS, TMConfig::Instance()->GETdataSourceFullPathOnline().Data(), CONF_SOURCEONLINE);
  fTextSOnline->Associate(this);
  fTextSOnline->SetWidth(textLength);
  fOnlineS->AddFrame(fTextSOnline, fLayout3);
  fTop->AddFrame(fOnlineS, fLayout);



  fOfflineSL = new TGCompositeFrame(this, 10,10, kHorizontalFrame);
  fOfflineSLabel = new TGLabel(fOfflineSL, "Offline Path:  [  ex:  /SPDrawdata/run4200.000@aldaqpc018  ]");
  fOfflineSL->AddFrame(fOfflineSLabel, fLayout7);
  fTop->AddFrame(fOfflineSL, fLayout5);

  fOfflineS = new TGCompositeFrame(this, 10,10, kHorizontalFrame);
  fRadioFrame[1] = new TGCompositeFrame(fOfflineS, 130, 10, kHorizontalFrame | kFixedWidth);
  fRadio[1]=new TGRadioButton(fRadioFrame[1], "Offline", CONF_OFFLINE);
  fRadioFrame[1]->AddFrame(fRadio[1],fLayout5);
  fRadio[1]->Associate(this); 
  fOfflineS->AddFrame(fRadioFrame[1], fLayout3);
  fButtonFOffline = new TGPictureButton(fOfflineS, fClient->GetPicture("folder_t.xpm"), BUTTON_FETCHOFFLINE);
  fButtonFOffline->SetToolTipText("Data file full path for OFF-LINE.", 0);
  fButtonFOffline->Associate(this);
  fOfflineS->AddFrame(fButtonFOffline, fLayout3);
  fTextSOffline = new TGTextEntry(fOfflineS, TMConfig::Instance()->GETdataSourceFullPathOffline().Data(), CONF_SOURCEOFFLINE);
  fTextSOffline->Associate(this);
  fTextSOffline->SetWidth(textLength);
  fOfflineS->AddFrame(fTextSOffline, fLayout3);
  TString downPath = Form("%s/src/images/arrowdown.xpm", getenv("SPDMOOD_SITE"));
  fBSourceDown = new TGPictureButton(fOfflineS,downPath.Data(),B_SOURCE_DOWN);
  fBSourceDown->SetToolTipText("Decrease...", 0);
  fBSourceDown->Associate(this);
  fOfflineS->AddFrame(fBSourceDown, fLayout3);
  TString upPath = Form("%s/src/images/arrowup.xpm", getenv("SPDMOOD_SITE"));
  fBSourceUp = new TGPictureButton(fOfflineS,upPath.Data(),B_SOURCE_UP);
  fBSourceUp->SetToolTipText("Increase...", 0);
  fBSourceUp->Associate(this);
  fOfflineS->AddFrame(fBSourceUp, fLayout3);
  fTop->AddFrame(fOfflineS, fLayout);



  fOfflineListSL = new TGCompositeFrame(this, 10,10, kHorizontalFrame);
  fOfflineListSLabel = new TGLabel(fOfflineListSL, "Offline File List Path:  [  ex:  /home/pixel/fileList.txt  ]");
  fOfflineListSL->AddFrame(fOfflineListSLabel, fLayout7);
  fTop->AddFrame(fOfflineListSL, fLayout5);

  fOfflineListS = new TGCompositeFrame(this, 10,10, kHorizontalFrame);
  fRadioFrame[2] = new TGCompositeFrame(fOfflineListS, 130, 10, kHorizontalFrame | kFixedWidth);
  fRadio[2]=new TGRadioButton(fRadioFrame[2], "Offline File List", CONF_OFFLINELIST);
  fRadioFrame[2]->AddFrame(fRadio[2],fLayout5);
  fRadio[2]->Associate(this); 
  fOfflineListS->AddFrame(fRadioFrame[2], fLayout3);
  fButtonFOfflineList = new TGPictureButton(fOfflineListS, fClient->GetPicture("folder_t.xpm"), BUTTON_FETCHOFFLINELIST);
  fButtonFOfflineList->SetToolTipText("Data file list file for OFF-LINE.", 0);
  fButtonFOfflineList->Associate(this);
  fOfflineListS->AddFrame(fButtonFOfflineList, fLayout3);
  fTextSOfflineList = new TGTextEntry(fOfflineListS, TMConfig::Instance()->GETdataSourceFullPathOfflineList().Data(), CONF_SOURCEOFFLINE);
  fTextSOfflineList->Associate(this);
  fTextSOfflineList->SetWidth(textLength);
  fOfflineListS->AddFrame(fTextSOfflineList, fLayout3);
  fTop->AddFrame(fOfflineListS, fLayout);



  fFormat = new TGButtonGroup(this,"Offline Data Format",kVerticalFrame);
  fRadioFormat[0]=new TGRadioButton(fFormat, "Date Format", CONF_DATEFORMAT);
  fRadioFormat[1]=new TGRadioButton(fFormat, "Root Format", CONF_ROOTFORMAT);
  fRadioFormat[0]->SetState(kButtonUp);
  fRadioFormat[1]->SetState(kButtonUp);
  if (TMConfig::Instance()->GETrootMode()) {
    fRadioFormat[1]->SetState(kButtonDown);
  }
  else {
    fRadioFormat[0]->SetState(kButtonDown);
  }
  AddFrame(fFormat,fLayout5);



  fMultTypeFrame = new TGCompositeFrame(this, 10,10, kHorizontalFrame);
  fMultipleType = new TGButtonGroup(fMultTypeFrame,"Type of files",kVerticalFrame);
  fRadioMultType[0]=new TGRadioButton(fMultipleType, "08000031156003.10.date", CONF_MULTTYPE0);
  fRadioMultType[1]=new TGRadioButton(fMultipleType, "run31156.000", CONF_MULTTYPE1);
  fMultipleType->Show();
  fMultTypeFrame->AddFrame(fMultipleType, fLayout);
  fRadioMultType[0]->SetState(kButtonUp);
  fRadioMultType[1]->SetState(kButtonUp);
  switch (TMConfig::Instance()->GETmultipleFileType()) {
  case kTRUE:
    fRadioMultType[0]->SetState(kButtonDown);
    break;
  case kFALSE:
    fRadioMultType[1]->SetState(kButtonDown);
    break;
  default:
    fRadioMultType[0]->SetState(kButtonDown);
    break;
  }
  AddFrame(fMultTypeFrame,fLayout5);



  fBottom = new TGCompositeFrame(this, 100, 100, kHorizontalFrame | kFixedSize);
  fBottom->SetHeight(60);
  fTextB[0] = new TGTextButton(fBottom, "Apply", BUTTON_APPLY);
  fTextB[0]->Associate(this);
  fTextB[1] = new TGTextButton(fBottom, "Close", BUTTON_CANCEL);
  fTextB[1]->Associate(this);
  fTextB[2] = new TGTextButton(fBottom, "Help", BUTTON_HELP);
  fTextB[2]->Associate(this);
  fBottom->AddFrame(fTextB[0], fLayout2);
  fBottom->AddFrame(fTextB[1], fLayout2);
  fBottom->AddFrame(fTextB[2], fLayout2);
  AddFrame(fBottom, fLayout2);




  fRadio[0]->SetState(kButtonUp);
  fRadio[1]->SetState(kButtonUp);
  fRadio[2]->SetState(kButtonUp);
  if (TMConfig::Instance()->GETdataSourceOnline()) {
    fRadio[0]->SetState(kButtonDown);
    fLastRadioState=0;
  }
  else {
    if (TMConfig::Instance()->GETdataSourceOfflineList()) {
      fRadio[2]->SetState(kButtonDown);
      fLastRadioState=2;
    }
    else {
      fRadio[1]->SetState(kButtonDown);
      fLastRadioState=1;
    }
  }

#ifndef WITH_DATE
  if (fRadio[0]->GetState() == kButtonDown) {
    fRadio[0]->SetState(kButtonUp);
    fRadio[1]->SetState(kButtonDown);
    fLastRadioState=1;
  }
  fRadio[0]->SetEnabled(kFALSE);
#endif





  CheckAndEnableText();



  fLetUserCloseWindow = kFALSE;
    
  SetWindowName("Data Source Definition");
  MapSubwindows();
  //Layout();
  Resize();
  MapWindow();


}
//___________________________________________________________________________________
TMConfigureDateSimple::~TMConfigureDateSimple() {
  // Destructor.
  delete fTop;
  delete fFormat;
  delete fBottom;
  delete fOnlineS;
  delete fOnlineSL;
  delete fOnlineSLabel;
  delete fTextSOnline;
  delete fButtonFOnline;
  delete fOfflineS;
  delete fOfflineListS;
  delete fOfflineSL;
  delete fOfflineListSL;
  delete fOfflineSLabel;
  delete fOfflineListSLabel;
  delete[] fRadio;
  delete[] fRadioFormat;
  delete fTextSOffline;
  delete fTextSOfflineList;
  delete fBSourceUp;
  delete fBSourceDown;
  delete fButtonFOffline;
  delete fButtonFOfflineList;
  delete fLayout;
  delete fLayout2;
  delete fLayout3;
  delete fLayout4;
  delete fLayout5;
  delete fLayout6;
  delete fLayout7;
  delete fLayout8;
  delete fLayout9;
  delete[] fTextB;
}
//___________________________________________________________________________________
void TMConfigureDateSimple::CheckAndEnableText() {
  if (kButtonDown == fRadio[0]->GetState()) {
    fButtonFOnline->SetEnabled(kTRUE);
    fTextSOnline->SetEnabled(kTRUE);
    fButtonFOffline->SetEnabled(kFALSE);
    fTextSOffline->SetEnabled(kFALSE);
    fButtonFOfflineList->SetEnabled(kFALSE);
    fTextSOfflineList->SetEnabled(kFALSE);
    fBSourceDown->SetEnabled(kFALSE);
    fBSourceUp->SetEnabled(kFALSE);
    fRadioFormat[0]->SetEnabled(kFALSE);
    fRadioFormat[1]->SetEnabled(kFALSE); 
    fRadioMultType[0]->SetEnabled(kFALSE);
    fRadioMultType[1]->SetEnabled(kFALSE);
    fLastRadioState=0;
  }
  else if (kButtonDown == fRadio[1]->GetState()) {
    fButtonFOnline->SetEnabled(kFALSE);
    fTextSOnline->SetEnabled(kFALSE);
    fButtonFOffline->SetEnabled(kTRUE);
    fTextSOffline->SetEnabled(kTRUE);
    fButtonFOfflineList->SetEnabled(kFALSE);
    fTextSOfflineList->SetEnabled(kFALSE);
    fBSourceDown->SetEnabled(kTRUE);
    fBSourceUp->SetEnabled(kTRUE);
    if (fLastRadioState==0) fRadioFormat[0]->SetEnabled(kTRUE);
    if (fLastRadioState==0) fRadioFormat[1]->SetEnabled(kTRUE);
    if (fLastRadioState==0) fRadioMultType[0]->SetEnabled(kTRUE);
    if (fLastRadioState==0) fRadioMultType[1]->SetEnabled(kTRUE);
    fLastRadioState=1;
  }
  else if (kButtonDown == fRadio[2]->GetState()) {
    fButtonFOnline->SetEnabled(kFALSE);
    fTextSOnline->SetEnabled(kFALSE);
    fButtonFOffline->SetEnabled(kFALSE);
    fTextSOffline->SetEnabled(kFALSE);
    fButtonFOfflineList->SetEnabled(kTRUE);
    fTextSOfflineList->SetEnabled(kTRUE);
    fBSourceDown->SetEnabled(kFALSE);
    fBSourceUp->SetEnabled(kFALSE);
    if (fLastRadioState==0) fRadioFormat[0]->SetEnabled(kTRUE);
    if (fLastRadioState==0) fRadioFormat[1]->SetEnabled(kTRUE);
    if (fLastRadioState==0) fRadioMultType[0]->SetEnabled(kTRUE);
    if (fLastRadioState==0) fRadioMultType[1]->SetEnabled(kTRUE);
    fLastRadioState=2;
  }
}
//___________________________________________________________________________________
void TMConfigureDateSimple::MakeAssignment()
{
  // Sets globals.
  TMConfig::Instance()->SETrootMode(kFALSE);

  if (kButtonDown == fRadio[0]->GetState()) {
    TMConfig::Instance()->SETdataSourceOnline(kTRUE);
    TMConfig::Instance()->SETdataSourceFullPath(fTextSOnline->GetText());
    // defaults:
    TMConfig::Instance()->SETmonitoringPolicy(1);
    TMConfig::Instance()->SETmultipleFiles(kFALSE);
    TMConfig::Instance()->SETdataSourceOfflineList(kFALSE);
  }
  else {
    TMConfig::Instance()->SETdataSourceOnline(kFALSE);
    // defaults:
    TMConfig::Instance()->SETmonitoringPolicy(1); //used to be 0, but for offline makes no difference
    TMConfig::Instance()->SETmultipleFiles(kTRUE);

    if (kButtonDown == fRadioFormat[1]->GetState()) {
      TMConfig::Instance()->SETrootMode(kTRUE);
    }
    if (kButtonDown == fRadio[2]->GetState()) {
      TMConfig::Instance()->SETdataSourceOfflineList(kTRUE);
    }
    else {
      TMConfig::Instance()->SETdataSourceOfflineList(kFALSE);
      TMConfig::Instance()->SETdataSourceFullPath(fTextSOffline->GetText());
    }
  }

  if (kButtonDown == fRadioMultType[0]->GetState())
    TMConfig::Instance()->SETmultipleFileType(kTRUE);
  else if (kButtonDown == fRadioMultType[1]->GetState())
    TMConfig::Instance()->SETmultipleFileType(kFALSE);

  TMConfig::Instance()->SETdataSourceFullPathOnline(fTextSOnline->GetText());
  TMConfig::Instance()->SETdataSourceFullPathOffline(fTextSOffline->GetText());
  TMConfig::Instance()->SETdataSourceFullPathOfflineList(fTextSOfflineList->GetText());
}
//___________________________________________________________________________________
void TMConfigureDateSimple::CloseWindow() {
  // Prevents usage of window manager close button.
  if (fLetUserCloseWindow == kFALSE) DontCallClose();
  else UnmapWindow();
}
//___________________________________________________________________________________
Bool_t TMConfigureDateSimple::ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2) {
  // Event handler function.
  //  printf("%d,%ld,%ld\n",GET_MSG(msg),parm1,parm2);
  switch(GET_MSG(msg)) {
  case kC_COMMAND :
    switch (GET_SUBMSG(msg)) {
    case kCM_RADIOBUTTON :
      {
	switch (parm1) {
	case CONF_ONLINE :
	  {
#ifdef WITH_DATE
	    fRadio[0]->SetState(kButtonDown);
#endif
	    fRadio[1]->SetState(kButtonUp);
	    fRadio[2]->SetState(kButtonUp);
	    CheckAndEnableText();
	    break;
	  }
	case CONF_OFFLINE :
	  {
#ifdef WITH_DATE
	    fRadio[0]->SetState(kButtonUp);
#endif
	    fRadio[1]->SetState(kButtonDown);
	    fRadio[2]->SetState(kButtonUp);
	    CheckAndEnableText();
	    break;
	  }
	case CONF_OFFLINELIST :
	  {
#ifdef WITH_DATE
	    fRadio[0]->SetState(kButtonUp);
#endif
	    fRadio[1]->SetState(kButtonUp);
	    fRadio[2]->SetState(kButtonDown);
	    CheckAndEnableText();
	    break;
	  }
	  break;
	}
      }
    case kCM_BUTTON :
      switch (parm1) {
      case BUTTON_APPLY :
	{
	  MakeAssignment();
	  fLetUserCloseWindow = kTRUE;
	  SendMessage(fSPD, MK_MSG(kC_COMMAND,kCM_BUTTON), B_LOGIN2, 0);
	  CloseWindow();
	  break;
	}
      case BUTTON_CANCEL :
	{
	  fLetUserCloseWindow = kTRUE;
	  SendMessage(fSPD, MK_MSG(kC_COMMAND,kCM_BUTTON), LOGIN_INTERRUPT, 0);
	  CloseWindow();
	  break;
	}
      case BUTTON_HELP :
	{
	  new TMAbout(gClient->GetRoot(), this, "REMOTESOURCE");
	  break;
	}
      case BUTTON_FETCHONLINE :
	{
	  printf("Option not supported!\n");
	  break;
	}
      case BUTTON_FETCHOFFLINE :
	{
	  TGFileInfo fi;
	  const Char_t *filetypesMO[] = {
	    "DATE Files",               "*.date",
	    "Root Files",               "*.root",
	    "Raw Files",                "*.raw",
	    "All Files",                "*.*",
	    0,                          0
	  };
	  fi.fFileTypes = filetypesMO;
	  new TGFileDialog(fClient->GetRoot(), this, kFDOpen, &fi);
	  fTextSOffline->Clear();
	  TString fileName = Form("%s", fi.fFilename);
	  if (fileName.CompareTo("(null)")==0) {
	    fTextSOffline->SetText(TMConfig::Instance()->GETdataSourceFullPathOffline().Data());
	  }
	  else {
	    fTextSOffline->SetText(fileName);
	  }
	  break;
	}
      case BUTTON_FETCHOFFLINELIST :
	{
	  TGFileInfo fi;
	  const Char_t *filetypesMO[] = {
	    "DATE Files",               "*.date",
	    "Root Files",               "*.root",
	    "Raw Files",                "*.raw",
	    "All Files",                "*.*",
	    0,                          0
	  };
	  fi.fFileTypes = filetypesMO;
	  new TGFileDialog(fClient->GetRoot(), this, kFDOpen, &fi);
	  fTextSOfflineList->Clear();
	  TString fileName = Form("%s", fi.fFilename);
	  if (fileName.CompareTo("(null)")==0) {
	    fTextSOfflineList->SetText(TMConfig::Instance()->GETdataSourceFullPathOfflineList().Data());
	  }
	  else {
	    fTextSOfflineList->SetText(fileName);
	  }
	  break;
	}
      case B_SOURCE_UP :
	{
	  IncrDataSource(1);
	  break;
	}
      case B_SOURCE_DOWN :
	{
	  IncrDataSource(-1);
	  break;
	}
      }
      break;
    }
  }
  return kTRUE;
}
//___________________________________________________________________________________
void TMConfigureDateSimple::IncrDataSource(Int_t amount) {
  TString old = Form("%s",fTextSOffline->GetText());
  TString newStr = "";

  if (kButtonDown == fRadioMultType[1]->GetState()) {
    Bool_t commafound=kFALSE;
    Int_t digitstart=-1;
    Int_t digitend=-1;
    Int_t i=0;
    while (i<old.Length()) {
      //      printf("%d: %c\n",i,old[i]);
      if (old[i]>=48 && old[i]<=57 && commafound==kFALSE) { // digit
	if (digitstart==-1) {
	  digitstart=i;
	}
      }
      else {
	if (old[i]=='.' && commafound==kFALSE) {
	  commafound=kTRUE;
	  if (digitstart>-1) {
	    digitend=i-1;
	    if (digitend - digitstart < 9) {
	      TString runNum = "";
	      Bool_t noZero=kFALSE;
	      for (Int_t j=digitstart; j<=digitend; j++) {
		if (old[j]==48 && noZero==kFALSE) {
		  newStr.Append(Form("%c",old[j]));
		}
		else {
		  noZero=kTRUE;
		  runNum.Append(Form("%c",old[j]));
		}
	      }
	      Int_t runNumber = atoi(runNum.Data());
	      runNumber+=amount;
	      newStr.Append(Form("%d",runNumber));
	    }
	    else {
	      for (Int_t j=digitstart; j<=digitend; j++) {
		newStr.Append(Form("%c",old[j]));
	      }
	    }
	  }
	}
	else {
	  if (digitstart>-1) {
	    digitend=i-1;
	    for (Int_t j=digitstart; j<=digitend; j++) {
	      newStr.Append(Form("%c",old[j]));
	    }
	  }
	}
	digitstart=-1;
	newStr.Append(Form("%c",old[i]));
      }
      i++;
    }
  }
  else {
    Bool_t commafound=kFALSE;
    Int_t digitstart=-1;
    Int_t digitend=-1;
    Int_t i=0;
    while (i<old.Length()) {
      //      printf("%d: %c\n",i,old[i]);
      if (old[i]>=48 && old[i]<=57 && commafound==kFALSE) { // digit
	if (digitstart==-1) {
	  digitstart=i;
	}
      }
      else {
	if (old[i]=='.' && commafound==kFALSE) {
	  commafound=kTRUE;
	  if (digitstart>-1) {
	    for (Int_t j=digitstart; j<digitstart+2; j++) {
	      newStr.Append(Form("%c",old[j]));
	    }
	    digitstart+=2;
	    digitend=i-4;
	    TString runNum = "";
	    Bool_t noZero=kFALSE;
	    for (Int_t j=digitstart; j<=digitend; j++) {
	      if (old[j]==48 && noZero==kFALSE) {
		newStr.Append(Form("%c",old[j]));
	      }
	      else {
		noZero=kTRUE;
		runNum.Append(Form("%c",old[j]));
	      }
	    }
	    Int_t runNumber = atoi(runNum.Data());
	    runNumber+=amount;
	    newStr.Append(Form("%d",runNumber));
	    if (i-3>=0) {
	      for (Int_t j=i-3; j<i; j++) {
		newStr.Append(Form("%c",old[j]));
	      }
	    }
	  }
	}
	else {
	  if (digitstart>-1) {
	    digitend=i-1;
	    for (Int_t j=digitstart; j<=digitend; j++) {
	      newStr.Append(Form("%c",old[j]));
	    }
	  }
	}
	digitstart=-1;
	newStr.Append(Form("%c",old[i]));
      }
      i++;
    }
  }

  fTextSOffline->SetText(newStr.Data());
}

