#include "TMSetMask.h"
#include "TMControlSPD.h"
#include <TGButton.h>

ClassImp(TMSetMask)

//___________________________________________________________________________________
TMSetMask::TMSetMask(const TGWindow *p, const TGWindow *main, Bool_t mask) : TGTransientFrame(gClient->GetRoot(), main, 600, 400, kVerticalFrame) {
  // Constructor.
  fSPD = main;
  bMask=mask;

  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(kLHintsExpandY | kLHintsExpandX,10,10,10,10);

  fTop = new TGGroupFrame(this, "Empty left field = all possible values.   Empty right field = left field value used", kVerticalFrame | kFixedSize);
  fTop->SetWidth(500);
  fTop->SetHeight(300);
  fTop->SetTitlePos(TGGroupFrame::kCenter);
  this->AddFrame(fTop, fLayout);

  fF[0] = new TGCompositeFrame(this, 10,10, kVerticalFrame);
  fTop->AddFrame(fF[0], fLayout5);
  TString labSTR[5];
  labSTR[0] = " Equipment ";
  labSTR[1] = " Half-Stave ";
  labSTR[2] = " Pixel Chip ";
  labSTR[3] = " Pixel Column ";
  labSTR[4] = " Pixel Row ";
  for (UInt_t entryN=0; entryN<5; entryN++) {
    fFrame[entryN] = new TGCompositeFrame(fF[0], 10,10, kHorizontalFrame);

    fText[entryN] = new TGTextEntry(fFrame[entryN], "0", TEXT_VAL0+entryN);
    fText[entryN]->Associate(this);
    fText[entryN]->SetWidth(100);
    fLabel[entryN] = new TGLabel(fFrame[entryN], labSTR[entryN].Data());

    fTextE[entryN] = new TGTextEntry(fFrame[entryN], "", TEXT_VALE0+entryN);
    fTextE[entryN]->Associate(this);
    fTextE[entryN]->SetWidth(100);
    fLabelE[entryN] = new TGLabel(fFrame[entryN], "to");

    fFrame[entryN]->AddFrame(fLabel[entryN], fLayout);
    fFrame[entryN]->AddFrame(fText[entryN], fLayout3);
    fFrame[entryN]->AddFrame(fLabelE[entryN], fLayout);
    fFrame[entryN]->AddFrame(fTextE[entryN], fLayout3);
    fF[0]->AddFrame(fFrame[entryN], fLayout5);
  }


  fF[1] = new TGCompositeFrame(this, 100, 100, kHorizontalFrame);
  this->AddFrame(fF[1], fLayout);
  fTextB[0] = new TGTextButton(fF[1], "Set", BUTTON_APPLY);
  fTextB[0]->Associate(this);
  fTextB[1] = new TGTextButton(fF[1], "Close", BUTTON_CLOSE);
  fTextB[1]->Associate(this);
  fTextB[2] = new TGTextButton(fF[1], "Clear", BUTTON_CLEAR);
  fTextB[2]->Associate(this);
  fF[1]->AddFrame(fTextB[0], fLayout2);
  fF[1]->AddFrame(fTextB[1], fLayout2);
  fF[1]->AddFrame(fTextB[2], fLayout2);


  fLetUserCloseWindow = kFALSE;
    
  if (bMask==kTRUE) SetWindowName("Mask Pixels");
  else SetWindowName("Un-Mask Pixels");
  MapSubwindows();
  Resize();
  MapWindow();
}

//___________________________________________________________________________________
TMSetMask::~TMSetMask()
{
  // Destructor.
  delete fLayout;
  delete fLayout2;
  delete fLayout3;
  delete fLayout4;
  delete fLayout5;
  delete[] fF;
  delete fTop;
  delete[] fText;
  delete[] fTextE;
  delete[] fLabel;
  delete[] fLabelE;
  delete[] fTextB;
}

//___________________________________________________________________________________
void TMSetMask::MakeAssignment()
{
  UInt_t valS[5];
  UInt_t valE[5];
  for (UInt_t i=0; i<5; i++) {
    TString start = fText[i]->GetText();
    if (start.CompareTo("")==0) {
      valS[i]=0;
      valE[i]=300;
    }
    else {
      valS[i]=atoi(fText[i]->GetText());
      if (valS[i]<0) return;

      TString end = fTextE[i]->GetText();
      if (end.CompareTo("")==0) {
	valE[i]=valS[i];
      }
      else {
	valE[i]=atoi(fTextE[i]->GetText());
	if (valE[i]<0) return;
      }
    }
  }
  TMControlSPD::Instance()->SetMask(valS[0],valE[0],valS[1],valE[1],valS[2],valE[2],valS[3],valE[3],valS[4],valE[4],bMask);
  TMControlSPD::Instance()->UpdateMask();
}

//___________________________________________________________________________________
void TMSetMask::CloseWindow()
{
  // Prevents usage of window manager close button.

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

//___________________________________________________________________________________
Bool_t TMSetMask::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();
	  break;
	}
      case BUTTON_CLOSE :
	{
	  fLetUserCloseWindow = kTRUE;
	  CloseWindow();
	  break;
	}
      case BUTTON_CLEAR :
	{
	  ClearFields();
	  break;
	}
      }
      break;
    }
  }
  return kTRUE;
}
//___________________________________________________________________________________
void TMSetMask::ClearFields() {
  for (UInt_t entryN=0; entryN<5; entryN++) {
    fText[entryN]->SetText("0");
    fTextE[entryN]->SetText("");
  }
}
