#include "entrywidget.h"
#include "ui_entrywidget.h"

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

void EntryWidget::initialize(MainWindow *mainWindow)
{
    ui->settingsWidget->hide();
    ui->offlineWidget->hide();
    ui->loginWidget->hide();
    this->mainWindow = mainWindow;

}

void EntryWidget::connectionEmptyState()
{
    ui->offlineWidget->show();
    ui->loginWidget->hide();
}

void EntryWidget::settingsState()
{
    ui->settingsWidget->show();
    ui->loginWidget->hide();
    ui->offlineWidget->hide();
}

bool EntryWidget::isLoginFieldsFill()
{
    return ui->loginInputField->text().length() <= 0 || ui->passwordInputField->text() <= 0;
}

ClientAutorization* EntryWidget::getAuthData()
{
    ClientAutorization *data = new ClientAutorization;
    QString username = ui->loginInputField->text();
    QString password = ui->passwordInputField->text();
    data->Login = username;
    data->Password = password;

    return data;
}

ServerSettings *EntryWidget::getServerSettings()
{
    ServerSettings *data = new ServerSettings;
    QString server = ui->serverInputField->text();
    QString port = ui->portInputField->text();

    data->Address = server;
    data->Port = port;

    return data;
}

void EntryWidget::loginIsActive(bool flag)
{
    if(flag) ui->loginWidget->show();
    else ui->loginWidget->hide();
}

void EntryWidget::settingsWidgetIsActive(bool flag)
{
    if(flag) ui->settingsWidget->show();
    else ui->settingsWidget->hide();
}

void EntryWidget::fillSettings(ServerSettings *settings)
{
    ui->serverInputField->setText(settings->Address);
    ui->portInputField->setText(settings->Port);
}

void EntryWidget::on_loginButton_clicked()
{
    mainWindow->login();
}

void EntryWidget::on_saveServerButton_clicked()
{
    mainWindow->saveServerSettingsWithConnect();
}

EntryWidget::~EntryWidget()
{
    delete ui;
}



