#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ClientOption clOpt;

    clOpt.host = "localhost";
    clOpt.port = 5672;
    clOpt.user = "user";
    clOpt.password = "12345678";

    Properties props;
    props.setAppID("ClientApp");
    props.setDeliveryMode(1);

    pm.setProperties(props);

    ExchangeOption exOpt;
    exOpt.name = "publisher";
    exOpt.type = "fanout";

    sender = new Sender(clOpt, exOpt);

    tm = new QTimer(this);
    connect(tm, SIGNAL(timeout()), this, SLOT(send_msg()));
    tm->start(1000);

    counter = 1;
}

MainWindow::~MainWindow()
{
    tm->stop();
    delete tm;
    sender->slotStop();
    QThread::msleep(150);
    delete sender;
    delete ui;
}

void MainWindow::send_msg()
{
    QString str = "message " + QString::number(counter);
    ui->listWidget->addItem("sent " + str);
    pm.setBodyMsg(QByteArray::fromStdString(str.toStdString()));
    sender->send(pm, "");
    counter++;
}