#include "TMScreenShotAll.h"
#include "TMConfig.h"
#include "TMControlSPD.h"
#include "TMAbout.h"
#include "SPDMood.h"

ClassImp(TMScreenShotAll)

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

  for (UInt_t ty=0; ty<gNrFileTypes; ty++) {
    fRadio[ty] = NULL;
  }

  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 TGButtonGroup(this,"File Format",kVerticalFrame);

  AddRadioButton(fTop,TY_GIF,GetLabel(TY_GIF));
  AddRadioButton(fTop,TY_JPG,GetLabel(TY_JPG));
  AddRadioButton(fTop,TY_C,GetLabel(TY_C));
  AddRadioButton(fTop,TY_EPS,GetLabel(TY_EPS));
  AddRadioButton(fTop,TY_PDF,GetLabel(TY_PDF));

  fRadio[TY_GIF]->SetState(kButtonDown);
  fTop->Show();
  AddFrame(fTop, fLayout);



  fMid = new TGGroupFrame(this, "Local Directory", kVerticalFrame);
  fMid->SetTitlePos(TGGroupFrame::kCenter);
  AddFrame(fMid, fLayout);

  TString defaultval;

  fF[0] = new TGCompositeFrame(fMid, 10,10, kHorizontalFrame);
  fMid->AddFrame(fF[0], fLayout4);
  defaultval = Form("%s/images_%d",getenv("HOME"),runNb);
  fTextLocal = new TGTextEntry(fF[0], defaultval.Data(), TEXT_VAL2);
  fTextLocal->Associate(this);
  fTextLocal->SetWidth(400);
  fF[0]->AddFrame(fTextLocal, fLayout3);


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


  fLetUserCloseWindow = kFALSE;

  SetWindowName("Produce ScreenShots");
  MapSubwindows();
  Resize();
  MapWindow();
}

//___________________________________________________________________________________
TMScreenShotAll::~TMScreenShotAll() {
  // Destructor.
  delete[] fF;
  delete fTop;
  delete fMid;
  delete fTextLocal;
  delete fLayout;
  delete fLayout2;
  delete fLayout3;
  delete fLayout4;
  delete fLayout5;
  delete[] fTextB;
}
//___________________________________________________________________________________
void TMScreenShotAll::AddRadioButton(TGCompositeFrame* frame, Int_t id, const Char_t *label) {
  fRadio[id] = new TGRadioButton(frame, label, id);
  fRadio[id]->SetState(kButtonUp);
}
//___________________________________________________________________________________
const Char_t* TMScreenShotAll::GetLabel(Int_t id) {
  if      (id==TY_GIF) return "gif";
  else if (id==TY_JPG) return "jpg";
  else if (id==TY_C)   return "C";
  else if (id==TY_EPS) return "eps";
  else if (id==TY_PDF) return "pdf";
  else                 return "";
}
//___________________________________________________________________________________
void TMScreenShotAll::MakeAssignment() {
  TString localDir = "";
  localDir = fTextLocal->GetText();
  TMConfig::Instance()->SETuploadCommand(localDir.Data());
  TString makeDir = Form("mkdir %s > /dev/null",localDir.Data());
  system(makeDir.Data());

  for (UInt_t ty=0; ty<gNrFileTypes; ty++) {
    if (fRadio[ty] != NULL) {
      if (fRadio[ty]->GetState() == kButtonDown) {
	TMConfig::Instance()->SETuploadCommand2(GetLabel(ty));
	break;
      }
    }
  }
}
//___________________________________________________________________________________
void TMScreenShotAll::CloseWindow() {
  // Prevents usage of window manager close button.
  if (fLetUserCloseWindow == kFALSE) DontCallClose();
  else UnmapWindow();
}
//___________________________________________________________________________________
Bool_t TMScreenShotAll::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();
	  TMControlSPD::Instance()->ContinueRunning();
	  break;
	}
      case BUTTON_HELP :
	{
	  new TMAbout(gClient->GetRoot(), this, "NODOC");
	  break;
	}
      }

      break;
    }
  }
  return kTRUE;
}
