#include "TMConfigureEqActive.h"
#include "TMConfig.h"
#include "TMAbout.h"
#include "SPDMood.h"

ClassImp(TMConfigureEqActive)

//___________________________________________________________________________________
TMConfigureEqActive::TMConfigureEqActive(const TGWindow *p, const TGWindow *main) : TGTransientFrame(gClient->GetRoot(), main, 500, 400, 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,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, "Which equipments should be active:", kHorizontalFrame | kFixedSize);
  fTop->SetWidth(300);
  fTop->SetHeight(500);
  fTop->SetTitlePos(TGGroupFrame::kCenter);
  this->AddFrame(fTop, fLayout);

  fF[0] = new TGCompositeFrame(this, 10,10, kVerticalFrame);
  fTop->AddFrame(fF[0], fLayout5);
  for (UInt_t eq=0; eq<10; eq++) {
    UInt_t id = CHECK_0+eq;
    TString label = Form("Eq %d", eq);
    fCheck[eq] = new TGCheckButton(fF[0], label.Data(), id);
    if (TMConfig::Instance()->GETeqActive(eq)) fCheck[eq]->SetState(kButtonDown);
    else fCheck[eq]->SetState(kButtonUp);
    fF[0]->AddFrame(fCheck[eq], fLayout5);
  }


  fF[1] = new TGCompositeFrame(this, 10,10, kVerticalFrame);
  fTop->AddFrame(fF[1], fLayout5);
  for (UInt_t eq=10; eq<20; eq++) {
    UInt_t id = CHECK_0+eq;
    TString label = Form("Eq %d", eq);
    fCheck[eq] = new TGCheckButton(fF[1], label.Data(), id);
    if (TMConfig::Instance()->GETeqActive(eq)) fCheck[eq]->SetState(kButtonDown);
    else fCheck[eq]->SetState(kButtonUp);
    fF[1]->AddFrame(fCheck[eq], fLayout5);
  }


  fF[3] = new TGCompositeFrame(this, 100, 100, kHorizontalFrame);
  this->AddFrame(fF[3], fLayout);
  fTextBall[0] = new TGTextButton(fF[3], "Set All", BUTTON_SETALL);
  fTextBall[0]->Associate(this);
  fTextBall[1] = new TGTextButton(fF[3], "Clear All", BUTTON_CLEARALL);
  fTextBall[1]->Associate(this);
  fF[3]->AddFrame(fTextBall[0], fLayout2);
  fF[3]->AddFrame(fTextBall[1], fLayout2);



  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("Activate Equipments");
  MapSubwindows();
  Resize();
  MapWindow();
}

//___________________________________________________________________________________
TMConfigureEqActive::~TMConfigureEqActive()
{
  // Destructor.
  delete[] fF;
  delete fTop;
  delete fLayout;
  delete fLayout2;
  delete fLayout3;
  delete fLayout4;
  delete fLayout5;
  delete[] fCheck;
  delete[] fTextB;
  delete[] fTextBall;
}

//___________________________________________________________________________________
void TMConfigureEqActive::MakeAssignment()
{
  for (UInt_t eq=0; eq<20; eq++) {
    if (kButtonDown == fCheck[eq]->GetState()) { 
      TMConfig::Instance()->SETeqActive(eq,kTRUE);
    }
    else {
      TMConfig::Instance()->SETeqActive(eq,kFALSE);
    }
  }
}

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

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

//___________________________________________________________________________________
Bool_t TMConfigureEqActive::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), UPDATE_EQACTIVATE, 0);
	  CloseWindow();
	  break;
	}
      case BUTTON_CANCEL :
	{
	  fLetUserCloseWindow = kTRUE;
	  CloseWindow();
	  break;
	}
      case BUTTON_HELP :
	{
	  new TMAbout(gClient->GetRoot(), this, "README");
	  break;
	}
      case BUTTON_SETALL :
	{
	  for (UInt_t eq=0; eq<20; eq++) {
	    fCheck[eq]->SetState(kButtonDown);
	  }
	  break;
	}
      case BUTTON_CLEARALL :
	{
	  for (UInt_t eq=0; eq<20; eq++) {
	    fCheck[eq]->SetState(kButtonUp);
	  }
	  break;
	}
      }
      break;
    }
  }
  return kTRUE;
}
