/* ======================================================================== *\
!
! *
! * This file is part of MARS, the MAGIC Analysis and Reconstruction
! * Software. It is distributed to you in the hope that it can be a useful
! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
! * It is distributed WITHOUT ANY WARRANTY.
! *
! * Permission to use, copy, modify and distribute this software and its
! * documentation for any purpose is hereby granted without fee,
! * provided that the above copyright notice appear in all copies and
! * that both that copyright notice and this permission notice appear
! * in supporting documentation. It is provided "as is" without express
! * or implied warranty.
! *
!
!
!   Author(s): Thomas Bretz  10/2001 <mailto:tbretz@uni-sw.gwdg.de>
!
!   Copyright: MAGIC Software Development, 2000-2001
!
!
\* ======================================================================== */

#include "MGCamDisplay.h"

#include <TList.h>               // TList::Add
#include <TCanvas.h>             // TCanvas::cd
#include <TGButton.h>            // TGPictureButton
#include <TGButtonGroup.h>       // TGVButtonGroup

#include "MGTask.h"              // MGTask::CreateGui
#include "MClone.h"              // MClone
#include "MHillasExt.h"          // MHillasExt
#include "MParList.h"            // MParList::AddToList
#include "MEvtLoop.h"            // MEvtLoop::GetParList
#include "MTaskList.h"           // MTaskList::AddToList
#include "MCamDisplay.h"         // MCamDisplay
#include "MHillasCalc.h"         // MHillasCalc
#include "MPedestalCam.h"        // MPedestalCam
#include "MCerPhotCalc.h"        // MCerPhotCalc
#include "MMcPedestalCopy.h"     // MMcPedestalCopy
#include "MMcPedestalNSBAdd.h"   // MMcPedestalNSBAdd
#include "MBlindPixelCalc.h"     // MBlindPixelCalc
#include "MImgCleanStd.h"        // MImgCleanStd
#include "MGeomCamMagic.h"       // MGeomMagicCam

ClassImp(MGCamDisplay);

enum
{
    kRButRawEvt,
    kRButCleanedEvt,
    kCButHillas
};

// --------------------------------------------------------------------------
//
//  Add Setup elements to GUI.
//
void MGCamDisplay::AddSetupElements()
{
    //
    // Create gui elements for vertical frame
    //
    TGGroupFrame *grp = new TGGroupFrame(fTab1, "Display");
    TGVButtonGroup *group = new TGVButtonGroup(grp);
    fList->Add(group);

    TGRadioButton *but1 = new TGRadioButton(group, "Raw Events",      kRButRawEvt);
    TGRadioButton *but2 = new TGRadioButton(group, "Cleaned Events",  kRButCleanedEvt);
    TGCheckButton *but3 = new TGCheckButton(grp,   "Display Ellipse", kCButHillas);

    but2->SetState(kButtonDown);
    but3->SetState(kButtonDown);

    fDisplayRaw    = kFALSE;
    fDisplayHillas = kTRUE;

    /*
     WARNING:
     Bacause of some strage and hidden dependencies the
     GetMainFrame call in the destructor of TGButton may fail if some
     of the other gui elements are deleted first.
     AddFirst adds the buttons at the beginning of the deletion list,
     this seems to work.
     */
    fList->AddFirst(but1);
    fList->AddFirst(but2);

    but1->Associate(this);
    but2->Associate(this);
    but3->Associate(this);

    TGLayoutHints *laybut = new TGLayoutHints(kLHintsNormal, 15, 0);
    fList->Add(laybut);

    grp->AddFrame(group);
    grp->AddFrame(but3, laybut);

    TGLayoutHints *laygrp1 = new TGLayoutHints(kLHintsNormal|kLHintsExpandX, 10, 10, 5, 0);
    TGLayoutHints *laygrp2 = new TGLayoutHints(kLHintsNormal|kLHintsExpandX, 10, 10, 5, 5);

    fTab1->AddFrame(grp, laygrp1);
    ((MGTask*)GetTaskList()->FindObject("MImgCleanStd"))->CreateGui(fTab1, laygrp2);
}

// --------------------------------------------------------------------------
//
//  Setup Task and parameter list for hillas calculation,
//  preprocess tasks and read in first event (process)
//
MGeomCam *MGCamDisplay::SetupTaskList()
{
    MTaskList *tlist = GetTaskList();
    MParList  *plist = GetParList();

    MMcPedestalCopy   *pcopy = new MMcPedestalCopy;
    MMcPedestalNSBAdd *pdnsb = new MMcPedestalNSBAdd;
    MCerPhotCalc      *ncalc = new MCerPhotCalc;
    MClone            *clone = new MClone("MCerPhotEvt");
    MImgCleanStd      *clean = new MImgCleanStd;
    MBlindPixelCalc   *blind = new MBlindPixelCalc;
    MHillasCalc       *hcalc = new MHillasCalc;

    tlist->AddToList(pcopy);
    tlist->AddToList(pdnsb);
    tlist->AddToList(ncalc);
    tlist->AddToList(clone);
    tlist->AddToList(clean);
    tlist->AddToList(blind);
    tlist->AddToList(hcalc);

    MGeomCamMagic *geom   = new MGeomCamMagic;
    MPedestalCam  *pedest = new MPedestalCam;
    MHillasExt    *hext   = new MHillasExt;

    plist->AddToList(geom);
    plist->AddToList(pedest);
    plist->AddToList(hext);

    return geom;
}

// --------------------------------------------------------------------------
//
//  Constructor.
//
MGCamDisplay::MGCamDisplay(const char *filename,
                           const TGWindow *p, /*const TGWindow *main,*/
                           UInt_t w, UInt_t h)
: MGEvtDisplay(filename, "Events", p, /*main,*/ w, h), fDisplay(NULL)
{
    //
    // Setup Task list for hillas calculation
    //
    MGeomCam *geom = SetupTaskList();

    //
    // Add missing setup elements to GUI
    //
    AddSetupElements();

    TCanvas *canv2 = AddTab("Errors");
    TCanvas *canv3 = AddTab("Phot/Err");
    TCanvas *canv4 = AddTab("Levels");
    TCanvas *canv5 = AddTab("Pedestals");

    //
    // Show camera display for the actual geometry
    //
    fDisplay  = new MCamDisplay(geom);
    fDisplay2 = new MCamDisplay(geom);
    fDisplay3 = new MCamDisplay(geom);
    fDisplay4 = new MCamDisplay(geom);
    fDisplay5 = new MCamDisplay(geom);

    fList->Add(fDisplay);
    fList->Add(fDisplay2);
    fList->Add(fDisplay3);
    fList->Add(fDisplay4);
    fList->Add(fDisplay5);

    fCanvas->cd();
    fDisplay->Draw();
    canv2->cd();
    fDisplay2->Draw();
    canv3->cd();
    fDisplay3->Draw();
    canv4->cd();
    fDisplay4->Draw();
    canv5->cd();
    fDisplay5->Draw();


    ReadFirstEvent();

    //
    //   Map the window, set up the layout, etc.
    //
    MapSubwindows();

    Layout();

    SetWindowName("Hillas Event Display");
    SetIconName("Hillas");

    MapWindow();
}

// --------------------------------------------------------------------------
//
//  Update event display:
//  dependent on the setup either the uncleaned or cleand data is shown
//  together with the hillas ellipse or not.
//
void MGCamDisplay::UpdateDisplay()
{
    if (!IsInitOk())
        return;

    const MParList *plist = fEvtLoop->GetParList();

    //
    // Show Hillas ellipse
    //
    MHillas *hillas = (MHillas*)plist->FindObject("MHillas");

    hillas->Print();
    if (fDisplayHillas)
    {
        fCanvas->cd();
        hillas->Draw();
    }
    else
        hillas->Clear();

    //
    // Display the requested event. This does a Canvas update, too.
    //
    MCerPhotEvt  *evt = NULL;
    if (fDisplayRaw)
    {
        //
        // Get a clone of MCerPhotEvt which is made before the image cleaning
        //
        const MClone *clone = (MClone*)GetTaskList()->FindObject("MClone");
        evt = (MCerPhotEvt*)clone->GetClone();
    }
    else
    {
        //
        // Get MCerPhotEvt which containes the cleaned data
        //
        evt = (MCerPhotEvt*)plist->FindObject("MCerPhotEvt");
    }

    const MImgCleanStd *clean = (MImgCleanStd*)GetTaskList()->FindObject("MImgCleanStd");
    const MPedestalCam *ped   = (MPedestalCam*)plist->FindObject("MPedestalCam");

    fDisplay->DrawPhotNum(evt);
    fDisplay2->DrawErrorPhot(evt);
    fDisplay3->DrawRatio(evt);
    fDisplay4->DrawLevels(evt, *clean);
    fDisplay5->DrawPedestals(ped);
}

// --------------------------------------------------------------------------
//
//  Process the messages from the setup GUI elements.
//
Bool_t MGCamDisplay::ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2)
{
    switch(GET_MSG(msg))
    {
    case kC_COMMAND:
        switch(GET_SUBMSG(msg))
        {
        case kCM_CHECKBUTTON:
            switch (parm1)
            {
            case kCButHillas:
                fDisplayHillas = !fDisplayHillas;
                UpdateDisplay();
                return kTRUE;
            }
            break;

        case kCM_RADIOBUTTON:
            switch (parm1)
            {
            case kRButRawEvt:
                fDisplayRaw = kTRUE;
                UpdateDisplay();
                return kTRUE;

            case kRButCleanedEvt:
                fDisplayRaw = kFALSE;
                UpdateDisplay();
                return kTRUE;
            }
            break;
        }
        break;
    }
    return MGEvtDisplay::ProcessMessage(msg, parm1, parm2);
}

