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

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

    connect(ui->action, SIGNAL(triggered(bool)), this, SLOT(slotStart()));
    connect(ui->action_2, SIGNAL(triggered(bool)), this, SLOT(slotStop()));

}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::slotStart()
{
    for (int i = 0; i < 5; i++)
    {
        Fork fork;
        list_fork.push_back(fork);
    }

    Philosopher *phil1 = new Philosopher("Socrat", &list_fork, 0);
    Philosopher *phil2 = new Philosopher("Arestotel", &list_fork, 1);
    Philosopher *phil3 = new Philosopher("Platon", &list_fork, 2);
    Philosopher *phil4 = new Philosopher("Evklid", &list_fork, 3);
    Philosopher *phil5 = new Philosopher("Diogen", &list_fork, 4);


    connect(phil1, SIGNAL(signalAddText(QString)), this, SLOT(slotTextOutput(QString)));
    connect(phil2, SIGNAL(signalAddText(QString)), this, SLOT(slotTextOutput(QString)));
    connect(phil3, SIGNAL(signalAddText(QString)), this, SLOT(slotTextOutput(QString)));
    connect(phil4, SIGNAL(signalAddText(QString)), this, SLOT(slotTextOutput(QString)));
    connect(phil5, SIGNAL(signalAddText(QString)), this, SLOT(slotTextOutput(QString)));


    connect(&thread1, SIGNAL(started()), phil1, SLOT(life()));
    connect(&thread2, SIGNAL(started()), phil2, SLOT(life()));
    connect(&thread3, SIGNAL(started()), phil3, SLOT(life()));
    connect(&thread4, SIGNAL(started()), phil4, SLOT(life()));
    connect(&thread5, SIGNAL(started()), phil5, SLOT(life()));


    phil1->moveToThread(&thread1);
    phil2->moveToThread(&thread2);
    phil3->moveToThread(&thread3);
    phil4->moveToThread(&thread4);
    phil5->moveToThread(&thread5);


    thread1.start(QThread::NormalPriority);
    thread2.start(QThread::NormalPriority);
    thread3.start(QThread::NormalPriority);
    thread4.start(QThread::NormalPriority);
    thread5.start(QThread::NormalPriority);
}

void MainWindow::slotStop()
{


}

void MainWindow::slotTextOutput(QString sOut)
{
    QString sss = QString::number(listing);
    sss+= ": " + sOut;

    ui->plainTextEdit->appendPlainText(sss);
    listing++;
}
