#include "formserialportsettingdialog.h"
#include "ui_formserialportsettingdialog.h"

#include <QListView>
#include <QStandardItemModel>
#include <QPushButton>
#include <QMessageBox>
#include <QFile>
#include <QTextStream>
#include <QByteArray>
#include <QDebug>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QGraphicsDropShadowEffect>
#include <QSettings>

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

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

void FormSerialPortSettingDialog::InitializeUi()
{
    this->setStyleSheet("background-color: white;");
    this->setProperty("canMove",true);
    //设置窗体透明
    this->setAttribute(Qt::WA_TranslucentBackground, true);
    //设置无边框
    this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    //实例阴影shadow
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    //设置阴影距离
    shadow->setOffset(0, 0);
    //设置阴影颜色
    shadow->setColor(QColor(44,44,44));
    //设置阴影圆角
    shadow->setBlurRadius(16);
    //给嵌套QDialog设置阴影
    ui->w_bg->setGraphicsEffect(shadow);
    //给垂直布局器设置边距(此步很重要, 设置宽度为阴影的宽度)
    //ui->lay_bg->setMargin(12);

    QApplication::setOverrideCursor(Qt::WaitCursor);

    ui->delay_timeout->setText("1000");
    ui->slave_id->setText("1");

    //设置奇偶校验
    ui->cb_parity->setView(new  QListView(this));
    ui->cb_parity->addItem("NoParity", QVariant::fromValue(QSerialPort::NoParity));
    ui->cb_parity->addItem("Even", QVariant::fromValue(QSerialPort::EvenParity));
    ui->cb_parity->addItem("Odd", QVariant::fromValue(QSerialPort::OddParity));
    ui->cb_parity->setCurrentIndex(0);

    //设置数据位
    ui->cb_data->setView(new  QListView(this));
    ui->cb_data->addItem("5", QVariant::fromValue(QSerialPort::Data5));
    ui->cb_data->addItem("6", QVariant::fromValue(QSerialPort::Data6));
    ui->cb_data->addItem("7", QVariant::fromValue(QSerialPort::Data7));
    ui->cb_data->addItem("8", QVariant::fromValue(QSerialPort::Data8));
    ui->cb_data->setCurrentIndex(3);

    //设置数据位
    ui->cb_stop->setView(new  QListView(this));
    ui->cb_stop->addItem("1", QVariant::fromValue(QSerialPort::OneStop));
    ui->cb_stop->addItem("2", QVariant::fromValue(QSerialPort::TwoStop));
    ui->cb_stop->addItem("3", QVariant::fromValue(QSerialPort::OneAndHalfStop));
    ui->cb_stop->setCurrentIndex(0);


    //设置串口模式
    //ui->cb_baund->setView(new  QListView(this));
    //QStandardItemModel* model_baund = new QStandardItemModel(this);

    ui->cb_baund->addItem("4800", QVariant::fromValue(QSerialPort::Baud4800));
    ui->cb_baund->addItem("9600", QVariant::fromValue(QSerialPort::Baud9600));
    ui->cb_baund->addItem("19200", QVariant::fromValue(QSerialPort::Baud19200));
    ui->cb_baund->addItem("38400", QVariant::fromValue(QSerialPort::Baud38400));
    ui->cb_baund->addItem("57600", QVariant::fromValue(QSerialPort::Baud57600));
    ui->cb_baund->addItem("115200", QVariant::fromValue(QSerialPort::Baud115200));
    ui->cb_baund->setCurrentIndex(1);

    //设置串口类型
    ui->cb_type->setView(new  QListView(this));
    ui->cb_type->addItem("Modbus RTU");

    //设置串口号
    ui->cb_serialport->setView(new  QListView(this));
    QStringList comport = get_avail_sp_();
    foreach (const QString& str, comport)
    {
        QStringList parts = str.split("-", Qt::SkipEmptyParts);
        ui->cb_serialport->addItem(str,parts[0].trimmed());
    }

    ui->interval->setText("10");

    ui->buttonBox->clear();
    QPushButton *okButton = ui->buttonBox->addButton(QDialogButtonBox::Ok);
    QPushButton *cancelButton = ui->buttonBox->addButton(QDialogButtonBox::Cancel);
    okButton->setText(tr("OK"));
    cancelButton->setText(tr("Close"));
    okButton->setFixedSize(100, 30);
    cancelButton->setFixedSize(100, 30);

    connect(okButton, &QPushButton::clicked, this, &FormSerialPortSettingDialog::onOkClicked);
    connect(cancelButton, &QPushButton::clicked, this, &FormSerialPortSettingDialog::onCancelClicked);

    // 恢复默认光标
    QApplication::restoreOverrideCursor();
}

QStringList FormSerialPortSettingDialog::get_avail_sp_() noexcept
{
    QStringList list_avail_sp;

    foreach (const QSerialPortInfo &port, QSerialPortInfo::availablePorts())
    {
        list_avail_sp.append(port.portName() + " - " + port.description());
    }

    list_avail_sp.sort();

    return list_avail_sp;
}

void FormSerialPortSettingDialog::onOkClicked()
{
    //写到INI文件
    QString appDir = QCoreApplication::applicationDirPath();
    QString iniFilePath = appDir + QString::fromStdString("/emsshower.ini");
    QSettings* pSettings = new QSettings(iniFilePath, QSettings::IniFormat,this);

    //写选中的串口
    int baund = ui->cb_baund->currentData().toInt();
    QString com = ui->cb_serialport->currentData().toString();
    int data = ui->cb_data->currentData().toInt();
    int parity = ui->cb_parity->currentData().toInt();
    int stop = ui->cb_stop->currentData().toInt();
    int slave_id = ui->slave_id->text().toInt();
    int intervai = ui->interval->text().toInt();

    pSettings->setValue("modbus/baund",baund);
    pSettings->setValue("modbus/com",com);
    pSettings->setValue("modbus/data",data);
    pSettings->setValue("modbus/parity",parity);
    pSettings->setValue("modbus/stop",stop);
    pSettings->setValue("modbus/interval",intervai);
    pSettings->setValue("slaves/slave_id",slave_id);

    accept();
}

void FormSerialPortSettingDialog::onCancelClicked()
{
    QDialog::reject();
}