//
// This File contains the definition of the MGCoordinates-class
//
//   Author: Thomas Bretz
//   Version: V1.0 (1-8-2000)

#include "MGSkyPosition.h"

#include <iostream.h>  // cout

#include <TArc.h>
#include <TLine.h>
#include <TText.h>
#include <TList.h>
#include <TCanvas.h>

#include "coord.h"
#include "SlaStars.h"
#include "SlaPlanets.h"

ClassImp(MGSkyPosition);

void MGSkyPosition::InitArc(TArc *arc, Int_t fillstyle, Int_t fillcolor, Int_t linecolor)
{
    arc->SetFillStyle(fillstyle);      // (s. TAttFill)
    arc->SetFillColor(fillcolor);      // (s. TAttFill)
    arc->SetLineColor(linecolor);
    arc->Draw();
}

void MGSkyPosition::InitPlanets()
{
    //
    // Magnitudes:
    // -----------
    //   Moon     -12.8
    //   Jupiter   -4.6
    //   Venus     -4.0
    //   Mars      -2.0
    //   Merkur     0.0
    //   Saturn     0.7
    //   Uranus     5.5
    //   Neptun     7.8
    //
    fSlaPlanet = new SlaPlanets;
    fSlaStar   = new SlaStars;

    //
    // Colors: black .. white:
    //
    //   1, 12, 13, 14, 15, 16, 17, 18, 19, 10
    //
    fMars    = new TArc(0, 0, 1);
    fVenus   = new TArc(0, 0, 2);
    fJupiter = new TArc(0, 0, 2);
    fMoon    = new TArc(0, 0, 3, 290, 70);
    fSun     = new TArc(0, 0, 2);
    fSaturn  = new TArc(0, 0, 1);

    fSaturnRing = new TArc(0, 0, 2);
    fSaturnRing->SetFillStyle(4000);      // (s. TAttFill)
    fSaturnRing->SetLineColor(kRed);
    fSaturnRing->Draw();

    InitArc(fMars,    1001, kRed/*13*/, kRed/*12*/);
    InitArc(fVenus,   1001, 15, 14);
    InitArc(fJupiter, 1001, 17, 16);
    InitArc(fMoon,    1001, 19, 15);
    InitArc(fSaturn,  1001, kYellow/*17*/, 16);

    for (int i=0; i<4; i++)
    {
        fSunL[i] = new TLine(0,0,0,0);
        fSunL[i]->SetLineColor(10);    // white gray
        fSunL[i]->SetLineStyle(1);     // solid  (s. TAttLine)
        fSunL[i]->Draw();

        fList->Add(fSunL[i]);
    }
    InitArc(fSun, 1001, kYellow/*10*/, kYellow/*19*/);

    fList->Add(fMars);
    fList->Add(fVenus);
    fList->Add(fJupiter);
    fList->Add(fMoon);
    fList->Add(fSun);
    fList->Add(fSaturn);

    //
    // Reset 'static' positions of planets
    //
    for (int i=0; i<9; i++)
    {
        fX[i] = ~0;
        fY[i] = ~0;
    }
}

void MGSkyPosition::InitText()
{
    fText = new TText(105, 105, "");
    //fText->SetFillStyle(4000);  // transparent
    fText->SetTextAlign(33);  // right, top
    fText->SetTextColor(10);  // white
    fText->Draw();

    fList->Add(fText);
}

void MGSkyPosition::DrawCoordinateSystem()
{
    TArc arc;
    arc.SetFillStyle(4000);  // transparent
    arc.SetLineColor(13);    // dark gray
    arc.SetLineStyle(1);     // solid  (s. TAttLine)
    arc.DrawArc(0, 0, 90);
    arc.DrawArc(0, 0, 67.5);
    arc.DrawArc(0, 0, 45);
    arc.DrawArc(0, 0, 22.5);

    //
    // FIXME? Use TAxis?
    //
    TLine line;
    line.SetLineColor(1);  // black
    line.SetLineStyle(1);  // solid  (s. TAttLine)
    line.DrawLine(-95,   0, 95,  0);
    line.DrawLine(  0, -95,  0, 95);

    TText text;
    text.SetTextAlign(22);  // centered, centered (s.TAttText)
    text.SetTextColor(13);  // dark gray
    text.DrawText(0.71*22.5, 0.71*22.5, "22.5\xb0");
    text.DrawText(0.71*45.0, 0.71*45.0, "45\xb0");
    text.DrawText(0.71*67.5, 0.71*67.5, "67.5\xb0");
    text.DrawText(0.71*90.0, 0.71*90.0, "90\xb0");

    text.SetTextAlign(22);  // centered, centered (s.TAttText)
    text.SetTextColor(1);   // black

    text.DrawText(0,  102, "N (0\xb0)");
    text.DrawText(0, -102, "S (180\xb0)");

    text.SetTextAngle(270);
    text.DrawText( 102, 0, "E (90\xb0)");

    text.SetTextAngle(90);
    text.DrawText(-102, 0, "W (270\xb0)");
}

void MGSkyPosition::InitPosition()
{
    fLin1a = new TLine(0, 0, 0, 0);
    fLin1b = new TLine(0, 0, 0, 0);
    fLin2  = new TLine(0, 0, 0, 0);

    fLin1a->SetLineColor(5);  // yellow (s. TAttFill)
    fLin1b->SetLineColor(5);  // yellow
    fLin2 ->SetLineColor(10); // white
    fLin1a->SetLineStyle(1);  // solid  (s. TAttLine)
    fLin1b->SetLineStyle(2);  // dashed

    fLin1a->Draw();
    fLin1b->Draw();
    fLin2->Draw();

    fList->Add(fLin1a);
    fList->Add(fLin1b);
    fList->Add(fLin2);

    for (int i=0; i<6; i++)
    {
        fDot[i] = new TArc(0, 0, 1);
        InitArc(fDot[i], 1001, 10, 19);
        fList->Add(fDot[i]);
    }
}

MGSkyPosition::MGSkyPosition(const TGWindow* p, const UInt_t w)
    : MGEmbeddedCanvas("SkyPosition", p, w, 110)
{
    DrawCoordinateSystem();
    InitPlanets();
    InitPosition();
    InitText();
    InitCanvas();
    SetNoContextMenu();
}

MGSkyPosition::~MGSkyPosition()
{
    delete fSlaPlanet;
    delete fSlaStar;

    cout << "MGSkyPosition destroyed." << endl;
}

void MGSkyPosition::SetLin1(Float_t x, Float_t y)
{
    fLin1a->SetX2(x);
    fLin1a->SetY2(y);
    fLin1b->SetX2(-x);
    fLin1b->SetY2(-y);
}

void MGSkyPosition::SetLin2(Float_t x1, Float_t y1, Float_t x2, Float_t y2)
{
    fLin2->SetX1(x1);
    fLin2->SetX2(x2);
    fLin2->SetY1(y1);
    fLin2->SetY2(y2);
}

void MGSkyPosition::SetDot(TArc *arc, RaDec &radec, Int_t off)
{
    RaDec rd(radec.Ra()+off*360/24*kDeg2Rad, radec.Dec());
    ZdAz zdaz = fSlaStar->CalcZdAzFast(rd);

    const float s = sin(zdaz.Az());
    const float c = cos(zdaz.Az());

    const float x = s*zdaz.Zd()*kRad2Deg;             // [deg]
    const float y = c*zdaz.Zd()*kRad2Deg;             // [deg]

    arc->SetX1(x);
    arc->SetY1(y);

    SetModified();
}

void MGSkyPosition::UpdatePosition(RaDec &radec, Float_t zd, Float_t az)
{
    //
    // zd [deg]
    // az [deg]
    //
    static int X =~0;
    static int Y =~0;
    static int Rx=~0;
    static int Ry=~0;

    const float rad = az*kDeg2Rad;    // [rad]

    const float s = sin(rad);
    const float c = cos(rad);

    const float x = s*zd;             // [deg]
    const float y = c*zd;             // [deg]

    const int pixx = (int)(x/fPix);
    const int pixy = (int)(y/fPix);

    const int rx = (int)(s*fWidth/2.);
    const int ry = (int)(c*fWidth/2.);

    if (X==pixx && Y==pixy && Rx==rx && Ry==ry)
        return;

    X = pixx;
    Y = pixy;

    Rx = rx;
    Ry = ry;

    const float dx = s*4.;
    const float dy = c*4.;

    SetLin1(s*95., c*95.);
    SetLin2(x+dy, y-dx, x-dy, y+dx);

    if (zd<80)
    {
        for(int i=0; i<3; i++)
            SetDot(fDot[i], radec, i-3);
        for(int i=3; i<6; i++)
            SetDot(fDot[i], radec, i-2);
    }

    SetModified();
}

void MGSkyPosition::UpdateText(Float_t x, Float_t y)
{
    static int X = ~0;
    static int Y = ~0;

    int xd = (int)/*floor*/(x);
    int yd = (int)/*floor*/(y);
    x *= 60.;
    y *= 60.;

    int fx = (int)/*floor*/(x*10.);
    int fy = (int)/*floor*/(y*10.);

    if (X==fx && Y==fy)
        return;

    X = fx;
    Y = fy;

    float xm = fmod(fabs(x), 60.);
    float ym = fmod(fabs(y), 60.);

    char txt[100];
    sprintf(txt, "Zd=%s%d\xb0%02.1f'\nAz=%s%d\xb0%02.1f'",
            x<0?"-":"", abs(xd), xm, y<0?"-":"", abs(yd), ym);

    fText->SetText(fText->GetX(), fText->GetY(), txt);

    SetModified();
}


void MGSkyPosition::Update(ZdAz &pos, double mjd)
{
    //
    // calculate actual time for planet positions
    //
    fSlaPlanet->SetMjd(mjd);
    fSlaStar->SetMjd(mjd);

    UpdatePlanet(kESun,     fSun);
    UpdatePlanet(kEMoon,    fMoon);
    UpdatePlanet(kEJupiter, fJupiter);
    UpdatePlanet(kEVenus,   fVenus);
    UpdatePlanet(kEMars,    fMars);
    UpdatePlanet(kESaturn,  fSaturn);

    RaDec radec = fSlaStar->CalcRaDecFast(pos*kDeg2Rad);

    UpdatePosition(radec, pos.Zd(), pos.Az());
    UpdateText(pos.Zd(), pos.Az());

    UpdateCanvas();
}

void MGSkyPosition::UpdatePlanet(Int_t planet, TArc *arc)
{
    //
    // calculate actual position of planet
    //
    fSlaPlanet->UpdatePlanetPos((ePlanets_t)planet);

    ZdAz zdaz = fSlaPlanet->GetPlanetPos((ePlanets_t)planet);

    //
    // check whether we have to change the position where it is drawn
    //
    const float s = sin(zdaz.Az());
    const float c = cos(zdaz.Az());

    const float deg = zdaz.Zd()*kRad2Deg;

    const float x = s*deg;
    const float y = c*deg;

    const int pixx = (int)(x/fPix);
    const int pixy = (int)(y/fPix);

    if (fX[planet]==pixx && fY[planet]==pixy)
        return;

    fX[planet] = pixx;
    fY[planet] = pixy;

    //
    // Set new position
    //
    arc->SetX1(x);
    arc->SetY1(y);

    SetModified();

    if (planet==kESaturn)
    {
        fSaturnRing->SetX1(x);
        fSaturnRing->SetY1(y);
    }

    if (planet==kESun)
    {
        fSunL[0]->SetX1(x-3.5); fSunL[0]->SetX2(x+3.5);
        fSunL[1]->SetX1(x-2.5); fSunL[1]->SetX2(x+2.5);
        fSunL[2]->SetX1(x-2.5); fSunL[2]->SetX2(x+2.5);
        fSunL[3]->SetX1(x);     fSunL[3]->SetX2(x);

        fSunL[0]->SetY1(y);     fSunL[0]->SetY2(y);
        fSunL[1]->SetY1(y-2.5); fSunL[1]->SetY2(y+2.5);
        fSunL[2]->SetY1(y+2.5); fSunL[2]->SetY2(y-2.5);
        fSunL[3]->SetY1(y-3.5); fSunL[3]->SetY2(y+3.5);
    }
}
