#include "grifodltgtform.h"
#include "ui_grifodltgtform.h"

#include "g346_common.h"
#include "ads_data.h"

#include <QGridLayout>
#include <QCheckBox>
#include <QLineEdit>
#include <QSpinBox>
#include <QLabel>
#include <QGroupBox>
#include <QSpacerItem>

#include "icd_g346.h"


using namespace grifo_icd;

class Implementation
{
public:
    QGridLayout* gly;
};

GrifoDlTgtForm::GrifoDlTgtForm(QWidget *parent) :
    QScrollArea(parent),
    ui(new Ui::grifodltgtform)
{    
    ui->setupUi(this);

    Ads1553MsgQ* a7=Ads1553Manager::instance().getMsgObj("A7");
    Ads1553MsgQ* a8=Ads1553Manager::instance().getMsgObj("A8");

    //Implementation p_;

    //groupbox con dentro tutti i datalink
    QGroupBox *gbDataLink= new QGroupBox("Datalink");

    //grid per l'inserimento dei dati ne groupbox
    QGridLayout *grid = new QGridLayout(gbDataLink);

    QSpacerItem *VertSpacer = new QSpacerItem(0,10, QSizePolicy::Expanding, QSizePolicy::Maximum);

    QSpacerItem *HorSpacer = new QSpacerItem(0,10, QSizePolicy::Expanding, QSizePolicy::Maximum);

    QHBoxLayout *HorLay = new QHBoxLayout();

    ui->verticalLayout->insertLayout(0, HorLay);
    HorLay->addWidget(gbDataLink);
    HorLay->addSpacerItem(HorSpacer);
    ui->verticalLayout->addSpacerItem(VertSpacer);

    QString sname;

    //associo ai datasource il loro nome corretto
    for(int i=0; i<10; ++i)
    {
        sname=QString("active_dl_%1").arg(i+1);
        AdsLogicData& d1=dlActive[i];
        d1.setDataName(sname.toLatin1().constData());
        d1.setObjectName(sname.toLatin1().constData());

        sname=QString("char_dl_%1").arg(i+1);
        AdsLogicData& d2=dlChar[i];
        d2.setDataName(sname.toLatin1().constData());
        d2.setObjectName(sname.toLatin1().constData());

        sname=QString("x_dl_%1").arg(i+1);
        AdsLogicData& d3=dlX[i];
        d3.setDataName(sname.toLatin1().constData());
        d3.setObjectName(sname.toLatin1().constData());

        sname=QString("y_dl_%1").arg(i+1);
        AdsLogicData& d4=dlY[i];
        d4.setDataName(sname.toLatin1().constData());
        d4.setObjectName(sname.toLatin1().constData());

        sname=QString("gnd_dl_%1").arg(i+1);
        AdsLogicData& d5=dlGndAngle[i];
        d5.setDataName(sname.toLatin1().constData());
        d5.setObjectName(sname.toLatin1().constData());

        sname=QString("vel_dl_%1").arg(i+1);
        AdsLogicData& d6=dlVel[i];
        d6.setDataName(sname.toLatin1().constData());
        d6.setObjectName(sname.toLatin1().constData());

        sname=QString("alt_dl_%1").arg(i+1);
        AdsLogicData& d7=dlAlt[i];
        d7.setDataName(sname.toLatin1().constData());
        d7.setObjectName(sname.toLatin1().constData());

        sname=QString("tgt_dl_%1").arg(i+1);
        AdsLogicData& d8=dlTgtInZone[i];
        d8.setDataName(sname.toLatin1().constData());
        d8.setObjectName(sname.toLatin1().constData());
    }

    //collego i dati relativi alla validità dei tgt del messaggio a7
    GRIFO_ADS_MAP(dlActive[0], "active_dl_1", msg1_data_link_target_t, dl_target_validity , dl_target_validity_01_field_t, *a7);
    GRIFO_ADS_MAP(dlActive[1], "active_dl_2", msg1_data_link_target_t, dl_target_validity , dl_target_validity_02_field_t, *a7);
    GRIFO_ADS_MAP(dlActive[2], "active_dl_3", msg1_data_link_target_t, dl_target_validity , dl_target_validity_03_field_t, *a7);
    GRIFO_ADS_MAP(dlActive[3], "active_dl_4", msg1_data_link_target_t, dl_target_validity , dl_target_validity_04_field_t, *a7);
    GRIFO_ADS_MAP(dlActive[4], "active_dl_5", msg1_data_link_target_t, dl_target_validity , dl_target_validity_05_field_t, *a7);
    GRIFO_ADS_MAP(dlActive[5], "active_dl_6", msg1_data_link_target_t, dl_target_validity , dl_target_validity_06_field_t, *a7);
    GRIFO_ADS_MAP(dlActive[6], "active_dl_7", msg1_data_link_target_t, dl_target_validity , dl_target_validity_07_field_t, *a7);
    GRIFO_ADS_MAP(dlActive[7], "active_dl_8", msg1_data_link_target_t, dl_target_validity , dl_target_validity_08_field_t, *a7);
    GRIFO_ADS_MAP(dlActive[8], "active_dl_9", msg1_data_link_target_t, dl_target_validity , dl_target_validity_09_field_t, *a7);
    GRIFO_ADS_MAP(dlActive[9], "active_dl_10", msg1_data_link_target_t, dl_target_validity , dl_target_validity_10_field_t, *a7);

    GRIFO_ADS_MAP(dlX[0], "x_dl_1", msg1_data_link_target_t, tgt_01_pos_x , target_position_x_t, *a7);
    GRIFO_ADS_MAP(dlX[1], "x_dl_2", msg1_data_link_target_t, tgt_02_pos_x , target_position_x_t, *a7);
    GRIFO_ADS_MAP(dlX[2], "x_dl_3", msg1_data_link_target_t, tgt_03_pos_x , target_position_x_t, *a7);
    GRIFO_ADS_MAP(dlX[3], "x_dl_4", msg1_data_link_target_t, tgt_03_pos_x , target_position_x_t, *a7);
    GRIFO_ADS_MAP(dlX[4], "x_dl_5", msg1_data_link_target_t, tgt_05_pos_x , target_position_x_t, *a7);
    GRIFO_ADS_MAP(dlX[5], "x_dl_6", msg2_data_link_target_t, tgt_06_pos_x , target_position_x_t, *a8);
    GRIFO_ADS_MAP(dlX[6], "x_dl_7", msg2_data_link_target_t, tgt_07_pos_x , target_position_x_t, *a8);
    GRIFO_ADS_MAP(dlX[7], "x_dl_8", msg2_data_link_target_t, tgt_08_pos_x , target_position_x_t, *a8);
    GRIFO_ADS_MAP(dlX[8], "x_dl_9", msg2_data_link_target_t, tgt_09_pos_x , target_position_x_t, *a8);
    GRIFO_ADS_MAP(dlX[9], "x_dl_10", msg2_data_link_target_t, tgt_10_pos_x , target_position_x_t, *a8);

    GRIFO_ADS_MAP(dlY[0], "y_dl_1", msg1_data_link_target_t, tgt_01_pos_y , target_position_y_t, *a7);
    GRIFO_ADS_MAP(dlY[1], "y_dl_2", msg1_data_link_target_t, tgt_02_pos_y , target_position_y_t, *a7);
    GRIFO_ADS_MAP(dlY[2], "y_dl_3", msg1_data_link_target_t, tgt_03_pos_y , target_position_y_t, *a7);
    GRIFO_ADS_MAP(dlY[3], "y_dl_4", msg1_data_link_target_t, tgt_03_pos_y , target_position_y_t, *a7);
    GRIFO_ADS_MAP(dlY[4], "y_dl_5", msg1_data_link_target_t, tgt_05_pos_y , target_position_y_t, *a7);
    GRIFO_ADS_MAP(dlY[5], "y_dl_6", msg2_data_link_target_t, tgt_06_pos_y , target_position_y_t, *a8);
    GRIFO_ADS_MAP(dlY[6], "y_dl_7", msg2_data_link_target_t, tgt_07_pos_y , target_position_y_t, *a8);
    GRIFO_ADS_MAP(dlY[7], "y_dl_8", msg2_data_link_target_t, tgt_08_pos_y , target_position_y_t, *a8);
    GRIFO_ADS_MAP(dlY[8], "y_dl_9", msg2_data_link_target_t, tgt_09_pos_y , target_position_y_t, *a8);
    GRIFO_ADS_MAP(dlY[9], "y_dl_10", msg2_data_link_target_t, tgt_10_pos_y , target_position_y_t, *a8);

    GRIFO_ADS_MAP(dlGndAngle[0], "gnd_dl_1", msg1_data_link_target_t, tgt_01_ground_track_angle , target_ground_track_angle_t, *a7);
    GRIFO_ADS_MAP(dlGndAngle[1], "gnd_dl_2", msg1_data_link_target_t, tgt_02_ground_track_angle , target_ground_track_angle_t, *a7);
    GRIFO_ADS_MAP(dlGndAngle[2], "gnd_dl_3", msg1_data_link_target_t, tgt_03_ground_track_angle , target_ground_track_angle_t, *a7);
    GRIFO_ADS_MAP(dlGndAngle[3], "gnd_dl_4", msg1_data_link_target_t, tgt_04_ground_track_angle , target_ground_track_angle_t, *a7);
    GRIFO_ADS_MAP(dlGndAngle[4], "gnd_dl_5", msg1_data_link_target_t, tgt_05_ground_track_angle , target_ground_track_angle_t, *a7);
    GRIFO_ADS_MAP(dlGndAngle[5], "gnd_dl_6", msg2_data_link_target_t, tgt_06_ground_track_angle , target_ground_track_angle_t, *a8);
    GRIFO_ADS_MAP(dlGndAngle[6], "gnd_dl_7", msg2_data_link_target_t, tgt_07_ground_track_angle , target_ground_track_angle_t, *a8);
    GRIFO_ADS_MAP(dlGndAngle[7], "gnd_dl_8", msg2_data_link_target_t, tgt_08_ground_track_angle , target_ground_track_angle_t, *a8);
    GRIFO_ADS_MAP(dlGndAngle[8], "gnd_dl_9", msg2_data_link_target_t, tgt_09_ground_track_angle , target_ground_track_angle_t, *a8);
    GRIFO_ADS_MAP(dlGndAngle[9], "gnd_dl_10", msg2_data_link_target_t, tgt_10_ground_track_angle , target_ground_track_angle_t, *a8);

    GRIFO_ADS_MAP(dlVel[0], "vel_dl_1", msg1_data_link_target_t, tgt_01_presentation_data , velocity_of_target_t, *a7);
    GRIFO_ADS_MAP(dlVel[1], "vel_dl_2", msg1_data_link_target_t, tgt_02_presentation_data , velocity_of_target_t, *a7);
    GRIFO_ADS_MAP(dlVel[2], "vel_dl_3", msg1_data_link_target_t, tgt_03_presentation_data , velocity_of_target_t, *a7);
    GRIFO_ADS_MAP(dlVel[3], "vel_dl_4", msg1_data_link_target_t, tgt_04_presentation_data , velocity_of_target_t, *a7);
    GRIFO_ADS_MAP(dlVel[4], "vel_dl_5", msg1_data_link_target_t, tgt_05_presentation_data , velocity_of_target_t, *a7);
    GRIFO_ADS_MAP(dlVel[5], "vel_dl_6", msg2_data_link_target_t, tgt_06_presentation_data , velocity_of_target_t, *a8);
    GRIFO_ADS_MAP(dlVel[6], "vel_dl_7", msg2_data_link_target_t, tgt_07_presentation_data , velocity_of_target_t, *a8);
    GRIFO_ADS_MAP(dlVel[7], "vel_dl_8", msg2_data_link_target_t, tgt_08_presentation_data , velocity_of_target_t, *a8);
    GRIFO_ADS_MAP(dlVel[8], "vel_dl_9", msg2_data_link_target_t, tgt_09_presentation_data , velocity_of_target_t, *a8);
    GRIFO_ADS_MAP(dlVel[9], "vel_dl_10", msg2_data_link_target_t, tgt_10_presentation_data , velocity_of_target_t, *a8);

    GRIFO_ADS_MAP(dlAlt[0], "alt_dl_1", msg1_data_link_target_t, tgt_01_presentation_data , altitude_of_target_t, *a7);
    GRIFO_ADS_MAP(dlAlt[1], "alt_dl_2", msg1_data_link_target_t, tgt_02_presentation_data , altitude_of_target_t, *a7);
    GRIFO_ADS_MAP(dlAlt[2], "alt_dl_3", msg1_data_link_target_t, tgt_03_presentation_data , altitude_of_target_t, *a7);
    GRIFO_ADS_MAP(dlAlt[3], "alt_dl_4", msg1_data_link_target_t, tgt_04_presentation_data , altitude_of_target_t, *a7);
    GRIFO_ADS_MAP(dlAlt[4], "alt_dl_5", msg1_data_link_target_t, tgt_05_presentation_data , altitude_of_target_t, *a7);
    GRIFO_ADS_MAP(dlAlt[5], "alt_dl_6", msg2_data_link_target_t, tgt_06_presentation_data , altitude_of_target_t, *a8);
    GRIFO_ADS_MAP(dlAlt[6], "alt_dl_7", msg2_data_link_target_t, tgt_07_presentation_data , altitude_of_target_t, *a8);
    GRIFO_ADS_MAP(dlAlt[7], "alt_dl_8", msg2_data_link_target_t, tgt_08_presentation_data , altitude_of_target_t, *a8);
    GRIFO_ADS_MAP(dlAlt[8], "alt_dl_9", msg2_data_link_target_t, tgt_09_presentation_data , altitude_of_target_t, *a8);
    GRIFO_ADS_MAP(dlAlt[9], "alt_dl_10", msg2_data_link_target_t, tgt_10_presentation_data , altitude_of_target_t, *a8);

    GRIFO_ADS_MAP(dlTgtInZone[0], "tgt_dl_1", msg1_data_link_target_t, tgt_01_presentation_data , intercept_zone_selector_t, *a7);
    GRIFO_ADS_MAP(dlTgtInZone[1], "tgt_dl_2", msg1_data_link_target_t, tgt_02_presentation_data , intercept_zone_selector_t, *a7);
    GRIFO_ADS_MAP(dlTgtInZone[2], "tgt_dl_3", msg1_data_link_target_t, tgt_03_presentation_data , intercept_zone_selector_t, *a7);
    GRIFO_ADS_MAP(dlTgtInZone[3], "tgt_dl_4", msg1_data_link_target_t, tgt_04_presentation_data , intercept_zone_selector_t, *a7);
    GRIFO_ADS_MAP(dlTgtInZone[4], "tgt_dl_5", msg1_data_link_target_t, tgt_05_presentation_data , intercept_zone_selector_t, *a7);
    GRIFO_ADS_MAP(dlTgtInZone[5], "tgt_dl_6", msg2_data_link_target_t, tgt_06_presentation_data , intercept_zone_selector_t, *a8);
    GRIFO_ADS_MAP(dlTgtInZone[6], "tgt_dl_7", msg2_data_link_target_t, tgt_07_presentation_data , intercept_zone_selector_t, *a8);
    GRIFO_ADS_MAP(dlTgtInZone[7], "tgt_dl_8", msg2_data_link_target_t, tgt_08_presentation_data , intercept_zone_selector_t, *a8);
    GRIFO_ADS_MAP(dlTgtInZone[8], "tgt_dl_9", msg2_data_link_target_t, tgt_09_presentation_data , intercept_zone_selector_t, *a8);
    GRIFO_ADS_MAP(dlTgtInZone[9], "tgt_dl_10", msg2_data_link_target_t, tgt_10_presentation_data , intercept_zone_selector_t, *a8);

    QLabel* lbl1=new QLabel("val",gbDataLink);
    grid->addWidget(lbl1, 0, 0);
    QLabel* lbl2=new QLabel("desc",gbDataLink);
    grid->addWidget(lbl2, 0, 1);
    QLabel* lbl3=new QLabel("X",gbDataLink);
    grid->addWidget(lbl3, 0, 2);
    QLabel* lbl4=new QLabel("Y",gbDataLink);
    grid->addWidget(lbl4, 0, 3);
    QLabel* lbl5=new QLabel("gnd angle",gbDataLink);
    grid->addWidget(lbl5, 0, 4);
    QLabel* lbl6=new QLabel("vel",gbDataLink);
    grid->addWidget(lbl6, 0, 5);
    QLabel* lbl7=new QLabel("alt",gbDataLink);
    grid->addWidget(lbl7, 0, 6);
    QLabel* lbl8=new QLabel("in zone",gbDataLink);
    grid->addWidget(lbl8, 0, 7);

    //creo i controlli sulla finestra e collego i datasource
    for(int i=0; i<10; ++i)
    {
        sname = dlActive[i].objectName();
        QCheckBox* cb_active=new QCheckBox(QString("%1").arg(i+1), gbDataLink);
        cb_active->setObjectName(QString("sp_ads_%1").arg(sname));
        cb_active->setProperty("adv_datasource", sname);

        QLineEdit* le_char=new QLineEdit(QString("DL%1").arg(i+1), gbDataLink);
        le_char->setMaxLength(4);
        le_char->setFixedWidth(50);

        sname = dlX[i].objectName();
        QSpinBox* sb_x=new QSpinBox(gbDataLink);
        sb_x->setObjectName(QString("sp_ads_%1").arg(sname));
        sb_x->setProperty("adv_datasource", sname);

        sname = dlY[i].objectName();
        QSpinBox* sb_y=new QSpinBox(gbDataLink);
        sb_y->setObjectName(QString("sp_ads_%1").arg(sname));
        sb_y->setProperty("adv_datasource", sname);

        sname = dlGndAngle[i].objectName();
        QSpinBox* sb_gnd_angle=new QSpinBox(gbDataLink);
        sb_gnd_angle->setObjectName(QString("sp_ads_%1").arg(sname));
        sb_gnd_angle->setProperty("adv_datasource", sname);

        sname = dlVel[i].objectName();
        QSpinBox* sb_vel=new QSpinBox(gbDataLink);
        sb_vel->setObjectName(QString("sp_ads_%1").arg(sname));
        sb_vel->setProperty("adv_datasource", sname);

        sname = dlAlt[i].objectName();
        QSpinBox* sb_alt=new QSpinBox(gbDataLink);
        sb_alt->setObjectName(QString("sp_ads_%1").arg(sname));
        sb_alt->setProperty("adv_datasource", sname);

        sname = dlTgtInZone[i].objectName();
        QCheckBox* cb_tgt_int=new QCheckBox("",gbDataLink);
        cb_tgt_int->setObjectName(QString("sp_ads_%1").arg(sname));
        cb_tgt_int->setProperty("adv_datasource", sname);

        grid->addWidget(cb_active, i+1, 0);
        grid->addWidget(le_char, i+1, 1);
        grid->addWidget(sb_x, i+1, 2);
        grid->addWidget(sb_y, i+1, 3);
        grid->addWidget(sb_gnd_angle, i+1,4);
        grid->addWidget(sb_vel, i+1,5);
        grid->addWidget(sb_alt, i+1,6);
        grid->addWidget(cb_tgt_int, i+1,7);

    }
}

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