#include <QMessageBox>
#include "mainwindow.h"
#include "./ui_mainwindow.h"


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

    m_instructorsAndTrainees = new InstructorsAndTraineesWidget(this);
    m_messangerWidget = new MessangerWidget();    
    m_serverLMSWidget = new ServerLMSWidget(m_instructorsAndTrainees, m_messangerWidget);

    ui->verticalLayout_1->addWidget(m_serverLMSWidget);
    ui->verticalLayout_2->addWidget(m_instructorsAndTrainees);

    QHBoxLayout *layout = new QHBoxLayout(this);
    ui->verticalLayout_2->addLayout(layout);

    layout->addWidget(m_messangerWidget);

    m_messangerWidget->setMaximumHeight(200);


    connect(m_instructorsAndTrainees, &InstructorsAndTraineesWidget::signal_BlockAutorization, m_serverLMSWidget, &ServerLMSWidget::slot_BlockAutorization);
    connect(m_instructorsAndTrainees, &InstructorsAndTraineesWidget::signal_InitMessanger, m_messangerWidget, &MessangerWidget::slot_InitMessanger);
    connect(m_instructorsAndTrainees, &InstructorsAndTraineesWidget::signal_traineeSelected, m_messangerWidget, &MessangerWidget::slot_traineeSelected);

    connect(m_messangerWidget, &MessangerWidget::signal_tabMessengerChanged, m_instructorsAndTrainees, &InstructorsAndTraineesWidget::signal_tabMessengerChanged);

    connect(this, &MainWindow::signal_LanguageChanged, m_serverLMSWidget, &ServerLMSWidget::slot_LanguageChanged);
    connect(this, &MainWindow::signal_LanguageChanged, m_messangerWidget, &MessangerWidget::slot_LanguageChanged);
    connect(this, &MainWindow::signal_LanguageChanged, m_instructorsAndTrainees, &InstructorsAndTraineesWidget::slot_LanguageChanged);

    //connect(m_serverLMSWidget, &ServerLMSWidget::signal_authorizationChanged, this, &MainWindow::slot_authorizationChanged); //Уже не требуется!


    //Задаём два пункта с текстом локалей в комбобоксе
    ui->cmbLanguage->addItems(QStringList() << "English" << "Русский");

    this->showMaximized();
}

MainWindow::~MainWindow()
{
    delete m_serverLMSWidget;
    delete m_messangerWidget;
    delete m_instructorsAndTrainees;
    delete ui;
}

void MainWindow::changeEvent(QEvent *event)
{
    // В случае получения события изменения языка приложения
    if (event->type() == QEvent::LanguageChange)
    {// переведём окно заново
        ui->retranslateUi(this);
    }
}

void MainWindow::on_cmbLanguage_currentIndexChanged(const QString &arg1)
{
    QString language;

    if(arg1 == QStringLiteral("English"))
        language = QString("en_EN");
    else
        language = QString("ru_RU");

    qtLanguageTranslator.load(QString("translations/testServerLMS_") + language, ".");
    qApp->installTranslator(&qtLanguageTranslator);

    emit signal_LanguageChanged(language);
}
