#include "communicationsettingdlg.h"

#include "ui_communicationsettingdlg.h"

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

    this->setWindowTitle("通信设置");

    initCombobox();
    initNetworkSetting();
}

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

void CommunicationSettingDlg::showEvent(QShowEvent *event) {
    setSaveSettingParms();
}

void CommunicationSettingDlg::initCombobox() {
    ui->dataSourceCombox->addItem(QStringLiteral("L测控"));
    ui->dataSourceCombox->addItem(QStringLiteral("卫通1路"));
    ui->dataSourceCombox->addItem(QStringLiteral("卫通2路"));
    ui->dataSourceCombox->setCurrentIndex(0);
}

void CommunicationSettingDlg::initNetworkSetting() {
    QStringList localIPList;
    getLocalIP(localIPList);
    ui->localIP->addItems(localIPList);

    ui->remoteIPEdit->setText(QStringLiteral("223.0.0.1"));
    ui->remotePort->setValue(8080);
    ui->localPort->setValue(8082);
}

void CommunicationSettingDlg::setSaveSettingParms() {
    if (!g_networkSettingInfo) return;
    int dataSourceType = g_networkSettingInfo->value("DataSource").toInt();
    ui->dataSourceCombox->setCurrentIndex(dataSourceType);
    QString groupName = "";
    switch (dataSourceType) {
        case 0:
            groupName = "LLink";
            break;
        case 1:
            groupName = "SATCOM1";
            break;
        case 2:
            groupName = "SATCOM2";
            break;
        default:
            break;
    }
    ui->remoteIPEdit->setText(
        g_networkSettingInfo->value(groupName + "/remoteIP").toString());
    ui->remotePort->setValue(
        g_networkSettingInfo->value(groupName + "/remotePort").toInt());
    ui->localPort->setValue(
        g_networkSettingInfo->value(groupName + "/localPort").toInt());

    QString localIP =
        g_networkSettingInfo->value(groupName + "/localIP").toString();
    for (int i = 0; i < ui->dataSourceCombox->count(); ++i) {
        if (localIP == ui->localIP->itemText(i)) {
            ui->localIP->setCurrentIndex(i);
        }
    }
}

// 保存设置
void CommunicationSettingDlg::on_saveSettingBtn_clicked() {
    ui->saveSettingBtn->setDisabled(true);
    int dataType = ui->dataSourceCombox->currentIndex();

    QString remoteIP = ui->remoteIPEdit->text();
    if (dataType == 0) {  // L链为组播
        if (!isMulticastAddress(remoteIP)) {
            emit sendErrorMessage("当前IP地址为单播地址，请改为组播地址!", 2);
            ui->saveSettingBtn->setDisabled(false);
            return;
        };
    } else {  // 卫通为单播
        if (isMulticastAddress(remoteIP)) {
            emit sendErrorMessage("当前IP地址为组播地址，请改为单播地址!", 2);
            ui->saveSettingBtn->setDisabled(false);
            return;
        };
    }

    // 保存配置参数
    if (!g_networkSettingInfo) return;
    g_networkSettingInfo->setValue("DataSource", dataType);
    QString groupName = "";
    switch (dataType) {
        case 0:
            groupName = "LLink";
            break;
        case 1:
            groupName = "SATCOM1";
            break;
        case 2:
            groupName = "SATCOM2";
            break;
        default:
            break;
    }
    g_networkSettingInfo->setValue(groupName + "/localIP",
                                   ui->localIP->currentText());
    g_networkSettingInfo->setValue(groupName + "/localPort",
                                   ui->localPort->value());
    g_networkSettingInfo->setValue(groupName + "/remoteIP",
                                   ui->remoteIPEdit->text());
    g_networkSettingInfo->setValue(groupName + "/remotePort",
                                   ui->remotePort->value());

    ui->saveSettingBtn->setDisabled(false);
    emit sendErrorMessage("保存成功!", 1);
}
