#include "Starguider.h"

#include <iostream.h> // cout

#include <TGMenu.h>
#include <TSystem.h>
#include <TGSplitter.h>    // TGHorizontal3DLine

#include "MGImage.h"
#include "MGCoordinates.h"

#include "Filter.h"
#include "Writer.h"
#include "base/timer.h"

enum {
    IDM_kFilter,
    IDM_kCatalog,
    IDM_kStart,
    IDM_kStop,
    IDM_kFileType,
    IDM_kPPM,
    IDM_kPNG,
    IDM_kOnce,
    IDM_kContinous,
    IDM_kRate25ps,
    IDM_kRate5ps,
    IDM_kRate1s,
    IDM_kRate5s,
    IDM_kRate30s,
    IDM_kRate1m,
    IDM_kRate5m,
    IDM_kSetup,
    IDM_kLimMag3,
    IDM_kLimMag4,
    IDM_kLimMag5,
    IDM_kLimMag6,
    IDM_kLimMag7,
    IDM_kLimMag8,
    IDM_kLimMag9
};

Starguider::Starguider()
: Camera(), TGMainFrame(gClient->GetRoot(), 768, 700), fRaDec(180, 40)
{
    // p = pointer to MainFrame (not owner)

    const TGWindow *p=gClient->GetRoot();

    //
    // Create Menu for Starguider Display
    //
    fDisplay = new TGPopupMenu(p);
    fDisplay->AddEntry("&Filter",      IDM_kFilter);
    fDisplay->AddEntry("Sao &Catalog", IDM_kCatalog);
    fDisplay->Associate(this);
    fList.Add(fDisplay);

    fFileType = new TGPopupMenu(p);
    fFileType->AddEntry("PP&M", IDM_kPPM);
    fFileType->AddEntry("&PNG", IDM_kPNG);
    fFileType->CheckEntry(IDM_kPNG);
    fFileType->Associate(this);
    fList.Add(fFileType);

    fWriteType = new TGPopupMenu(p);
    fWriteType->AddEntry("Once",      IDM_kOnce);
    fWriteType->AddEntry("Continous", IDM_kContinous);
    fWriteType->CheckEntry(IDM_kOnce);
    fWriteType->Associate(this);
    fList.Add(fWriteType);

    fWriteRate = new TGPopupMenu(p);
    fWriteRate->AddEntry("25/s", IDM_kRate25ps);
    fWriteRate->AddEntry("5/s",  IDM_kRate5ps);
    fWriteRate->AddEntry("1s",   IDM_kRate1s);
    fWriteRate->AddEntry("5s",   IDM_kRate5s);
    fWriteRate->AddEntry("30s",  IDM_kRate30s);
    fWriteRate->AddEntry("1min", IDM_kRate1m);
    fWriteRate->AddEntry("5min", IDM_kRate5m);
    fWriteRate->CheckEntry(IDM_kRate1m);
    fWriteRate->Associate(this);
    fList.Add(fWriteRate);

    fWrtRate = 25*60;

    fWrite = new TGPopupMenu(p);
    fWrite->AddEntry("&Start",      IDM_kStart);
    fWrite->AddEntry("Sto&p",       IDM_kStop);
    fWrite->AddSeparator();
    fWrite->AddPopup("File &Type",  fFileType);
    fWrite->AddPopup("&Write Type", fWriteType);
    fWrite->AddPopup("Write &Rate", fWriteRate);
    fWrite->DisableEntry(IDM_kStop);
    fWrite->Associate(this);
    fList.Add(fWrite);

    fLimMag = new TGPopupMenu(p);
    fLimMag->AddEntry("3", IDM_kLimMag3);
    fLimMag->AddEntry("4", IDM_kLimMag4);
    fLimMag->AddEntry("5", IDM_kLimMag5);
    fLimMag->AddEntry("6", IDM_kLimMag6);
    fLimMag->AddEntry("7", IDM_kLimMag7);
    fLimMag->AddEntry("8", IDM_kLimMag8);
    fLimMag->AddEntry("9", IDM_kLimMag9);
    fLimMag->CheckEntry(IDM_kLimMag8);
    fLimMag->Associate(this);
    fList.Add(fLimMag);

    fSao.SetLimitMag(8.0);

    fSetup = new TGPopupMenu(p);
    fSetup->AddPopup("Lim. &Magnitude", fLimMag);
    fSetup->Associate(this);
    fList.Add(fSetup);

    fMenu = new TGMenuBar(this, 0, 0, kHorizontalFrame);
    fMenu->AddPopup("&Display", fDisplay, NULL);
    fMenu->AddPopup("&Write",   fWrite,   NULL);
    fMenu->AddPopup("&Setup",   fSetup,   NULL);
    fMenu->Resize(fMenu->GetDefaultSize());
    AddFrame(fMenu); //, new TGLayoutHints (kLHintsNormal, 0, 4, 0, 0));
    fList.Add(fMenu);

    fCRaDec = new MGCoordinates(this, kETypeRaDec);
    fCRaDec->Move(1, fMenu->GetDefaultHeight()+584);
    AddFrame(fCRaDec);
    fList.Add(fCRaDec);

    fCZdAz = new MGCoordinates(this, kETypeZdAz);
    fCZdAz->Move(240+12, fMenu->GetDefaultHeight()+584);
    AddFrame(fCZdAz);
    fList.Add(fCZdAz);

    // TGHorizontal3DLine *fLineSep = new TGHorizontal3DLine(this);
    // AddFrame(fLineSep, new TGLayoutHints (kLHintsNormal | kLHintsExpandX));
    // fList.Add(fLineSep);

    //
    // Create Image Display
    //
    fImage = new MGImage(this, 768, 576);
    fImage->Move(0, fMenu->GetDefaultHeight());
    AddFrame(fImage);
    fList.Add(fImage);

    //
    // Make everything visible
    //
    SetWindowName("Starguider Main Window");
    SetIconName("Starguider");

    MapSubwindows();
    MapWindow();

    fSao.SetPixSize(0.006);
}

Starguider::~Starguider()
{
    cout << "Camera Display destroyed." << endl;
}

void Starguider::Layout()
{
    // Resize(GetDefaultSize());
}

void Starguider::CloseWindow()
{
    cout << "EventDisplay::CloseWindow: Exit Application Loop." << endl;

    ExitLoop();

    gSystem->ExitLoop();
}

Bool_t Starguider::ProcessMessage(Long_t msg, Long_t mp1, Long_t mp2)
{
    switch (GET_MSG(msg))
    {
    case kC_COMMAND:
        switch (GET_SUBMSG(msg))
        {
        case kCM_MENU:
            switch (mp1)
            {
            case IDM_kCatalog:
                if (fDisplay->IsEntryChecked(IDM_kCatalog))
                    fDisplay->UnCheckEntry(IDM_kCatalog);
                else
                    fDisplay->CheckEntry(IDM_kCatalog);
                return kTRUE;

            case IDM_kFilter:
                if (fDisplay->IsEntryChecked(IDM_kFilter))
                    fDisplay->UnCheckEntry(IDM_kFilter);
                else
                    fDisplay->CheckEntry(IDM_kFilter);
                return kTRUE;

            case IDM_kStart:
                fWrite->DisableEntry(IDM_kStart);
                fWrite->EnableEntry(IDM_kStop);
                return kTRUE;

            case IDM_kStop:
                fWrite->DisableEntry(IDM_kStop);
                fWrite->EnableEntry(IDM_kStart);
                return kTRUE;

            case IDM_kPNG:
                fFileType->CheckEntry(IDM_kPNG);
                fFileType->UnCheckEntry(IDM_kPPM);
                return kTRUE;

            case IDM_kPPM:
                fFileType->CheckEntry(IDM_kPPM);
                fFileType->UnCheckEntry(IDM_kPNG);
                return kTRUE;

            case IDM_kOnce:
                fWriteType->CheckEntry(IDM_kOnce);
                fWriteType->UnCheckEntry(IDM_kContinous);
                return kTRUE;

            case IDM_kContinous:
                fWriteType->CheckEntry(IDM_kContinous);
                fWriteType->UnCheckEntry(IDM_kOnce);
                return kTRUE;

            case IDM_kRate25ps:
            case IDM_kRate5ps:
            case IDM_kRate1s:
            case IDM_kRate5s:
            case IDM_kRate30s:
            case IDM_kRate1m:
            case IDM_kRate5m:
                for (int i=IDM_kRate25ps; i<=IDM_kRate5m; i++)
                    if (mp1==i)
                        fWriteRate->CheckEntry(i);
                    else
                        fWriteRate->UnCheckEntry(i);
                switch (mp1)
                {
                case IDM_kRate25ps:
                    fWrtRate = 1;
                    return kTRUE;
                case IDM_kRate5ps:
                    fWrtRate = 5;
                    return kTRUE;
                case IDM_kRate1s:
                    fWrtRate = 25;
                    return kTRUE;
                case IDM_kRate5s:
                    fWrtRate = 5*25;
                    return kTRUE;
                case IDM_kRate30s:
                    fWrtRate = 30*25;
                    return kTRUE;
                case IDM_kRate1m:
                    fWrtRate = 60*25;
                    return kTRUE;
                case IDM_kRate5m:
                    fWrtRate = 5*60*25;
                    return kTRUE;
                }
                return kTRUE;

            case IDM_kLimMag3:
            case IDM_kLimMag4:
            case IDM_kLimMag5:
            case IDM_kLimMag6:
            case IDM_kLimMag7:
            case IDM_kLimMag8:
            case IDM_kLimMag9:
                for (int i=IDM_kLimMag3; i<=IDM_kLimMag9; i++)
                    if (mp1==i)
                        fLimMag->CheckEntry(i);
                    else
                        fLimMag->UnCheckEntry(i);

                fSao.SetLimitMag(mp1-IDM_kLimMag3+3);
                return kTRUE;
            }
            break;
        }
        break;
    }
    return kTRUE;
}

void Starguider::ProcessFrame(const unsigned long n, byte *img, struct timeval *tm)
{

    if (!fWrite->IsEntryEnabled(IDM_kStart) &&
        (!(n%fWrtRate) || fWriteType->IsEntryChecked(IDM_kOnce)))
    {
        if (fFileType->IsEntryChecked(IDM_kPNG))
        {
            static int num = 0;

            char name[80];
            sprintf(name, "pix/file%04d.png", num);
            Writer::Png(name, img, tm);
        }

        if (fFileType->IsEntryChecked(IDM_kPPM))
        {
            static int num = 0;
            char name[80];
            sprintf(name, "pix/file%04d.ppm", num);
            Writer::Ppm(name, img);
        }

        if (fWriteType->IsEntryChecked(IDM_kOnce))
            ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kStop, 0);
    }

    if (!(n%25))
    {
        cout << "Img: " << n << endl;

        if (fDisplay->IsEntryChecked(IDM_kFilter))
            Filter::Execute(img);

        if (fDisplay->IsEntryChecked(IDM_kCatalog))
        {
            byte cimg[768*576];

            XY xy = fCRaDec->GetCoordinates();

            fRaDec.Set(xy.X(), xy.Y());

            Timer time(tm);
            fSao.GetImg(img, cimg, time.CalcMjd(), fRaDec);
            fImage->DrawColImg(img, cimg);

            fCZdAz->SetCoordinates(fSao.GetZdAz());
        }
        else
            fImage->DrawImg(img);
    }

}
