#include "TMConfigureMood.h"
#include "TMAbout.h"
#include "SPDMood.h"
#include <TGFileDialog.h>

ClassImp(TMConfigureMood)

//___________________________________________________________________________________
TMConfigureMood::TMConfigureMood(const TGWindow *p, const TGWindow *main) : TGTransientFrame(gClient->GetRoot(), main, 500, 400, kVerticalFrame)
{
  // Constructor

  fLa = new TGLayoutHints(kLHintsTop | kLHintsLeft, 1, 1, 1, 1);
  fLb = new TGLayoutHints(kLHintsRight | kLHintsExpandX, 1,1,1,1);
  fLc = new TGLayoutHints(kLHintsRight, 1,1,1,1);
  fLd = new TGLayoutHints(kLHintsLeft, 1,1,1,1);
  fLe = new TGLayoutHints(kLHintsBottom | kLHintsLeft | kLHintsExpandX, 1,1,1,1);

  fGroup = new TGGroupFrame(this, "MOOD Configuration");
  fGroup->SetTitlePos(TGGroupFrame::kCenter);
  this->AddFrame(fGroup, fLa);

  const Char_t *moodGroupLabels[]={
    "eventSizeDistroMin",
    "eventSizeDistroMax",
    "eventSizeDistroBins",
    "handleEventMin",
    "handleEventMax"
  };
  Int_t initialValues[] = {
    //    o.eventSizeDistroMin,
    //    o.eventSizeDistroMax,
    //    o.eventSizeDistroBins,
    o.handleEventMin,
    o.handleEventMax
  };

  for (Int_t i=0 ; i<2 ; i++) {
    fF[i] = new TGCompositeFrame(fGroup, 100,10, kHorizontalFrame);
    fGroup->AddFrame(fF[i], fLb);
    fLabel[i] = new TGLabel(fF[i], moodGroupLabels[i]);
    fF[i]->AddFrame(fLabel[i], fLd);
    fNumber[i] = new TGNumberEntry(fF[i], initialValues[i]);
    fNumber[i]->SetWidth(100);
    fF[i]->AddFrame(fNumber[i], fLc);
  }
	
  fF[8] = new TGCompositeFrame(fGroup, 100, 100, kHorizontalFrame);
  fGroup->AddFrame(fF[8], fLb);
  fLabel[6] = new TGLabel(fF[8], "MOOD Version Flag");
  fNumber[6] = new TGNumberEntry(fF[8], o.moodVersionFlag, 5, -1, TGNumberFormat::kNESHex);
  fNumber[6]->SetWidth(100);
  fF[8]->AddFrame(fNumber[6], fLc);
  fF[8]->AddFrame(fLabel[6], fLd);
	
  fF[9] = new TGCompositeFrame(fGroup, 100, 100, kHorizontalFrame);
  fGroup->AddFrame(fF[9], fLb);
  fLabel[7] = new TGLabel(fF[9], "Monitoring Level");
  fF[9]->AddFrame(fLabel[7], fLd);
//  fCombo = new TGComboBox(fF[9], C_COMBO);
//	
//  fCombo->AddEntry("Run on LDC", C_LDC);
//  fCombo->AddEntry("Run on GDC", C_GDC);
//  fCombo->AddEntry("Non !..", C_NON);
//  fCombo->Resize(100,23);
//  Char_t dummy[3][5];
//  sprintf(dummy[0], "kLDC");
//  sprintf(dummy[1], "kGDC");
//  sprintf(dummy[2], "kNON");
//  if (0 == strcmp(o.monitoringLevel, dummy[0])) fCombo->Select(C_LDC);
//  else if (0 == strcmp(o.monitoringLevel, dummy[1])) fCombo->Select(C_GDC);
//  else if (0 == strcmp(o.monitoringLevel, dummy[2])) fCombo->Select(C_NON);
//  else fCombo->Select(C_NON);
//  fF[9]->AddFrame(fCombo, fLc);

  fF[5] = new TGCompositeFrame(fGroup, 100,100, kHorizontalFrame);
  fGroup->AddFrame(fF[5], fLb);
  fF[6] = new TGCompositeFrame(fGroup, 100,100, kHorizontalFrame);
  fGroup->AddFrame(fF[6], fLb);
  fText[0] = new TGTextEntry(fF[5], o.logFileFullPath, CONF_LOG);
  fText[0]->Associate(this);
//  fText[1] = new TGTextEntry(fF[6], o.confFileFullPath, CONF_CONF);
//  fText[1]->Associate(this);
  fF[5]->AddFrame(fText[0], fLb);
  //  fF[6]->AddFrame(fText[1], fLb);
  fButton[0] = new TGPictureButton(fF[5], fClient->GetPicture("folder_t.xpm"), CONF_LOG);
  fButton[0]->SetToolTipText("Point logging file",0);
  fButton[0]->Associate(this);
  fButton[1] = new TGPictureButton(fF[6], fClient->GetPicture("folder_t.xpm"), CONF_CONF);
  fButton[1]->SetToolTipText("Point configuration file",0);
  fButton[1]->Associate(this);
  fButton[2] = new TGPictureButton(fF[5], fClient->GetPicture("app_t.xpm"), CONF_OPEN);
  fButton[2]->SetToolTipText("Open logging file",0);
  fButton[2]->Associate(this);
  fButton[3] = new TGPictureButton(fF[6], fClient->GetPicture("app_t.xpm"), CONF_OPEN2);
  fButton[3]->SetToolTipText("Show configuration",0);
  fButton[3]->Associate(this);
    
  fF[5]->AddFrame(fButton[0], fLc);
  fF[6]->AddFrame(fButton[1], fLc);	
  fF[5]->AddFrame(fButton[2], fLc);	
  fF[6]->AddFrame(fButton[3], fLc);	

  fF[7] = new TGCompositeFrame(this, 100, 100, kHorizontalFrame);
  this->AddFrame(fF[7], fLe);
  fTextB[0] = new TGTextButton(fF[7], "Apply", CONF_APPLY);
  fTextB[0]->Associate(this);
  fTextB[1] = new TGTextButton(fF[7], "Close", CONF_CLOSE);
  fTextB[1]->Associate(this);
  fTextB[2] = new TGTextButton(fF[7], "Help", CONF_HELP);
  fTextB[2]->Associate(this);
  fF[7]->AddFrame(fTextB[0], fLe);
  fF[7]->AddFrame(fTextB[1], fLe);
  fF[7]->AddFrame(fTextB[2], fLe);

  const Char_t *moodGroupLabels_2[]={
    "Events for runNb",
    "Events for nbInRun"
  };
  Int_t initialValues_2[] = {
    o.aa_runNb_,
    o.aa_nbInRun_
  };
  fSearch = new TGGroupFrame(this, "Search");
  fSearch->SetTitlePos(TGGroupFrame::kCenter);
  this->AddFrame(fSearch, fLb);
  for (Int_t i=8 ; i<10 ; i++) {
    fF[i+2] = new TGCompositeFrame(fSearch, 100,10, kHorizontalFrame);
    fSearch->AddFrame(fF[i+2], fLb);
    fLabel[i+2] = new TGLabel(fF[i+2], moodGroupLabels_2[i-8]);
    fF[i+2]->AddFrame(fLabel[i+2], fLd);
    fNumber[i+2] = new TGNumberEntry(fF[i+2], initialValues_2[i-8]);
    fNumber[i+2]->SetWidth(100);
    fF[i+2]->AddFrame(fNumber[i+2], fLc);
  }
    
  fInconsistent = new TGGroupFrame(this, "Inconsistent Events");
  fInconsistent->SetTitlePos(TGGroupFrame::kCenter);
  this->AddFrame(fInconsistent, fLb);
  fComposit[0] = new TGCompositeFrame(fInconsistent, 100,10, kHorizontalFrame);
  fInconsistent->AddFrame(fComposit[0], fLb);
  fCheck[0] = new TGCheckButton(fComposit[0], "Dump, if any");
  fComposit[0]->AddFrame(fCheck[0], fLb);
  fCheck[1] = new TGCheckButton(fComposit[0], "Alert before dump");
  fComposit[0]->AddFrame(fCheck[1], fLb);
        
  if (kTRUE == o.dumpInconsistent) {
    fCheck[0]->SetState(kButtonDown);
    printf("o.dumpInconsistent = %d\n", o.dumpInconsistent);
  } else {
    fCheck[0]->SetState(kButtonUp);
    printf("o.dumpInconsistent = %d\n", o.dumpInconsistent);
  }
            
  if (kTRUE == o.alerBeforeDumping) {
    fCheck[1]->SetState(kButtonDown);
    printf("o.alerBeforeDumping = %d\n", o.alerBeforeDumping);
  } else {
    fCheck[1]->SetState(kButtonUp);
    printf("o.alerBeforeDumping = %d\n", o.alerBeforeDumping);
  } 

  fLetUserCloseWindow = kFALSE;

  SetWindowName("MOOD Configuration");
  MapSubwindows();
  Resize();
  MapWindow();
}

//___________________________________________________________________________________
void TMConfigureMood::CloseWindow()
{
  // Prevents window manager close window function.

  if (fLetUserCloseWindow == kFALSE)
    DontCallClose();
  else
    //DestroyWindow();
    UnmapWindow();
}

//___________________________________________________________________________________
TMConfigureMood::~TMConfigureMood()
{
  // Destructor.

  delete[] fNumber;
  delete[] fF;
  delete[] fLabel;
  delete[] fLabelIn;
  delete[] fText;
  delete[] fButton;
  delete[] fCheck;
  delete[] fComposit;
  delete fLa;
  delete fLb;
  delete fLc;
  delete fLd;
  delete fLe;
  delete fGroup;
  delete fInconsistent;
  delete fSearch;
  delete[] fTextB;
  //  delete fCombo;
}

//___________________________________________________________________________________
void TMConfigureMood::MakeAssignment()
{
  // Assigns globals related to MOOD.

  o.moodVersionFlag		= fNumber[6]->GetIntNumber();
//  o.eventSizeDistroMin    = fNumber[0]->GetIntNumber();
//  o.eventSizeDistroMax    = fNumber[1]->GetIntNumber();
//  o.eventSizeDistroBins   = fNumber[2]->GetIntNumber();
  o.handleEventMin        = fNumber[3]->GetIntNumber();
  o.handleEventMax        = fNumber[4]->GetIntNumber();
  o.aa_runNb_                 = fNumber[10]->GetIntNumber();
  o.aa_nbInRun_               = fNumber[11]->GetIntNumber();
        
  o.logFileFullPath = Form("%s", fText[0]->GetText());
  //  snprintf(o.logFileFullPath, sizeof(o.logFileFullPath), "%s", fText[0]->GetText());
  //  snprintf(o.confFileFullPath, sizeof(o.confFileFullPath), "%s", fText[1]->GetText());
	
//  Char_t dummy[3][4];
//  sprintf(dummy[0], "kLDC");
//  sprintf(dummy[1], "kGDC");
//  sprintf(dummy[2], "kNON");
  //	if (C_LDC == fCombo->GetSelected()) o.monitoringLevel = dummy[0];
  //	else if (C_GDC == fCombo->GetSelected()) o.monitoringLevel = dummy[1];
  //	else o.monitoringLevel = dummy[2];
//  if (C_LDC == fCombo->GetSelected()) snprintf(o.monitoringLevel, 5, "%s", dummy[0]);
//  else if (C_GDC == fCombo->GetSelected()) snprintf(o.monitoringLevel, 5, "%s", dummy[1]);
//  else snprintf(o.monitoringLevel, 5, "%s", dummy[2]);
    
  if (kButtonUp == fCheck[0]->GetState()) o.dumpInconsistent = kFALSE;
  else o.dumpInconsistent = kTRUE;
  if (kButtonUp == fCheck[1]->GetState()) o.alerBeforeDumping = kFALSE;
  else o.alerBeforeDumping = kTRUE;
}

//___________________________________________________________________________________
Bool_t TMConfigureMood::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 CONF_APPLY :
	{
	  fLetUserCloseWindow = kTRUE;
	  MakeAssignment();
	  break;
	}
      case CONF_CLOSE :
	{
	  fLetUserCloseWindow = kTRUE;
	  CloseWindow();
	  break;
	}
      case CONF_HELP :
	{
	  new TMAbout(gClient->GetRoot(), this, "README");
	  break;
	}
      case CONF_LOG :
	{
	  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);
	  fText[0]->Clear();
	  Char_t fileName[100];
	  snprintf(fileName, sizeof(fileName), "%s", fi.fFilename);
	  fText[0]->SetText(fileName);
	  break;
	}
      case CONF_CONF :
	{
	  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);
	  fText[1]->Clear();
	  Char_t fileName[100];
	  snprintf(fileName, sizeof(fileName), "%s", fi.fFilename);
	  fText[1]->SetText(fileName);
	  break;
	}
      case CONF_OPEN :
	{
	  new TMAbout(gClient->GetRoot(), this, "LOG");
	  break;
	}
      case CONF_OPEN2 :
	{
	  new TMAbout(gClient->GetRoot(), this, "SHOWGLOBALS");
	  break;
	}
      }
    }
  }
  return kTRUE;
}
