#include "info_widget.hh"
#include "ui_info_widget.h"
#include <QDateTime>
#include <QDebug>

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

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

void InfoWidget::networkOutput(double value)
{
    ui_->netOutputEdit->setText(QString::number(value));
}

void InfoWidget::tradingTime(bool isTradingTime)
{
    ui_->netTradingEdit->setText(isTradingTime ? "true" : "false");
}

void InfoWidget::networkInfo(const NetworkInfo& networkInfo)
{
    ui_->netFileNameEdit->setText(networkInfo.file);
    ui_->netCurrencyEdit->setText(networkInfo.currency);
    ui_->netTimeFrameEdit->setText(networkInfo.period);
    ui_->netUsedFromEdit->setText(networkInfo.from.toString(Qt::ISODate));
    ui_->netUsedToEdit->setText(networkInfo.to.toString(Qt::ISODate));
}

void InfoWidget::orders(const std::vector<Order>& orders)
{
    clearBuyOrder();
    clearSellOrder();

    for (auto& order : orders) {
        if (order.type == "BUY")
            buyOrder(order);
        else if (order.type == "SELL")
            sellOrder(order);
    }
}

void InfoWidget::clearBuyOrder()
{
    ui_->buyTicketEdit->clear();
    ui_->buyMagicEdit->clear();
    ui_->buyAgeEdit->clear();
    ui_->buyLotsEdit->clear();
    ui_->buyProfitEdit->clear();
}

void InfoWidget::buyOrder(const Order& buyOrder)
{
    ui_->buyTicketEdit->setText(QString::number(buyOrder.ticket));
    ui_->buyMagicEdit->setText(QString::number(buyOrder.magic));

    int age = buyOrder.openTime.secsTo(buyOrder.time) / 60;
    ui_->buyAgeEdit->setText(QString::number(age) + tr(" minutes"));

    ui_->buyLotsEdit->setText(QString::number(buyOrder.lots));
    ui_->buyProfitEdit->setText(QString::number(buyOrder.profit));
}

void InfoWidget::clearSellOrder()
{
    ui_->sellTicketEdit->clear();
    ui_->sellMagicEdit->clear();
    ui_->sellAgeEdit->clear();
    ui_->sellLotsEdit->clear();
    ui_->sellProfitEdit->clear();
}

void InfoWidget::sellOrder(const Order& sellOrder)
{
    ui_->sellTicketEdit->setText(QString::number(sellOrder.ticket));
    ui_->sellMagicEdit->setText(QString::number(sellOrder.magic));

    int age = sellOrder.openTime.secsTo(sellOrder.time) / 60;
    ui_->sellAgeEdit->setText(QString::number(age) + tr(" minutes"));

    ui_->sellLotsEdit->setText(QString::number(sellOrder.lots));
    ui_->sellProfitEdit->setText(QString::number(sellOrder.profit));
}
