#include "grifocmddock.h"
#include "ui_grifocmddock.h"

#include <QShortcut>
#include <QDebug>
#include <QToolButton>
#include <QAction>

static const char* cmdStr[]=
{
    "auto",
    "lprf",
    "mprf",
    "hprf",
    "lprf_nc",
    "mprf_short",
    "agr_short",
    "agr_long",
    "mprf_look_up",
    "mprf_adaptive",
    "no_lprf_nc",
    0
};

GrifoCmdDock::GrifoCmdDock(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::GrifoCmdDock),
    storedWfCmd(0)
{
    ui->setupUi(this);

    //ui->tb_ads_silence->setVisible(false);
    ui->tb_ads_silence->setEnabled(false);

    ui->tbSpecificCommands->setVisible(false);
    ui->cb_AutoselectPanel->setVisible(false);

    {
        for(int i=0; cmdStr[i]; ++i)
        {
            QAction* a=new QAction(cmdStr[i], this);
            a->setData(i);
            ui->tbWfCmd->addAction(a);
            if (i==0)
                ui->tbWfCmd->setText(QString("WF: %1").arg(cmdStr[i]));

            connect(a, &QAction::triggered, this, [this](bool checked)
            {
                QAction* a=qobject_cast<QAction*>(sender());
                int cmd=a->data().toInt();
                qDebug()<<"WFACT:"<<cmd;
                ui->tbWfCmd->setText(QString("WF: %1").arg(cmdStr[cmd]));
                storedWfCmd=cmd;
                wfCmdUpdate(storedWfCmd);
            });
        }
        connect(ui->tbWfCmd, &QToolButton::clicked, this, [this]()
        {
           qDebug()<<"WFCMD:"<<storedWfCmd<<cmdStr[storedWfCmd];
           wfCmdUpdate(storedWfCmd);
        });
    }

    connect(ui->tbElPlus, &QToolButton::clicked, this, [this]() { Q_EMIT this->elevationPlus();});
    connect(ui->tbElMinus, &QToolButton::clicked, this, [this]() { Q_EMIT this->elevationMinus();});
    connect(ui->tbEl0, &QToolButton::clicked, this, [this]() { Q_EMIT this->elevationZero();});

    QObject::connect(new QShortcut(Qt::Key_F9, ui->tbElPlus), &QShortcut::activated, this, [this](){ui->tbElPlus->click();});
    QObject::connect(new QShortcut(Qt::Key_F10, ui->tbElMinus), &QShortcut::activated, this, [this](){ui->tbElMinus->click();});
}

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

bool GrifoCmdDock::autoSelectPanel() const
{
  return ui->cb_AutoselectPanel->isChecked();
}

void GrifoCmdDock::selectControlDock(int n)
{
  ui->tbSpecificCommands->setCurrentIndex(n);
}

bool GrifoCmdDock::smartAntenna() const
{
  return ui->cbSmartAnenna->checkState()!=Qt::Unchecked;
}

bool GrifoCmdDock::smartAntennaCoverage() const
{
  return ui->cbSmartAnenna->checkState()==Qt::Checked;
}

void GrifoCmdDock::wfCmdUpdate(int n)
{
    Q_EMIT waveformCommand(n);
}
