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

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

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

	setWindowModality(Qt::WindowModal);
	QFormLayout* form = new QFormLayout(formWidget);
	QString value1 = QString(tr("Destination"));
	_des = new QLineEdit(this);
	form->addRow(value1, _des);
	QString value2 = QString(tr("Netmask"));
	_mask = new QLineEdit(this);
	form->addRow(value2, _mask);
	QString value3 = QString(tr("Gateway"));
	_gw = new QLineEdit(this);
	form->addRow(value3, _gw);

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

GetRouteDialog::~GetRouteDialog() {

}

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

bool GetRouteDialog::updateReferenceData() {

	if (!NetworkManager::validate(_des->text())) {
		lbl_error->setText(tr("Wrong Destination!"));
		return false;
	}
	if (!NetworkManager::validate(_des->text())) {
		lbl_error->setText(tr("Wrong Netmask!"));
		return false;
	}
	if (!NetworkManager::validate(_des->text())) {
		lbl_error->setText(tr("Wrong Gateway!"));
		return false;
	}
	return true;
}