#include "control_dialog.hh"
#include "ui_control_dialog.h"

namespace {

QString toString(const TradeData& data, QString name, int index)
{
    if(!data.hasValue(name.toStdString()))
        return QObject::tr("N/A");

    if (index >= data.valueCount(name.toStdString()))
        return QObject::tr("N/A");

    return QString::number(data.value(name.toStdString(), index).second);
}

} //namespace

ControlDialog::ControlDialog(QWidget* parent) :
    QDialog(parent), ui_(new Ui::ControlDialog)
{
    ui_->setupUi(this);
}

void ControlDialog::setData(const TradeData& data)
{
    ui_->ema10_0->setText(toString(data, "EMA10", 0));
    ui_->ema10_1->setText(toString(data, "EMA10", 1));
    ui_->ema10_2->setText(toString(data, "EMA10", 2));
    ui_->ema20_0->setText(toString(data, "EMA20", 0));
    ui_->ema20_1->setText(toString(data, "EMA20", 1));
    ui_->ema20_2->setText(toString(data, "EMA20", 2));

    if(data.hasOrder()) {
        auto& order = data.order(0);
        ui_->orderTicket->setText(QString::number(order.ticket));
        ui_->orderType->setText(QString::fromStdString(order.type));
        ui_->orderOpenPrice->setText(QString::number(order.openPrice));
        ui_->orderLots->setText(QString::number(order.lots));
        ui_->orderStopLoss->setText(QString::number(order.stopLoss));
        ui_->orderTakeProfit->setText(QString::number(order.takeProfit));
        ui_->orderProfit->setText(QString::number(order.profit));
    } else {
        ui_->orderTicket->setText("N/A");
        ui_->orderType->setText("N/A");
        ui_->orderOpenPrice->setText("N/A");
        ui_->orderLots->setText("N/A");
        ui_->orderStopLoss->setText("N/A");
        ui_->orderTakeProfit->setText("N/A");
        ui_->orderProfit->setText("N/A");
    }
}

ControlDialog::~ControlDialog()
{
    delete ui_;
}
