#include "MCeCoCom.h"

#include <iostream>

#include "MLog.h"

#include "MString.h"

using namespace std;

/*
 // THIS COULD NEVER WORK BECAUSE IT IGNORES WHETHER
 // WE SEND STARGUIDER OR DRIVE REPORTS!
bool MCeCoCom::SendMsg(const char *msg, bool force=kFALSE)
{
    const MTime t(-1);

    if ((double)t-(double)fTime<0.001*fSendInterval && !force) 
          return true;

    if (lout.Lock("MTcpIpIO::Send"))
    {
        //const Int_t rc = lout.IsOutputDeviceEnabled(MLog::eGui);
        //lout.DisableOutputDevice(MLog::eGui);
        lout << msg << flush;
        lout.UnLock("MTcpIpIO::Send");
        //if (rc)
        //    lout.EnableOutputDevice(MLog::eGui);
    }

    fTime = t;

    return Send(msg, strlen(msg));
}
*/
bool MCeCoCom::InterpreteCmd(TString cmd, TString str)
{
    cout << cmd << ": " << str << endl;
    return true;
}

bool MCeCoCom::InterpreteReport(TString &str)
{
    if (!str.EndsWith("OVER"))
    {
        cout << "Communication Problem: Termination (OVER) not found." << endl;
        fComStat = kComProblem;
        return false;
    }

    int y, m, d, h, min, s, ms, len;

    int n=sscanf(str.Data(),
                 "%02d %04d %02d %02d %02d %02d %02d %03d %n",
                 &fCCStatus, &y, &m, &d, &h, &min, &s, &ms, &len);
    if (n!=8)
    {
        cout << "Communication Problem: Wrong number of arguments" << endl;
        fComStat = kComProblem;
        return false;
    }
    str.Remove(0, len);

    fT.Set(y, m, d, h, min, s, ms);

    // Remove the 30 tokens of the subsystem status
    //  table 12.1 p59
    for (int i=0; i<31; i++)
        str.Remove(0, str.First(' ')+1);

    // skip solar_irr_Wm2, wind_speed, UPS
    float zd, az, dec, ra, temp, solar, wind, hum;
    n=sscanf(str.Data(),
             /*"%f %f %f %f %f %f %f %f %*f %*f %*s %*s %*s %*s %n",*/
             "%f %f %f %f %f %f %f %f %*f %*f %n",
             &zd, &az, &dec, &ra, &temp, &solar, &wind, &hum, &len);
    if (n!=8)
    {
        cout << "Communication Problem: Wrong number of arguments" << endl;
        fComStat = kComProblem;
        return false;
    }
/*
    str.Remove(0, len);

    if (str!=(TString)"OVER")
    {
        cout << "Communication Problem: Termination (OVER) too far away." << endl;
        fComStat = kComProblem;
        return false;
    }
 */

    fHumidity = hum;
    fTemperature = temp;
    fSolarRadiation = solar;
    fWindSpeed = wind;

    if (fWindSpeed>50)
        fAlarmCounter++;
    else
        fAlarmCounter=0;

    cout << "Zd/Az: " << zd << "/" << az << "  ";
    cout << "Ra/Dec: " << ra/15 << "h/" << dec << "  ";
    //cout << "SolRad: " << solar << "W/mē";
    cout << endl;

    fComStat = kCmdReceived;
    return true;
}

bool MCeCoCom::InterpreteStr(TString str)
{
    const Ssiz_t tok  = str.First(' ');
    const TString cmd = tok<0 ? str : str(0, tok);
    if (tok<0)
        str = "";
    else
        str.Remove(0, tok+1);

    if (cmd==(TString)"CC-REPORT" && str.EndsWith("OVER"))
        return InterpreteReport(str);

    return InterpreteCmd(cmd, str.Strip(TString::kBoth));
}

bool MCeCoCom::SendRep(const char *cmd, const char *str, bool force)
{

    UShort_t y1, y2, ms1, ms2;
    Byte_t mon1, mon2, d1, d2, h1, h2, m1, m2, s1, s2;

    fT.GetDate(y2, mon2, d2);
    fT.GetTime(h2, m2, s2, ms2);

    MTime t(-1);
    t.GetDate(y1, mon1, d1);
    t.GetTime(h1, m1, s1, ms1);

    if (t-fT>20)
        fComStat = kComProblem;

    const TString msg =
        MString::Format("%s M%d "
             "%02d %04d %02d %02d %02d %02d %02d %03d "
             "%02d %04d %02d %02d %02d %02d %02d %03d "
             "%s\n", cmd, fTelescope,
             fStatus,  y1, mon1, d1, h1, m1, s1, ms1,
             fComStat, y2, mon2, d2, h2, m2, s2, ms2,
             str);

    // Send report to CC
    const bool rc = Send(msg.Data(), msg.Length());

    // Write report to stream
    if (fOut)
        if (fOut->Lock("MTcpIpIO::Send"))
        {
            *fOut << msg << flush;
            fOut->UnLock("MTcpIpIO::Send");
        }

    return rc;
}

TString MCeCoCom::GetWeather() const
{
    if (fSolarRadiation<0 || (double)MTime(-1)-(double)fT>11)
        return "";

    return MString::Format("Temp: %.1f'C  Hum: %.1f%%  Wind: %.1fkm/h",
                           fTemperature, fHumidity, fWindSpeed);
}

Int_t MCeCoCom::GetWeatherStatus() const
{
    if (fSolarRadiation<0 || (double)MTime(-1)-(double)fT>11)
        return 0;

    Int_t rc = 0;
    if (fHumidity>80)
        rc++;
    if (fWindSpeed>30)
        rc++;
    if (fWindSpeed>40)
        rc++;
    if (fWindSpeed>50)
        rc++;

    return rc;
}
