#include "TMDisplayingHitMapEq.h"
#include "TMDisplayingList.h"
#include "TMHistogrammingHitMapEq.h"
#include "TMHistogrammingList.h"
#include "TMMainFrame.h"
#include "TMControlSPD.h"
#include "TMControlMutex.h"
#include "SPDMood.h"
#include <TH2F.h>
#include <TCanvas.h>

ClassImp(TMDisplayingHitMapEq)

extern pthread_mutex_t gMutex;

//_____________________________________________________________________
TMDisplayingHitMapEq::TMDisplayingHitMapEq(const Char_t *frameName, const TGWindow *p, UInt_t width, UInt_t height, TMHistogramming *hist, UInt_t id, UInt_t plotNr, UInt_t cloneId) :
  TMDisplaying(frameName, p, width, height, hist, id, plotNr, cloneId)
{
//  if (!(TMControlSPD::Instance()->IsRunning())) {
    TCanvas *canv = fTheCanvas->GetCanvas();
    canv->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", "TMDisplayingHitMapEq", this, "ExecuteEvent(Int_t,Int_t,Int_t,TObject*)");
//  }
}
//_____________________________________________________________________
TMDisplayingHitMapEq::~TMDisplayingHitMapEq() 
{}
//_____________________________________________________________________
void TMDisplayingHitMapEq::ExecuteEvent(Int_t event, Int_t px, Int_t py, TObject *sel) {
  if (event==kButton1Double) {
  //  if (event==kButton1Double || event==kButton1Motion) {
    if (fPleaseWait) {
      time_t stamp = time(NULL);
      TString stampMsg = Form("%s",ctime(&stamp));
      stampMsg.ReplaceAll("\n","");
      printf("Busy - Please try again... (Time %s)\n",stampMsg.Data());
      return;
    }
    fPleaseWait = kTRUE;
    //    printf("double 1\n");
    
    Bool_t ihavelock = TMControlMutex::Instance()->DoIHaveLock();
    if (!ihavelock) {
      pthread_mutex_lock(&gMutex);
      //      printf("TMDispHitMapEq::ExecEve");
      TMControlMutex::Instance()->ITakeLock();
    }

    fTheCanvas->Disconnect();

//    printf("double 1.5\n");

    TCanvas *c = (TCanvas *) gTQSender;
    TPad *pad = (TPad *) c->GetSelectedPad();
    if (!pad) {
      fTheCanvas->Connect();
      if (!ihavelock) {
	TMControlMutex::Instance()->IReleaseLock();
	pthread_mutex_unlock(&gMutex);
      }
      fPleaseWait = kFALSE;
      return;
    }

    //    printf("double 2\n");

    Float_t x = pad->AbsPixeltoX(px);
    Float_t y = pad->AbsPixeltoY(py);
    x = pad->PadtoX(x);
    y = pad->PadtoY(y);

    //    printf("double 3\n");

    TH2F *hitMap = ((TMHistogrammingHitMapEq*)fHist)->GetHitMap();
    Int_t binx = hitMap->GetXaxis()->FindBin(x);
    Int_t biny = hitMap->GetYaxis()->FindBin(y);
    //    printf("binx=%d , biny=%d\n",binx,biny);
    
    //    printf("double 4\n");

    Int_t hs = ((256*6-1)-biny)/256;
    if (hs>5) {
      fTheCanvas->Connect();
      if (!ihavelock) {
	TMControlMutex::Instance()->IReleaseLock();
	pthread_mutex_unlock(&gMutex);
      }
      fPleaseWait = kFALSE;
      return;
    }
    Int_t chip = binx/32;
    if (chip>9) {
      fTheCanvas->Connect();
      if (!ihavelock) {
	TMControlMutex::Instance()->IReleaseLock();
	pthread_mutex_unlock(&gMutex);
      }
      fPleaseWait = kFALSE;
      return;
    }
    UInt_t eq = ((TMHistogrammingHitMapEq*)fHist)->GetEq();
    
    //    printf("double 5\n");

    // let's open a display for this chip
    UInt_t displayID = TMSetupDisplays::GetChipID(eq,hs,chip);
    //    TMMainFrame::Instance()->ProcessOpenDisplay(displayID,fPlotNr,fCloneId,((TMHistogrammingHitMapEq*)fHist)->GetHistChip(hs,chip));
    TMMainFrame::Instance()->ProcessOpenDisplay(displayID);

    //    printf("double 6\n");


    fTheCanvas->Connect();
    if (!ihavelock) {
      TMControlMutex::Instance()->IReleaseLock();
      pthread_mutex_unlock(&gMutex);
    }
    fPleaseWait = kFALSE;
  }
}
//_____________________________________________________________________
void TMDisplayingHitMapEq::ProcessReset() {
  TMControlSPD::Instance()->HoldRunning();
  fHist->Reset();
  Update();
  // reset also the chip histos:
  UInt_t eq = ((TMHistogrammingHitMapEq*)fHist)->GetEq();
  for (UInt_t hs=0; hs<6; hs++) {
    for (UInt_t chip=0; chip<10; chip++) {
      UInt_t displayID = TMSetupDisplays::GetChipID(eq,hs,chip);
      TMHistogrammingList::Instance()->ResetID(displayID,fPlotNr);
      TMDisplayingList::Instance()->UpdateDisplayID(displayID,fPlotNr);
    }
  }
  TMControlSPD::Instance()->ContinueRunning();
}
//______________________________________________________________________
void TMDisplayingHitMapEq::ProcessStartRun() {
//  TCanvas *canv = fTheCanvas->GetCanvas();
//  canv->Disconnect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)");
}
//______________________________________________________________________
void TMDisplayingHitMapEq::ProcessEndRun() {
//  TCanvas *canv = fTheCanvas->GetCanvas();
//  canv->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", "TMDisplayingHitMapEq", this, "ExecuteEvent(Int_t,Int_t,Int_t,TObject*)");
}
