#include "TMDisplayingCErrorEqChoice.h"
#include "TMHistogrammingCErrorEqChoice.h"
#include "TMControlSPD.h"
#include "SPDMood.h"
#include "AliITSRawStreamSPD.h"
#include <TGButton.h>
#include <TGTextView.h>
#include <TCanvas.h>

ClassImp(TMDisplayingCErrorEqChoice)

//_____________________________________________________________________
TMDisplayingCErrorEqChoice::TMDisplayingCErrorEqChoice(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,kTRUE),
  fCurrentErr(0), fCurrentEq(20)
{
  gClient->GetColorByName("green", green);
  gClient->GetColorByName("gray", gray);

  CreateTopFrame();
  CreateButtonFrame();
  CreateBottomFrame();

  ToggleErrChoiceButton(fCurrentErr);
  ToggleEqChoiceButton(fCurrentEq);

  MapSubwindows();
  Resize();
  MapWindow();

  TCanvas *canv = fTheCanvas->GetCanvas();
  fHist->Draw(canv);
  Update();
}
//_____________________________________________________________________
TMDisplayingCErrorEqChoice::~TMDisplayingCErrorEqChoice() {}
//_____________________________________________________________________
void TMDisplayingCErrorEqChoice::CreateButtonFrame() {
  TGCompositeFrame *tf;
  UInt_t id;

  tf = new TGCompositeFrame(this, 50, 21, kHorizontalFrame | kFixedSize);
  AddFrame(tf, new TGLayoutHints(kLHintsExpandX, 0,0,0,0));
  id = ADD_ID_START+21+AliITSRawStreamSPDErrorLog::kNrErrorCodes;
  fShowErrorCodesButton = new TGTextButton(tf,"Show Error Codes",id);
  fShowErrorCodesButton->Associate(this);
  tf->AddFrame(fShowErrorCodesButton, new TGLayoutHints(kLHintsNormal, 0,0,0,0));
  for (UInt_t err=0; err<AliITSRawStreamSPDErrorLog::kNrErrorCodes; err++) {
    TString errName = Form("EC %d",err);
    id = ADD_ID_START + 21 + err;
    fSwitchErrButton[err] = new TGTextButton(tf,errName.Data(),id);
    fSwitchErrButton[err]->Associate(this);
    tf->AddFrame(fSwitchErrButton[err], new TGLayoutHints(kLHintsNormal, 0,0,0,0));
  }

  tf = new TGCompositeFrame(this, 50, 21, kHorizontalFrame | kFixedSize);
  AddFrame(tf, new TGLayoutHints(kLHintsExpandX, 0,0,0,0));
  id = ADD_ID_START + 20;
  fSwitchEqButton[20] = new TGTextButton(tf,"All Eq",id);
  fSwitchEqButton[20]->Associate(this);
  tf->AddFrame(fSwitchEqButton[20], new TGLayoutHints(kLHintsNormal, 0,0,0,0));
  for (UInt_t eq=0; eq<20; eq++) {
    TString eqName = Form("Eq %d",eq);
    id = ADD_ID_START + eq;
    fSwitchEqButton[eq] = new TGTextButton(tf,eqName.Data(),id);
    fSwitchEqButton[eq]->Associate(this);
    tf->AddFrame(fSwitchEqButton[eq], new TGLayoutHints(kLHintsNormal, 0,0,0,0));
  }

}
//______________________________________________________________________
Bool_t TMDisplayingCErrorEqChoice::ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2) {
  //  printf("%d,%ld,%ld\n",GET_MSG(msg),parm1,parm2);
  switch(GET_MSG(msg)) {
  case kC_COMMAND :
    switch (GET_SUBMSG(msg)) {

    case kCM_BUTTON :
      if (parm1>=ADD_ID_START && parm1<ADD_ID_START+21) {
	TMControlSPD::Instance()->HoldRunning();
	UInt_t eq = parm1-ADD_ID_START;

	ToggleEqChoiceButton(eq);

	TCanvas *canv = fTheCanvas->GetCanvas();
	fHist->Draw(canv);

	Update();
	TMControlSPD::Instance()->ContinueRunning();
	return kTRUE;
      }

      else if (parm1>=ADD_ID_START+21 && parm1<ADD_ID_START+21+AliITSRawStreamSPDErrorLog::kNrErrorCodes) {
	TMControlSPD::Instance()->HoldRunning();
	UInt_t err = parm1-ADD_ID_START-21;

	ToggleErrChoiceButton(err);

	TCanvas *canv = fTheCanvas->GetCanvas();
	fHist->Draw(canv);

	Update();
	TMControlSPD::Instance()->ContinueRunning();
	return kTRUE;
      }

      else if (parm1==ADD_ID_START+21+AliITSRawStreamSPDErrorLog::kNrErrorCodes) {
	TMControlSPD::Instance()->HoldRunning();
	ProcessOpenCErrorCodesFrame();
	TMControlSPD::Instance()->ContinueRunning();
	return kTRUE;
      }

    }
  }
  return TMDisplaying::ProcessMessage(msg,parm1,parm2);
}
//_____________________________________________________________________
void TMDisplayingCErrorEqChoice::ProcessOpenCErrorCodesFrame() {
  TGMainFrame *mF = new TGMainFrame(gClient->GetRoot(),300,500);
  mF->SetWindowName("Consistency Error Codes");

  TGTextView *tv =  new TGTextView(mF, 305, 340, kHorizontalFrame | kFixedSize);

  mF->AddFrame(tv, new TGLayoutHints(kLHintsExpandX  | kLHintsExpandY,0,0,0,0));

  for (UInt_t err=0; err<AliITSRawStreamSPDErrorLog::kNrErrorCodes; err++) {
    TString msg = Form(" %d. %s",err,AliITSRawStreamSPD::GetErrorName(err));
    tv->AddLine(msg.Data());
  }
  tv->Update();

  mF->MapSubwindows();
  mF->Resize();
  mF->MapWindow();
}
//_____________________________________________________________________
void TMDisplayingCErrorEqChoice::ToggleEqChoiceButton(UInt_t eq) {
  if (eq>20) return;
  
  fSwitchEqButton[fCurrentEq]->AllowStayDown(kFALSE);
  fSwitchEqButton[fCurrentEq]->SetBackgroundColor(gray);
  fSwitchEqButton[fCurrentEq]->SetState(kButtonUp);

  fCurrentEq = eq;

  fSwitchEqButton[fCurrentEq]->AllowStayDown(kTRUE);
  fSwitchEqButton[fCurrentEq]->SetBackgroundColor(green);
  fSwitchEqButton[fCurrentEq]->SetState(kButtonDown);

  ((TMHistogrammingCErrorEqChoice*)fHist)->SetCurrentEq(eq);
}
//_____________________________________________________________________
void TMDisplayingCErrorEqChoice::ToggleErrChoiceButton(UInt_t err) {
  if (err>=AliITSRawStreamSPDErrorLog::kNrErrorCodes) return;
  
  fSwitchErrButton[fCurrentErr]->AllowStayDown(kFALSE);
  fSwitchErrButton[fCurrentErr]->SetBackgroundColor(gray);
  fSwitchErrButton[fCurrentErr]->SetState(kButtonUp);

  fCurrentErr = err;

  fSwitchErrButton[fCurrentErr]->AllowStayDown(kTRUE);
  fSwitchErrButton[fCurrentErr]->SetBackgroundColor(green);
  fSwitchErrButton[fCurrentErr]->SetState(kButtonDown);

  ((TMHistogrammingCErrorEqChoice*)fHist)->SetCurrentErr(err);
}
