#include "serversetting.h"
#include "ui_serversetting.h"

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

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

void ServerSetting::initForm()
{
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->setWindowFlags(this->windowFlags() | Qt::WindowSystemMenuHint | Qt::WindowMaximizeButtonHint);
    ui->ledAddress->setPlaceholderText("example.com/ip:port");
    ui->ledPort->setPlaceholderText("admin");
}

void ServerSetting::initWidget()
{
    connect(ui->btnCencel, &QPushButton::clicked, [&](){
        this->hide();
    });
    connect(ui->btnOk, &QPushButton::clicked, [&](){
        emit okClicked(ui->ledAddress->text(), ui->ledPort->text().toInt());
        this->hide();
    });
}

void ServerSetting::on_btnOk_clicked()
{
    if (ui->ledAddress->text().isEmpty() || ui->ledPort->text().isEmpty() ||
            ui->ledPwd->text().isEmpty())
        return;

    QString address = ui->ledAddress->text();
    QString userName = ui->ledPort->text();
    QString password = ui->ledPwd->text();

    TCHttpService::getInstance()->setConfiguration(userName, password, address);
}
