#include "modify_device.h"
#include "app_settings.h"
#include <QtGui/QDialogButtonBox>
#include "nsst_define.h"

modify_device::modify_device(QWidget *parent)
    : QDialog(parent)
{
    ui.setupUi(this);

    QValidator *validator = new QIntValidator(2000,60000,this);
    ui.edtPort->setValidator(validator);

    AppSettings::DeviceInfo device_info;
    AppSettingsInstance()->GetDeviceInfo(&device_info);
    ui.widgetIP->settext(device_info.device_ip);
    QString str;
    str.sprintf("%d",device_info.device_port);
    ui.edtPort->setText(str);
    str.sprintf("%d",device_info.device_rtspport);
    ui.edtDataPort->setText(str);
    ui.edtUsername->setText(device_info.device_username);
    ui.edtPassword->setText(device_info.device_password);
    ui.edtPassword->setEchoMode(QLineEdit::Password);
    ui.cbbType->setCurrentIndex(device_info.device_type);

    QObject::connect(ui.buttonBox,SIGNAL(clicked(QAbstractButton*)),this,SLOT(OnButtonsClick(QAbstractButton*)));
    QObject::connect(ui.cbbType,SIGNAL(currentIndexChanged(int)),this,SLOT(OnDeviceTypeChanged(int)));
}

modify_device::~modify_device()
{

}

void modify_device::OnButtonsClick( QAbstractButton* button )
{
    if (ui.buttonBox->button(QDialogButtonBox::Ok) == (QPushButton*)button)
    {
        AppSettings::DeviceInfo device_info;
        device_info.device_ip = ui.widgetIP->text();
        device_info.device_port = ui.edtPort->text().toInt();
        device_info.device_rtspport = ui.edtDataPort->text().toInt();
        device_info.device_username = ui.edtUsername->text();
        device_info.device_password = ui.edtPassword->text();
        device_info.device_type = ui.cbbType->currentIndex();
        AppSettingsInstance()->SetDeviceInfo(device_info);
        this->accept();
    }
    else
    {
        this->reject();
    }
    //hide();
}

void modify_device::OnDeviceTypeChanged( int index )
{
    if (ui.cbbType->currentIndex() == NSSTDEV_K)
    {
        ui.edtPort->setText(QString::number(8000));
    }
    else if (ui.cbbType->currentIndex() == NSSTDEV_A)
    {
        ui.edtPort->setText(QString::number(6666));
    }
    else
    {
        ui.edtPort->setText(QString::number(80));
    }
}
