#include "grifotgtform.h"
#include "ui_grifotgtform.h"

#include <QGridLayout>


#include "g346_common.h"

#include "ads_manager.h"

#include "time_metrics.h"

#include <math.h>

#include <QDebug>

#include "icd_g346.h"

class widgets_t
{
    QGridLayout* gly;
};

using namespace icd1553;

class TgtWidgets
{
public:
    QLabel* id;
    QLabel* timetag;
    QDoubleSpinBox* range;
    QLabel* az;
    QLabel* el;
    QLabel* dxy;
};


GrifoTgtForm* GrifoTgtForm::theInstance;

class GrifoTgtForm::Implementation
{
public:
    TgtWidgets tgt[10];
};

GrifoTgtForm::GrifoTgtForm(QWidget *parent) :
    QScrollArea(parent),
    ui(new Ui::GrifoTgtForm),
    p_(*new Implementation)
{
    theInstance=this;

    ui->setupUi(this);

    //widgets_t tmp;
    //tmp.gly=new QGridLayout;

    Ads1553MsgQ* b5=Ads1553Manager::instance().getMsgObj("B5");

    GRIFO_ADS_MAP(hpt_timetag, "HPT_Timetag", tracked_target_message_t, relative_timetag, relative_timetag_t, *b5);
    GRIFO_ADS_MAP(hpt_id, "HPT_ID", tracked_target_message_t, aux, track_id_t, *b5);

    GRIFO_ADS_MAP(hpt_range_meters, "HPT_range_meters", tracked_target_message_t, range_of_tracked_target, range_of_tracked_target_t, *b5);
    GRIFO_ADS_MAP_SCALED(hpt_range, "HPT_range", tracked_target_message_t, range_of_tracked_target, range_of_tracked_target_t, *b5, GC_K_METERS_TO_FEETS);

    {
        static const char* data_lbl[]=
        {
            "ID", "Timetag", "Range", "Azimuth","Elevation", "Display",
            0
        };

        QLabel* tmp=new QLabel;
        ui->mttLayout->addWidget(tmp, 0, 0);

        for(int l=0; l<100; ++l)
        {
            if (data_lbl[l]==0)
                break;
            tmp=new QLabel;
            tmp->setText(data_lbl[l]);
            ui->mttLayout->addWidget(tmp, l+1, 0);
        }

        for(int i=0; i<10; ++i)
        {

            QLabel* tmp=new QLabel;
            tmp->setText(QString("MTT%1").arg(i+1));
            ui->mttLayout->addWidget(tmp, 0, i+1);

            TgtWidgets& t=p_.tgt[i];
            //for(int l=0; l<100; ++l)
            {
                tmp=new QLabel;
                tmp->setText("0");
                t.id=tmp;
                ui->mttLayout->addWidget(tmp, 1, i+1);

                tmp=new QLabel;
                tmp->setText("0");
                t.timetag=tmp;
                ui->mttLayout->addWidget(tmp, 2, i+1);

                QDoubleSpinBox* sp=new QDoubleSpinBox;
                sp->setDecimals(3);
                sp->setMaximum(100000);
                sp->setMinimum(-100000);
                sp->setReadOnly(true);
                sp->setButtonSymbols(QAbstractSpinBox::NoButtons);

                sp->setValue(0);
                t.range=sp;
                ui->mttLayout->addWidget(sp, 3, i+1);

                tmp=new QLabel;
                tmp->setText("0");
                t.az=tmp;
                ui->mttLayout->addWidget(tmp, 4, i+1);

                tmp=new QLabel;
                tmp->setText("0");
                t.el=tmp;
                ui->mttLayout->addWidget(tmp, 5, i+1);

                tmp=new QLabel;
                tmp->setText("(0,0)");
                t.dxy=tmp;
                ui->mttLayout->addWidget(tmp, 6, i+1);
            }
        }
    }
#if 1
    for(int i=0; i<10; ++i)
    {
        GRIFO_ADS_MAP_SCALED(trk[i].range, "HPT_range", tracked_target_message_t, range_of_tracked_target, range_of_tracked_target_t, *b5, GC_K_METERS_TO_FEETS);

        QString tmp=QString("TRK_RNG_1").arg(i);
        trk[i].range.setDataName(tmp);
#if 0
        w.dWaypointLon.setDataName(QString("WAY_%1_LON").arg(wpid));
        w.dWaypointLon.setFormat(waypoint_latlon_t::start_bit, waypoint_latlon_t::set_mask);
        w.dWaypointLon.setLsb(WP_LATLON_LSB);
        w.dWaypointLon.attachToMsg(a3, ((i*sizeof(waypoint_data_t))/2)+ICD_WOFFSETOF(msg_graphic_setting_t, waypoints.wp[0].waypoint_lon_code));
#endif
    }
#endif
}


GrifoTgtForm::~GrifoTgtForm()
{
    delete ui;
}

GrifoTgtForm* GrifoTgtForm::instance()
{
    return theInstance;
}

static void vchar(char& c1, char& c2, bool valid)
{
    if (valid)
    {
        c1=' ';
        c2=' ';
    }
    else
    {
        c1='(';
        c2=')';
    }
}

void GrifoTgtForm::processRawTrackMessage(int n, const avb_1553_word_t* raw_msg)
{
    const tracked_target_message_t* msg=reinterpret_cast<const tracked_target_message_t*>(raw_msg);

    char c1=' ';
    char c2=' ';

    if (msg->track_validity_and_status_word_01.raw==0)
        p_.tgt[n].id->setText(QString("(%1)").arg(msg->aux.id.get()));
    else
        p_.tgt[n].id->setText(QString("%1").arg(msg->aux.id.get()));

    p_.tgt[n].range->setValue(msg->range_of_tracked_target.get());
    if (msg->track_validity_and_status_word_01.range_validity.get())
        p_.tgt[n].range->setPrefix("");
    else
        p_.tgt[n].range->setPrefix("?");

    p_.tgt[n].timetag->setText(QString("%1").arg(msg->relative_timetag.get()));


    vchar(c1, c2, msg->track_validity_and_status_word_01.antenna_position_validity.get());

    p_.tgt[n].az->setText(QString("%1%2%3").arg(c1).arg(gc_math::rad_to_deg(msg->antenna_azimuth_position.get())).arg(c2));
    p_.tgt[n].el->setText(QString("%1%2%3").arg(c1).arg(gc_math::rad_to_deg(msg->antenna_elevation_position.get())).arg(c2));

    if (msg->track_validity_and_status_word_01.display_coordinates_validity.get())
    {
        p_.tgt[n].dxy->setText(QString("%1,%2").arg(msg->target_x_display_coordinate.display_coord_of_the_tracked_target.get()).arg(msg->target_y_display_coordinate.display_coord_of_the_tracked_target.get()));
    }
    else
    {
        p_.tgt[n].dxy->setText(QString("(%1,%2)").arg(msg->target_x_display_coordinate.display_coord_of_the_tracked_target.get()).arg(msg->target_y_display_coordinate.display_coord_of_the_tracked_target.get()));
    }
}

void B5_track_async_message_routine(Ads1553MsgQ* msg)
{
    GrifoTgtForm::instance()->processRawTrackMessage(0, msg->getBuffer());
}

void B4_track_async_message_routine(Ads1553MsgQ* msg)
{
    GrifoTgtForm::instance()->processRawTrackMessage(1, msg->getBuffer());
}

void B11_track_async_message_routine(Ads1553MsgQ* msg)
{
    GrifoTgtForm::instance()->processRawTrackMessage(2, msg->getBuffer());
}

void B12_track_async_message_routine(Ads1553MsgQ* msg)
{
    GrifoTgtForm::instance()->processRawTrackMessage(3, msg->getBuffer());
}

void B13_track_async_message_routine(Ads1553MsgQ* msg)
{
    GrifoTgtForm::instance()->processRawTrackMessage(4, msg->getBuffer());
}

void B14_track_async_message_routine(Ads1553MsgQ* msg)
{
    GrifoTgtForm::instance()->processRawTrackMessage(5, msg->getBuffer());
}

void B15_track_async_message_routine(Ads1553MsgQ* msg)
{
    GrifoTgtForm::instance()->processRawTrackMessage(6, msg->getBuffer());
}

void B16_track_async_message_routine(Ads1553MsgQ* msg)
{
    GrifoTgtForm::instance()->processRawTrackMessage(7, msg->getBuffer());
}

void B17_track_async_message_routine(Ads1553MsgQ* msg)
{
    GrifoTgtForm::instance()->processRawTrackMessage(8, msg->getBuffer());
}

void B18_track_async_message_routine(Ads1553MsgQ* msg)
{
    GrifoTgtForm::instance()->processRawTrackMessage(9, msg->getBuffer());
}
