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

ClassImp(TMMovie)

//___________________________________________________________________________________
TMMovie::TMMovie(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(kLHintsTop | kLHintsExpandX,10,10,10,10);

  fTop = new TGGroupFrame(this, "Parameters for movie making:", kVerticalFrame);
  fTop->SetTitlePos(TGGroupFrame::kCenter);
  this->AddFrame(fTop, fLayout);

  fF[0] = new TGCompositeFrame(this, 10,10, kVerticalFrame);
  fTop->AddFrame(fF[0], fLayout5);
  TString defaultval = Form("%s",TMConfig::Instance()->GETmovieName().Data());
  fText1 = new TGTextEntry(fF[0], defaultval.Data(), TEXT_VAL1);
  fText1->Associate(this);
  fText1->SetWidth(300);
  fLabel1 = new TGLabel(fF[0], " Name of movie to produce (.mpg will be added):");
  fF[0]->AddFrame(fLabel1, fLayout);
  fF[0]->AddFrame(fText1, fLayout3);

  fF[3] = new TGCompositeFrame(this, 10,10, kHorizontalFrame);
  fTop->AddFrame(fF[3], fLayout4);
  defaultval = Form("%d",TMConfig::Instance()->GETmovieDelay());
  fText4 = new TGTextEntry(fF[3], defaultval.Data(), TEXT_VAL4);
  fText4->Associate(this);
  fText4->SetWidth(100);
  fLabel4 = new TGLabel(fF[3], " Movie delay value (1 = no delay): ");
  fF[3]->AddFrame(fLabel4, fLayout);
  fF[3]->AddFrame(fText4, fLayout3);

//!!!  fF[2] = new TGCompositeFrame(this, 10,10, kHorizontalFrame);
//!!!  fTop->AddFrame(fF[2], fLayout4);
//!!!  defaultval = Form("%d",TMConfig::Instance()->GETmovieTotal());
//!!!  fText3 = new TGTextEntry(fF[2], defaultval.Data(), TEXT_VAL3);
//!!!  fText3->Associate(this);
//!!!  fText3->SetWidth(100);
//!!!  fLabel3 = new TGLabel(fF[2], " Nr of images to create: ");
//!!!  fF[2]->AddFrame(fLabel3, fLayout);
//!!!  fF[2]->AddFrame(fText3, fLayout3);
//!!!
//!!!  fF[1] = new TGCompositeFrame(this, 10,10, kHorizontalFrame);
//!!!  fTop->AddFrame(fF[1], fLayout4);
//!!!  defaultval = Form("%d",TMConfig::Instance()->GETmovieRate());
//!!!  fText2 = new TGTextEntry(fF[1], defaultval.Data(), TEXT_VAL2);
//!!!  fText2->Associate(this);
//!!!  fText2->SetWidth(100);
//!!!  fLabel2 = new TGLabel(fF[1], " Make an image every x events: ");
//!!!  fF[1]->AddFrame(fLabel2, fLayout);
//!!!  fF[1]->AddFrame(fText2, fLayout3);



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



  fLetUserCloseWindow = kFALSE;
    
  SetWindowName("Make Movie Screen Shots");
  MapSubwindows();
  Resize();
  MapWindow();
}
//___________________________________________________________________________________
TMMovie::~TMMovie() {
  // Destructor.
  delete[] fF;
  delete fTop;
  delete fText1;
  //  delete fText2;
  //  delete fText3;
  delete fText4;
  delete fLayout;
  delete fLayout2;
  delete fLayout3;
  delete fLayout4;
  delete fLayout5;
  delete fLabel1;
  delete fLabel2;
  delete[] fTextB;
}
//___________________________________________________________________________________
void TMMovie::MakeAssignment() {
  // Sets globals.
  TMConfig::Instance()->SETmovieName(fText1->GetText());
  //  TMConfig::Instance()->SETmovieRate(atoi(fText2->GetText()));
  //  TMConfig::Instance()->SETmovieTotal(atoi(fText3->GetText()));
  TMConfig::Instance()->SETmovieDelay(atoi(fText4->GetText()));

}
//___________________________________________________________________________________
void TMMovie::CloseWindow() {
  // Prevents usage of window manager close button.
  if (fLetUserCloseWindow == kFALSE)
    DontCallClose();
  else
    //DestroyWindow();
    UnmapWindow();
}
//___________________________________________________________________________________
Bool_t TMMovie::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_INFO :
	{
	  new TMAbout(gClient->GetRoot(), this, "REMOTESOURCE");
	  break;
	}
      case BUTTON_APPLY :
	{
	  MakeAssignment();
	  fLetUserCloseWindow = kTRUE;
	  //	  SendMessage(fSPD, MK_MSG(kC_COMMAND,kCM_BUTTON), MOVIE_START, 0);
	  CloseWindow();
	  break;
	}
      case BUTTON_CANCEL :
	{
	  fLetUserCloseWindow = kTRUE;
	  CloseWindow();
	  break;
	}
      case BUTTON_HELP :
	{
	  new TMAbout(gClient->GetRoot(), this, "NODOC");
	  break;
	}
      }
      break;
    }
  }
  return kTRUE;
}
