#include "TMTrigMask.h"
#include "TMHistogramming.h"
#include "TMHistogrammingList.h"
#include "TMConfig.h"
#include "TMAbout.h"

ClassImp(TMTrigMask)

//___________________________________________________________________________________
TMTrigMask::TMTrigMask(const TGWindow *p, const TGWindow *main, TMHistogramming* hist, Int_t sendID) : TGTransientFrame(gClient->GetRoot(), main, 500, 400, kVerticalFrame) {
  // Constructor.
  fSPD = main;
  fSendID = sendID;
  fHist = hist;

  fLayout  = new TGLayoutHints(kLHintsCenterY | kLHintsExpandX,1,1,1,1);
  fLayout2 = new TGLayoutHints(kLHintsBottom | kLHintsExpandX,1,1,1,1);
  fLayout3 = new TGLayoutHints(kLHintsCenterY,10,10,10,10);
  fLayout4 = new TGLayoutHints(kLHintsBottom | kLHintsExpandX,10,10,10,10);
  fLayout5 = new TGLayoutHints(kLHintsTop | kLHintsExpandX,10,10,10,10);

  TString defaultval;

  fF[0] = new TGCompositeFrame(this, 10,10, kHorizontalFrame);
  this->AddFrame(fF[0], fLayout);
  fLabel0 = new TGLabel(fF[0], "Trig Mask [0]");
  if (fHist!=NULL) {
    defaultval = Form("0x%x",fHist->GetTriggerMask(0));
  }
  else {
    defaultval = Form("0x%x",TMConfig::Instance()->GETtriggerMask0());
  }
  fText0 = new TGTextEntry(fF[0], defaultval.Data(), TEXT_VAL0);
  fText0->Associate(this);
  fText0->SetWidth(300);
  fF[0]->AddFrame(fLabel0, fLayout);
  fF[0]->AddFrame(fText0, fLayout);

  fF[1] = new TGCompositeFrame(this, 10,10, kHorizontalFrame);
  this->AddFrame(fF[1], fLayout);
  fLabel1 = new TGLabel(fF[1], "Trig Mask [1]");
  if (fHist!=NULL) {
    defaultval = Form("0x%x",fHist->GetTriggerMask(1));
  }
  else {
    defaultval = Form("0x%x",TMConfig::Instance()->GETtriggerMask1());
  }
  fText1 = new TGTextEntry(fF[1], defaultval.Data(), TEXT_VAL1);
  fText1->Associate(this);
  fText1->SetWidth(300);
  fF[1]->AddFrame(fLabel1, fLayout);
  fF[1]->AddFrame(fText1, fLayout);

  fF[2] = new TGCompositeFrame(this, 100, 100, kHorizontalFrame);
  this->AddFrame(fF[2], fLayout);
  fTextB[0] = new TGTextButton(fF[2], "Apply", BUTTON_APPLY);
  fTextB[0]->Associate(this);
  fTextB[1] = new TGTextButton(fF[2], "Close", BUTTON_CANCEL);
  fTextB[1]->Associate(this);
  fTextB[2] = new TGTextButton(fF[2], "Activate All", BUTTON_ACT_ALL);
  fTextB[2]->Associate(this);
  fF[2]->AddFrame(fTextB[0], fLayout2);
  fF[2]->AddFrame(fTextB[1], fLayout2);
  fF[2]->AddFrame(fTextB[2], fLayout2);

  fLetUserCloseWindow = kFALSE;

  SetWindowName("Set Trigger Mask");
  MapSubwindows();
  Resize();
  MapWindow();
}
//___________________________________________________________________________________
TMTrigMask::~TMTrigMask() {
  // Destructor.
  delete[] fF;
  delete fText0;
  delete fText1;
  delete fLabel0;
  delete fLabel1;
  delete fLayout;
  delete fLayout2;
  delete fLayout3;
  delete fLayout4;
  delete fLayout5;
  delete[] fTextB;
}
//___________________________________________________________________________________
void TMTrigMask::MakeAssignment() {
  UInt_t trigMask0 = strtoul(fText0->GetText(),NULL,0);
  UInt_t trigMask1 = strtoul(fText1->GetText(),NULL,0);
  if (fHist!=NULL) {
    fHist->SetTriggerMask(trigMask0,trigMask1);
  }
  else {
    // set for all histos in list...
    TMHistogrammingList::Instance()->SetTriggerMaskAll(trigMask0,trigMask1);
    TMConfig::Instance()->SETtriggerMask0(trigMask0);
    TMConfig::Instance()->SETtriggerMask1(trigMask1);
  }
}
//___________________________________________________________________________________
void TMTrigMask::CloseWindow() {
  // Prevents usage of window manager close button.
  if (fLetUserCloseWindow == kFALSE) DontCallClose();
  else UnmapWindow();
}
//___________________________________________________________________________________
Bool_t TMTrigMask::ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2) {
  // Event handler function.
  switch(GET_MSG(msg)) {
  case kC_COMMAND :
    switch (GET_SUBMSG(msg)) {
    case kCM_BUTTON :
      switch (parm1) {

      case BUTTON_APPLY :
	{
	  MakeAssignment();
	  fLetUserCloseWindow = kTRUE;
	  SendMessage(fSPD, MK_MSG(kC_COMMAND,kCM_BUTTON), fSendID, 0);
	  CloseWindow();
	  break;
	}
      case BUTTON_CANCEL :
	{
	  fLetUserCloseWindow = kTRUE;
	  CloseWindow();
	  break;
	}
      case BUTTON_ACT_ALL :
	{
	  //	  new TMAbout(gClient->GetRoot(), this, "NODOC");
	  fText0->SetText("0xffffffff");
	  fText1->SetText("0xffffffff");
	  break;
	}
      }

      break;
    }
  }
  return kTRUE;
}
