#include "TMDeadNoisy.h"
#include "TMConfig.h"
#include "TMAbout.h"
#include "SPDMood.h"
#include <TGButton.h>

ClassImp(TMDeadNoisy)

//___________________________________________________________________________________
TMDeadNoisy::TMDeadNoisy(const TGWindow *p, const TGWindow *main, Int_t sendID) : TGTransientFrame(gClient->GetRoot(), main, 500, 400, kVerticalFrame) {
  fSPD = main;
  fSendID = sendID;

  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);

  fTop = new TGGroupFrame(this, "For each chip:", kVerticalFrame);
  fTop->SetTitlePos(TGGroupFrame::kCenter);
  this->AddFrame(fTop, fLayout);

  fF[0] = new TGCompositeFrame(this, 10,10, kHorizontalFrame);
  fTop->AddFrame(fF[0], fLayout5);
  Char_t defaultval[10];
  sprintf(defaultval,"%d",TMConfig::Instance()->GETdeadLimit());
  fText1 = new TGTextEntry(fF[0], defaultval, TEXT_VAL1);
  fText1->Associate(this);
  fText1->SetWidth(100);
  fLabel1 = new TGLabel(fF[0], " Find nr of dead pixels, with hits <= ");
  fF[0]->AddFrame(fLabel1, fLayout);
  fF[0]->AddFrame(fText1, fLayout3);

  fLabel3 = new TGLabel(fTop, "Noisy pixels will be masked automatically.");
  fTop->AddFrame(fLabel3, fLayout4);

  fF[1] = new TGCompositeFrame(this, 10,10, kHorizontalFrame);
  fTop->AddFrame(fF[1], fLayout4);
  //  Char_t defaultval[10];
  sprintf(defaultval,"%d",TMConfig::Instance()->GETnoisyLimit());
  fText2 = new TGTextEntry(fF[1], defaultval, TEXT_VAL2);
  fText2->Associate(this);
  fText2->SetWidth(100);
  fLabel2 = new TGLabel(fF[1], " Find nr of noisy pixels, with hits > ");
  fF[1]->AddFrame(fLabel2, fLayout);
  fF[1]->AddFrame(fText2, fLayout3);

  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], "Help", BUTTON_HELP);
  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("Get Nr of Dead and Noisy Pixels");
  MapSubwindows();
  Resize();
  MapWindow();
}
//___________________________________________________________________________________
TMDeadNoisy::~TMDeadNoisy() {
  delete[] fF;
  delete fTop;
  delete fText1;
  delete fText2;
  delete fLayout;
  delete fLayout2;
  delete fLayout3;
  delete fLayout4;
  delete fLayout5;
  delete fLabel1;
  delete fLabel2;
  delete[] fTextB;
}

//___________________________________________________________________________________
void TMDeadNoisy::MakeAssignment() {
  // Sets globals.
  TMConfig::Instance()->SETdeadLimit(atoi(fText1->GetText()));
  TMConfig::Instance()->SETnoisyLimit(atoi(fText2->GetText()));
}
//___________________________________________________________________________________
void TMDeadNoisy::CloseWindow() {
  // Prevents usage of window manager close button.
  if (fLetUserCloseWindow == kFALSE) DontCallClose();
  else UnmapWindow();
}
//___________________________________________________________________________________
Bool_t TMDeadNoisy::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_MENU), fSendID, 0);
	  CloseWindow();
	  break;
	}
      case BUTTON_CANCEL :
	{
	  fLetUserCloseWindow = kTRUE;
	  CloseWindow();
	  break;
	}
      case BUTTON_HELP :
	{
	  new TMAbout(gClient->GetRoot(), this, "NODOC");
	  break;
	}

      }
      break;
    }
  }
  return kTRUE;
}
