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

ClassImp(TMScreenShot)

//___________________________________________________________________________________
TMScreenShot::TMScreenShot(const TGWindow *p, const TGWindow *main, Int_t sendID, ULong_t runNb, const Char_t *windowName) : TGTransientFrame(gClient->GetRoot(), main, 500, 400, kVerticalFrame) {
  // Constructor.
  fSPD = main;
  fSendID = sendID;

  TString windowName2 = Form("%s",windowName);
  windowName2.ReplaceAll(' ','_');

  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, "Where do you want to save the image:", kVerticalFrame);
  fTop->SetTitlePos(TGGroupFrame::kCenter);
  this->AddFrame(fTop, fLayout);

  TString defaultval;

  fF[1] = new TGCompositeFrame(this, 10,10, kHorizontalFrame);
  fTop->AddFrame(fF[1], fLayout4);
  defaultval = Form("%s/image_%d_%s.gif",getenv("HOME"),runNb,windowName2.Data());
  fLabel = new TGLabel(fF[1], "Local File Name", LABEL_LOCAL);
  fTextLocal = new TGTextEntry(fF[1], defaultval.Data(), TEXT_VAL2);
  fTextLocal->Associate(this);
  fTextLocal->SetWidth(300);
  fF[1]->AddFrame(fLabel, fLayout);
  fF[1]->AddFrame(fTextLocal, 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("Produce ScreenShot");
  MapSubwindows();
  Resize();
  MapWindow();
}

//___________________________________________________________________________________
TMScreenShot::~TMScreenShot() {
  // Destructor.
  delete[] fF;
  delete fTop;
  delete fTextLocal;
  delete fLabel;
  delete fLayout;
  delete fLayout2;
  delete fLayout3;
  delete fLayout4;
  delete fLayout5;
  delete[] fTextB;
}

//___________________________________________________________________________________
void TMScreenShot::MakeAssignment() {
  TString localFile = "";
  localFile = fTextLocal->GetText();
  TMConfig::Instance()->SETuploadCommand(localFile.Data());
}
//___________________________________________________________________________________
void TMScreenShot::CloseWindow() {
  // Prevents usage of window manager close button.
  if (fLetUserCloseWindow == kFALSE) DontCallClose();
  else UnmapWindow();
}
//___________________________________________________________________________________
Bool_t TMScreenShot::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_HELP :
	{
	  new TMAbout(gClient->GetRoot(), this, "NODOC");
	  break;
	}
      }

      break;
    }
  }
  return kTRUE;
}
