//
// Created by Krad on 2021/11/11.
//

#include <QFormLayout>
#include <QLabel>
#include <QtWidgets/QLineEdit>
#include "getipdialog.h"
#include "device/networkmanager.h"

GetIPDialog::GetIPDialog(QWidget* parent, Qt::WindowFlags f) : GUIFormBaseDialog(parent, f) {

	setWindowModality(Qt::WindowModal);
	QFormLayout* form = new QFormLayout(formWidget);
	QLabel* value1 = new QLabel(tr("IP Address"));
	_ip = new QLineEdit(this);
	form->addRow(value1, _ip);

	QLabel* value2 = new QLabel(tr("Netmask"));
	_mask = new QLineEdit(this);
	form->addRow(value2, _mask);

	lbl_error = new QLabel(this);
	lbl_error->setObjectName(QString::fromUtf8("warn"));
	form->addRow("", lbl_error);

}

GetIPDialog::~GetIPDialog() {

}

QStringList GetIPDialog::getList()const
{
	QStringList tmp;
	tmp << _ip->text() << _mask->text();
	return tmp;
}
void GetIPDialog::setList(const QStringList& list)
{
	if (!list.empty()) {
		_ip->setText(list[0]);
		_mask->setText(list[1]);
	}
}

bool GetIPDialog::updateReferenceData() {

	if (!NetworkManager::validate(_ip->text())) {
		lbl_error->setText(tr("Wrong IP!"));
		return false;
	}
	if (!NetworkManager::validate(_mask->text())) {
		lbl_error->setText(tr("Wrong Netmask!"));
		return false;
	}
	return true;
}
